From 9885c972a6bfa6f856e505cdd90d9b71fdbdadaf Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 14 Aug 2017 14:48:37 -0700 Subject: Add an explicit `Name` type Fixes #23 Up to this point, the compiler has used the ordinary `String` type to represent declaration names, which means a bunch of lookup structures throughout the compiler were string-to-whatever maps, which can reduce efficiency. It also means that things like the `Token` type end up carying a `String` by value and paying for things like reference-counting. This change adds a `Name` type that is used to represent names of variables, types, macros, etc. Names are cached and unique'd globally for a session, and the string-to-name mapping gets done during lexing. From that point on, most mapping is from pointers, which should make all the various table lookups faster. More importantly (possibly), this brings us one step closer to being able to pool-allocate the AST nodes. --- source/slang/slang.cpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'source/slang/slang.cpp') diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 2bacb28ca..f6e9bd70b 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -20,9 +20,13 @@ namespace Slang { Session::Session() { + // Initialize name pool + getNamePool()->setRootNamePool(getRootNamePool()); + // Initialize the lookup table of syntax classes: - #define SYNTAX_CLASS(NAME, BASE) mapNameToSyntaxClass.Add(#NAME, getClass()); + #define SYNTAX_CLASS(NAME, BASE) \ + mapNameToSyntaxClass.Add(getNamePool()->getName(#NAME), getClass()); #include "object-meta-begin.h" #include "syntax-base-defs.h" @@ -110,6 +114,8 @@ struct IncludeHandlerImpl : IncludeHandler CompileRequest::CompileRequest(Session* session) : mSession(session) { + getNamePool()->setRootNamePool(session->getRootNamePool()); + setSourceManager(&sourceManagerStorage); sourceManager->initialize(session->getBuiltinSourceManager()); @@ -378,7 +384,7 @@ int CompileRequest::addEntryPoint( { RefPtr entryPoint = new EntryPointRequest(); entryPoint->compileRequest = this; - entryPoint->name = name; + entryPoint->name = getNamePool()->getName(name); entryPoint->profile = entryPointProfile; entryPoint->translationUnitIndex = translationUnitIndex; @@ -391,7 +397,7 @@ int CompileRequest::addEntryPoint( } RefPtr CompileRequest::loadModule( - String const& name, + Name* name, String const& path, String const& source, SourceLoc const&) @@ -462,15 +468,20 @@ void CompileRequest::handlePoundImport( // as the "name" when registering things, but this saves // us the trouble of trying to special-case things when // checking an `import` down the road. - mapNameToLoadedModules.Add(path, moduleDecl); + // + // Ideally we'd construct a suitable name by effectively + // running the name->path logic in reverse (e.g., replacing + // `-` with `_` and `/` with `.`). + Name* name = getNamePool()->getName(path); + mapNameToLoadedModules.Add(name, moduleDecl); mapPathToLoadedModule.Add(path, moduleDecl); loadedModulesList.Add(moduleDecl); } RefPtr CompileRequest::findOrImportModule( - String const& name, - SourceLoc const& loc) + Name* name, + SourceLoc const& loc) { // Have we already loaded a module matching this name? // If so, return it. @@ -485,7 +496,7 @@ RefPtr CompileRequest::findOrImportModule( // For example, `foo_bar` becomes `foo-bar.slang`. StringBuilder sb; - for (auto c : name) + for (auto c : getText(name)) { if (c == '_') c = '-'; @@ -541,8 +552,8 @@ RefPtr CompileRequest::findOrImportModule( RefPtr findOrImportModule( CompileRequest* request, - String const& name, - SourceLoc const& loc) + Name* name, + SourceLoc const& loc) { return request->findOrImportModule(name, loc); } -- cgit v1.2.3