Avoid GC allocation

This commit is contained in:
ryuukk 2022-10-19 18:32:33 +02:00 committed by GitHub
parent 0e27404baf
commit 73d8ff66dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 12 deletions

View File

@ -332,18 +332,21 @@ struct ModuleCache
/** /**
* Params: * Params:
* moduleName = the name of the module being imported, in "a/b/c" style * moduleName = the name of the module being imported, in "a/b/c" style
* cb = the callback used to be called whenever a module is found,
* the absolute path is passed as parameter
* Returns: * Returns:
* The absolute path to the files that contains the module, or empty if * The number of resolved paths
* not found.
*/ */
istring[] resolveImportLocations(string moduleName) size_t resolveImportLocations(string moduleName, scope void delegate(istring) cb)
{ {
assert(moduleName !is null, "module name is null"); assert(moduleName !is null, "module name is null");
if (isRooted(moduleName)) if (isRooted(moduleName))
return [istring(moduleName)]; {
cb(istring(moduleName));
istring[] ret; return 1;
}
size_t count = 0;
string alternative; string alternative;
foreach (importPath; importPaths[]) foreach (importPath; importPaths[])
{ {
@ -354,7 +357,8 @@ struct ModuleCache
&& path.existsAnd!isFile) && path.existsAnd!isFile)
{ {
// prefer exact import names above .di/package.d files // prefer exact import names above .di/package.d files
ret ~= istring(path); cb(istring(path));
count++;
} }
// no exact matches and no .di/package.d matches either // no exact matches and no .di/package.d matches either
else if (!alternative.length) else if (!alternative.length)
@ -363,7 +367,10 @@ struct ModuleCache
string dotD = dotDi[0 .. $ - 1]; string dotD = dotDi[0 .. $ - 1];
string withoutSuffix = dotDi[0 .. $ - 3]; string withoutSuffix = dotDi[0 .. $ - 3];
if (existsAnd!isFile(dotD)) if (existsAnd!isFile(dotD))
ret ~= istring(dotD); // return early for exactly matching .d files {
cb(istring(dotD));
count++;
}
else if (existsAnd!isFile(dotDi)) else if (existsAnd!isFile(dotDi))
alternative = dotDi; alternative = dotDi;
else if (existsAnd!isDir(withoutSuffix)) else if (existsAnd!isDir(withoutSuffix))
@ -381,16 +388,19 @@ struct ModuleCache
{ {
string dotD = buildPath(path, moduleName) ~ ".d"; string dotD = buildPath(path, moduleName) ~ ".d";
if (existsAnd!isFile(dotD)) if (existsAnd!isFile(dotD))
ret ~= istring(dotD); // return early for exactly matching .d files {
cb(istring(dotD));
count++;
}
} }
} }
if (alternative.length > 0) if (alternative.length > 0)
{ {
ret ~= istring(alternative); cb(istring(alternative));
count++;
alternative = ""; alternative = "";
} }
return count;
return ret;
} }
auto getImportPaths() const auto getImportPaths() const