Skip to content

Commit

Permalink
Cache depspec objects to optimize search speed
Browse files Browse the repository at this point in the history
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 6f87552
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/Zef/Distribution.pm6
Expand Up @@ -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. # 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 # it doesn't need to be a hash mapping as its just for matching
has @!provides-specs;
method 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) # 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 # 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` # 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 # 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) # an identity part (identity parts are separated by a *single* colon; double colon is left alone)
my $spec = Zef::Distribution::DependencySpecification.new(.key); 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) }) } method provides-spec-matcher($spec, :$strict) { self.provides-specs.first({ ?$_.spec-matcher($spec, :$strict) }) }
Expand Down

2 comments on commit 6f87552

@ugexe
Copy link
Owner Author

@ugexe ugexe commented on 6f87552 Jan 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Collaborator

@niner niner commented on 6f87552 Jan 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.