Skip to content

Commit

Permalink
reduce calling of splitTypeName in makeTypeDeclaration
Browse files Browse the repository at this point in the history
compose 4 searches in one. It reduce splitTypeName calls which parse string every time and made an heap allocations.
Also it allow to avoid an extra linear search for module with required name
  • Loading branch information
Einzich committed Feb 9, 2024
1 parent 32b7038 commit 7cd52a9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 29 deletions.
6 changes: 6 additions & 0 deletions include/daScript/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,11 +1185,17 @@ namespace das
bool addModule ( Module * module );
void foreach ( const callable<bool (Module * module)> & func, const string & name ) const;
void foreach_in_order ( const callable<bool (Module * module)> & func, Module * thisM ) const;

void findWithCallback ( const string & name, Module * inWhichModule, const callable<void (Module * pm, const string &name, Module * inWhichModule)> & func ) const;
void findAlias ( vector<TypeDeclPtr> & ptr, Module * pm, const string & aliasName, Module * inWhichModule ) const;
vector<TypeDeclPtr> findAlias ( const string & name, Module * inWhichModule ) const;
void findAnnotation ( vector<AnnotationPtr> & ptr, Module * pm, const string & annotationName, Module * inWhichModule ) const;
vector<AnnotationPtr> findAnnotation ( const string & name, Module * inWhichModule ) const;
vector<AnnotationPtr> findStaticAnnotation ( const string & name ) const;
vector<TypeInfoMacroPtr> findTypeInfoMacro ( const string & name, Module * inWhichModule ) const;
void findEnum ( vector<EnumerationPtr> & ptr, Module * pm, const string & enumName, Module * inWhichModule ) const;
vector<EnumerationPtr> findEnum ( const string & name, Module * inWhichModule ) const;
void findStructure ( vector<StructurePtr> & ptr, Module * pm, const string & funcName, Module * inWhichModule ) const;
vector<StructurePtr> findStructure ( const string & name, Module * inWhichModule ) const;
Module * findModule ( const string & name ) const;
TypeDeclPtr makeStructureType ( const string & name ) const;
Expand Down
16 changes: 12 additions & 4 deletions src/ast/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2740,10 +2740,18 @@ namespace das {
}

TypeDecl * Program::makeTypeDeclaration(const LineInfo &at, const string &name) {
auto structs = library.findStructure(name,thisModule.get());
auto handles = library.findAnnotation(name,thisModule.get());
auto enums = library.findEnum(name,thisModule.get());
auto aliases = library.findAlias(name,thisModule.get());

das::vector<das::StructurePtr> structs;
das::vector<das::AnnotationPtr> handles;
das::vector<das::EnumerationPtr> enums;
das::vector<das::TypeDeclPtr> aliases;
library.findWithCallback(name, thisModule.get(), [&](Module * pm, const string &name, Module * inWhichModule) {
library.findStructure(structs, pm, name, inWhichModule);
library.findAnnotation(handles, pm, name, inWhichModule);
library.findEnum(enums, pm, name, inWhichModule);
library.findAlias(aliases, pm, name, inWhichModule);
});

if ( ((structs.size()!=0)+(handles.size()!=0)+(enums.size()!=0)+(aliases.size()!=0)) > 1 ) {
string candidates = describeCandidates(structs);
candidates += describeCandidates(handles, false);
Expand Down
77 changes: 52 additions & 25 deletions src/ast/ast_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,38 +759,57 @@ namespace das {
return it!=modules.end() ? *it : nullptr;
}

void ModuleLibrary::findWithCallback ( const string & name, Module * inWhichModule, const callable<void (Module * pm, const string &name, Module * inWhichModule)> & func ) const {
string moduleName, funcName;
splitTypeName(name, moduleName, funcName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) ) {
func(pm, funcName, inWhichModule);
}
return true;
}, moduleName);
}

