Skip to content

Commit

Permalink
dub: Cache PackageManager across dub{Package,Dependant} targets
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Feb 22, 2024
1 parent cc9ec99 commit 32d02e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
26 changes: 25 additions & 1 deletion payload/reggae/dub/interop/dublib.d
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,33 @@ package struct Dub {
// module that don't do that on purpose for speed reasons.
auto fullDub(in string projectPath) @trusted {
import dub.dub: DubClass = Dub;
import dub.packagemanager: PackageManager;
import dub.internal.vibecompat.inet.path: NativePath;

auto dub = new DubClass(projectPath);
// Cache the PackageManager.
// A reggaefile.d with lots of dub{Package,Dependant} targets benefits from
// this, also depending on the size of the dub packages cache.
static class DubWithCachedPackageManager : DubClass {
this(string rootPath) {
super(rootPath);
}

override PackageManager makePackageManager() const {
static PackageManager cachedPM = null;
if (!cachedPM) {
// The PackageManager wants a path to a local directory, for an
// implicit `<local>/.dub/packages` repo. The base
// implementation uses the dub root project directory; use the
// reggae project directory as our local root.
import reggae.config: options;
auto localRoot = NativePath(options.projectPath);
cachedPM = new PackageManager(localRoot, m_dirs.userPackages, m_dirs.systemSettings, false);
}
return cachedPM;
}
}

auto dub = new DubWithCachedPackageManager(projectPath);
dub.packageManager.getOrLoadPackage(NativePath(projectPath));
dub.loadPackage();
dub.project.validate();
Expand Down
12 changes: 8 additions & 4 deletions tests/it/runtime/dependencies.d
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,14 @@ unittest {
]
);

// but do write configuration if dub cfg has changed
auto lines = runIt("-d myvar=foo");
srcLine.should.not.be in lines;
cfgLine.should.be in lines;
// we cache dub's PackageManager per-thread; use a new thread to make sure the changed .sdl is reloaded
import core.thread: Thread;
new Thread(() {
// but do write configuration if dub cfg has changed
auto lines = runIt("-d myvar=foo");
srcLine.should.not.be in lines;
cfgLine.should.be in lines;
}).start().join();
}
}
}

0 comments on commit 32d02e4

Please sign in to comment.