Skip to content

Commit

Permalink
Avoid linear lookup and name allocations in Project.getDependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed May 3, 2024
1 parent 46f905c commit c620806
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions source/dub/project.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Project {
PackageManager m_packageManager;
Package m_rootPackage;
Package[] m_dependencies;
Package[string] m_dependenciesByName;
Package[][Package] m_dependees;
SelectedVersions m_selections;
string[] m_missingDependencies;
Expand Down Expand Up @@ -227,9 +228,8 @@ class Project {
*/
inout(Package) getDependency(string name, bool is_optional)
inout {
foreach(dp; m_dependencies)
if( dp.name == name )
return dp;
if (auto pp = name in m_dependenciesByName)
return *pp;
if (!is_optional) throw new Exception("Unknown dependency: "~name);
else return null;
}
Expand Down Expand Up @@ -519,8 +519,10 @@ class Project {
void reinit()
{
m_dependencies = null;
m_dependenciesByName = null;
m_missingDependencies = [];
collectDependenciesRec(m_rootPackage);
foreach (p; m_dependencies) m_dependenciesByName[p.name] = p;
m_missingDependencies.sort();
}

Expand Down

0 comments on commit c620806

Please sign in to comment.