void ModuleLibrary::findAlias ( vector<TypeDeclPtr> & ptr, Module * pm, const string & aliasName, Module * inWhichModule ) const {
if ( auto pp = pm->findAlias(aliasName) ) {
if ( pp->isEnumT() ) {
if ( !pp->enumType->isPrivate || pp->enumType->module==inWhichModule ) {
ptr.push_back(das::move(pp));
}
} else if ( pp->baseType==Type::tStructure ) {
if ( !pp->structType->privateStructure || pp->structType->module==inWhichModule ) {
ptr.push_back(das::move(pp));
}
} else {
ptr.push_back(das::move(pp));
}
}
}

vector<TypeDeclPtr> ModuleLibrary::findAlias ( const string & name, Module * inWhichModule ) const {
vector<TypeDeclPtr> ptr;
string moduleName, aliasName;
splitTypeName(name, moduleName, aliasName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) )
if ( auto pp = pm->findAlias(aliasName) ) {
if ( pp->isEnumT() ) {
if ( !pp->enumType->isPrivate || pp->enumType->module==inWhichModule ) {
ptr.push_back(pp);
}
} else if ( pp->baseType==Type::tStructure ) {
if ( !pp->structType->privateStructure || pp->structType->module==inWhichModule ) {
ptr.push_back(pp);
}
} else {
ptr.push_back(pp);
}
}
findAlias(ptr, pm, aliasName, inWhichModule);
return true;
}, moduleName);
return ptr;
}

void ModuleLibrary::findAnnotation ( vector<AnnotationPtr> & ptr, Module * pm, const string & annotationName, Module * ) const {
if ( auto pp = pm->findAnnotation(annotationName) )
ptr.push_back(das::move(pp));
}

vector<AnnotationPtr> ModuleLibrary::findAnnotation ( const string & name, Module * inWhichModule ) const {
vector<AnnotationPtr> ptr;
string moduleName, annName;
splitTypeName(name, moduleName, annName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) )
if ( auto pp = pm->findAnnotation(annName) )
ptr.push_back(pp);
findAnnotation(ptr, pm, annName, inWhichModule);
return true;
}, moduleName);
return ptr;
Expand All @@ -809,34 +828,42 @@ namespace das {
return ptr;
}

void ModuleLibrary::findStructure ( vector<StructurePtr> & ptr, Module * pm, const string & structName, Module * inWhichModule ) const {
if ( auto pp = pm->findStructure(structName) ) {
if ( !pp->privateStructure || pp->module==inWhichModule ) {
ptr.push_back(das::move(pp));
}
}
}

vector<StructurePtr> ModuleLibrary::findStructure ( const string & name, Module * inWhichModule ) const {
vector<StructurePtr> ptr;
string moduleName, funcName;
splitTypeName(name, moduleName, funcName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) ) {
if ( auto pp = pm->findStructure(funcName) ) {
if ( !pp->privateStructure || pp->module==inWhichModule ) {
ptr.push_back(pp);
}
}
findStructure(ptr, pm, funcName, inWhichModule);
}
return true;
}, moduleName);
return ptr;
}

void ModuleLibrary::findEnum ( vector<EnumerationPtr> & ptr, Module * pm, const string & enumName, Module * inWhichModule ) const {
if ( auto pp = pm->findEnum(enumName) ) {
if ( !pp->isPrivate || pp->module==inWhichModule ) {
ptr.push_back(das::move(pp));
}
}
}

vector<EnumerationPtr> ModuleLibrary::findEnum ( const string & name, Module * inWhichModule ) const {
vector<EnumerationPtr> ptr;
string moduleName, enumName;
splitTypeName(name, moduleName, enumName);
foreach([&](Module * pm) -> bool {
if ( !inWhichModule || inWhichModule->isVisibleDirectly(pm) ) {
if ( auto pp = pm->findEnum(enumName) ) {
if ( !pp->isPrivate || pp->module==inWhichModule ) {
ptr.push_back(pp);
}
}
findEnum(ptr, pm, enumName, inWhichModule);
}
return true;
}, moduleName);
Expand Down

0 comments on commit 7cd52a9

Please sign in to comment.