-
Notifications
You must be signed in to change notification settings - Fork 555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(perf): Make all gems load faster #28104
base: main
Are you sure you want to change the base?
Conversation
Gem.find_latest_files's performance scales with the amount of files contained across all gems of a project. This can quickly get out of hand, especially on projects with many gems, or large gems. Gem.loaded_spec is already populated by bundler, and we can iterate over only the name of the gems to detect those matching the google-cloud-* pattern. We can then guess the name of the file we expect to be present. On my machine, this makes `auto_load_files` go from ~100ms to <1ms, and requiring `bigtable` goes from ~220ms to ~110.
I don't understand what the CI failure is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for this! I agree this would help. I left a couple of comments.
# Ruby 2.0 does not have Gem.find_latest_files | ||
Gem.find_files "google-cloud-*.rb" | ||
Gem.loaded_specs.values.filter_map do |spec| | ||
next unless spec.name.start_with?("google-cloud-") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CI failures are rubocop warnings. To conform with this library's style, omit the method-argument parentheses on this line and the following two lines.
Gem.find_files "google-cloud-*.rb" | ||
Gem.loaded_specs.values.filter_map do |spec| | ||
next unless spec.name.start_with?("google-cloud-") | ||
path = File.join(spec.lib_dirs_glob, spec.name + ".rb") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is, spec.lib_dirs_glob
can return glob syntax (i.e. including commas etc) in cases where there are multiple source paths. That would cause this to fail. Maybe we can iterate over spec.require_paths
instead? (Note that require_paths
is relative to spec.full_gem_path
, and can also be nil
, where we should default to "lib"
.)
Gem.find_latest_files
's performance scales with the amount of files contained across all gems of a project. This can quickly get out of hand, especially on projects with many gems, or large gems.Gem.loaded_spec
is already populated by bundler, and we can iterate over only the name of the gems to detect those matching thegoogle-cloud-*
pattern. We can then guess the name of the file we expect to be present.On my machine, this makes
auto_load_files
go from ~100ms to <1ms, and requiringbigtable
goes from ~220ms to ~110ms.bundle exec rake ci
in the gem subdirectory.