Permalink
Browse files

Cache depspec objects to optimize search speed

Provides a significant speed boost when resolving dependency
chains by avoiding the overhead of creating DependencySpec objects
for each query/distribution combo.
  • Loading branch information...
ugexe committed Jan 28, 2019
1 parent 37eba40 commit 6f875525a00e66a1a1bbd9ebb938554ba24753c5
Showing with 5 additions and 3 deletions.
  1. +5 −3 lib/Zef/Distribution.pm6
@@ -89,16 +89,18 @@ class Zef::Distribution does Distribution is Zef::Distribution::DependencySpecif

# make locating a module that is part of a distribution (ex. URI::Escape of URI) easier.
# it doesn't need to be a hash mapping as its just for matching
has @!provides-specs;
method provides-specs {
cache gather for %(self.hash<provides>) {
@!provides-specs := +@!provides-specs ?? @!provides-specs !! @(self.hash<provides>).map({
# if $spec.name is not defined then .key (the module name of the current provides)
# is not a valid module name (according to Zef::Identity grammar anyway). I ran into
# this problem with `NativeCall::Errno` where one of the provides was: `X:NativeCall::Errorno`
# The single colon cannot just be fixed to DWIM because that could just as easily denote
# an identity part (identity parts are separated by a *single* colon; double colon is left alone)
my $spec = Zef::Distribution::DependencySpecification.new(.key);
take $spec if defined($spec.name);
}
next unless defined($spec.name);
$spec;
}).grep(*.defined).Slip;
}

method provides-spec-matcher($spec, :$strict) { self.provides-specs.first({ ?$_.spec-matcher($spec, :$strict) }) }

2 comments on commit 6f87552

@ugexe

This comment has been minimized.

Copy link
Owner Author

ugexe replied Jan 29, 2019

Measuring dependency resolution speed with the command:
time zef install Cro::OpenAPI::RoutesFromDefinition --debug --dry --/test

Before this commit: 59s
After this commit: 11s

@niner

This comment has been minimized.

Copy link
Collaborator

niner replied Jan 29, 2019

Wow, what a nice catch!
Feels a bit like the cache sub's name is a bit misleading...

Please sign in to comment.