21474d931e89 — Vesa Norilo 5 months ago
move ownership of RepositoryNodes to master singleton, persistent lifetime and ensured immutable relationship between package/file and pointer
2 files changed, 15 insertions(+), 11 deletions(-)

M src/k3/CodeRepository.cpp
M src/k3/Parser.h
M src/k3/CodeRepository.cpp +9 -10
@@ 339,7 339,7 @@ namespace K3 {
 							importQueue.emplace_back(
 								ImportTask{
 									{ mod->first, mostRecentVersion, file.first },
-									file.second.get(),
+									file.second,
 									{},
 									relativeImportBase
 								}

          
@@ 352,8 352,8 @@ namespace K3 {
 				auto loadedFile = mod->second.files.find(uri.path);
 				if (loadedFile != mod->second.files.end()) {
 					// already loaded
-					parent->AddImport(loadedFile->second.get());
-					InvalidateSymbolsInNode(loadedFile->second.get());
+					parent->AddImport(loadedFile->second);
+					InvalidateSymbolsInNode(loadedFile->second);
 					return { };
 				}
 			} else {

          
@@ 365,19 365,18 @@ namespace K3 {
 					}).first;
 			}
 
-			auto& newNode{ mod->second.files[uri.path] };
-			if (!newNode) {
-				newNode.reset(new RepositoryNode);
+			auto nk = NodeKey(uri.package, uri.path);
+			if (nodes.find(nk) == nodes.end()) {
+				nodes.emplace(nk, std::make_unique<RepositoryNode>());
 			}
 
-			parent->AddImport(newNode.get());
-
-			auto& importToNode{ mod->second.files[uri.path] };
+			mod->second.files[uri.path] = nodes[nk].get();
+			parent->AddImport(nodes[nk].get());
 
 			importQueue.emplace_back(
 				ImportTask{
 					std::move(uri),
-					importToNode.get(),
+					nodes[nk].get(),
 					imm,
 					relativeImportBase
 				}

          
M src/k3/Parser.h +6 -1
@@ 186,7 186,7 @@ namespace K3 {
 		};
 
 		struct RepositoryModule {
-			std::unordered_map<std::string, std::unique_ptr<RepositoryNode>> files;
+			std::unordered_map<std::string, RepositoryNode*> files;
 			std::string version;
 		};
 

          
@@ 196,12 196,17 @@ namespace K3 {
 			std::unordered_map<std::string, RepositoryModule> modules;
 			std::unordered_map<std::string, symbol_t> completeDefinitions;
 			std::unordered_map<RepositoryNode*, RepositoryNode> rollback;
+			std::unordered_map<std::string, std::unique_ptr<RepositoryNode>> nodes;
 			std::list<RepositoryNode> adhoc;
 			RepositoryNode root;
 			RepositoryNode kernel;
 			Err<symbol_t> Build(const std::string& qualifiedName);
 			std::string defaultRepo, defaultVersion;
 
+			static std::string NodeKey(std::string const& package, std::string const& file) {
+				return package + ":" + file;
+			}
+
 			struct ImportTask {
 				BufferKey uri;
 				RepositoryNode* node;