Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gmalette
Copy link

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 ~110ms.

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea.
  • Follow the instructions in CONTRIBUTING. Most importantly, ensure the tests and linter pass by running bundle exec rake ci in the gem subdirectory.
  • Update code documentation if necessary.

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.
@gmalette
Copy link
Author

I don't understand what the CI failure is

Copy link
Member

@dazuma dazuma left a 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-")
Copy link
Member

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")
Copy link
Member

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".)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants