Shopify / constant_resolver

Resolve a partially qualified Ruby constant reference to the fully qualified name and the path of the file defining it.
MIT License
26 stars 10 forks source link

make faster #5

Closed exterm closed 4 years ago

exterm commented 4 years ago

ENHANCE

...by reducing the number of object allocations in the loop over all files. Scanning for all files is the only slow part of the gem; the rest is just hash lookup.

exterm commented 4 years ago

Did very primitive benchmarks on shopify/shopify:

require_relative "config/application.rb"
require "constant_resolver"

config = Rails.application.config
paths = config.eager_load_paths + config.autoload_paths + config.autoload_once_paths
paths = paths.map { |p| Pathname.new(p).relative_path_from(Rails.root).to_s }.sort.uniq

time = Time.now

# the call to `resolve` triggers the full path scan
10.times { ConstantResolver.new(root_path: ".", load_paths: paths).resolve("ApiClient") }

duration = Time.now - time

puts duration.seconds

With constant resolver master: 8.15s 6.40s 6.22s 6.02s 7.86s 6.40s avg 6.84, std dev 0.92

With these modifications: 4.60s 4.09s 4.19s 4.21s 5.40s 4.15s avg 4.44, std dev 0.50

I'd say this speeds it up by roughly 30%. Good enough for now.