-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Speed up default gem loading by adding files stub line to gemspec #9385
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -839,10 +839,18 @@ def self._resort!(specs) # :nodoc: | |
| # Loads the default specifications. It should be called only once. | ||
|
|
||
| def self.load_defaults | ||
| each_spec([Gem.default_specifications_dir]) do |spec| | ||
| # #load returns nil if the spec is bad, so we just ignore | ||
| # it at this stage | ||
| Gem.register_default_spec(spec) | ||
| default_dir = Gem.default_specifications_dir | ||
| base_dir = Gem.default_dir | ||
| gems_dir = File.join(base_dir, "gems") | ||
|
|
||
| each_gemspec([default_dir]) do |path| | ||
| stub = Gem::StubSpecification.default_gemspec_stub(path, base_dir, gems_dir) | ||
| if stub.stubbed? && !stub.files.equal?(Gem::StubSpecification::StubLine::NO_FILES) | ||
| Gem.register_default_spec(stub) | ||
| else | ||
| spec = load(path) | ||
| Gem.register_default_spec(spec) if spec | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -2372,6 +2380,10 @@ def to_ruby | |
| result << "#{Gem::StubSpecification::PREFIX}#{name} #{version} #{platform} #{raw_require_paths.join("\0")}" | ||
| result << "#{Gem::StubSpecification::PREFIX}#{extensions.join "\0"}" unless | ||
| extensions.empty? | ||
| unless files.empty? | ||
| files_line = "#{Gem::StubSpecification::FILES_PREFIX}#{files.join "\0"}" | ||
| result << files_line if files_line.bytesize <= 200 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Do you think we should count the number of files instead of the bytesize of the string ? e.g a gem with few files with long paths name vs a gem with many files but short paths name
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right. I will measure them. |
||
| end | ||
|
Comment on lines
+2383
to
+2386
|
||
| result << nil | ||
| result << "Gem::Specification.new do |s|" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,14 +9,19 @@ class Gem::StubSpecification < Gem::BasicSpecification | |||||||||||||||||||||||||||||||||||||||||
| # :nodoc: | ||||||||||||||||||||||||||||||||||||||||||
| PREFIX = "# stub: " | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # :nodoc: | ||||||||||||||||||||||||||||||||||||||||||
| FILES_PREFIX = "# files: " | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # :nodoc: | ||||||||||||||||||||||||||||||||||||||||||
| OPEN_MODE = "r:UTF-8:-" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| class StubLine # :nodoc: all | ||||||||||||||||||||||||||||||||||||||||||
| attr_reader :name, :version, :platform, :require_paths, :extensions, | ||||||||||||||||||||||||||||||||||||||||||
| :full_name | ||||||||||||||||||||||||||||||||||||||||||
| attr_accessor :files | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| NO_EXTENSIONS = [].freeze | ||||||||||||||||||||||||||||||||||||||||||
| NO_FILES = [].freeze | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # These are common require paths. | ||||||||||||||||||||||||||||||||||||||||||
| REQUIRE_PATHS = { # :nodoc: | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -44,6 +49,7 @@ def initialize(data, extensions) | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @platform = Gem::Platform.new parts[2] | ||||||||||||||||||||||||||||||||||||||||||
| @extensions = extensions | ||||||||||||||||||||||||||||||||||||||||||
| @files = NO_FILES | ||||||||||||||||||||||||||||||||||||||||||
| @full_name = if platform == Gem::Platform::RUBY | ||||||||||||||||||||||||||||||||||||||||||
| "#{name}-#{version}" | ||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -122,6 +128,13 @@ def data | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| stubline.chomp! # readline(chomp: true) allocates 3x as much as .readline.chomp! | ||||||||||||||||||||||||||||||||||||||||||
| @data = StubLine.new stubline, extensions | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Read files stub line if present | ||||||||||||||||||||||||||||||||||||||||||
| filesline = extensions == StubLine::NO_EXTENSIONS ? extline : file.readline | ||||||||||||||||||||||||||||||||||||||||||
| if filesline.start_with?(FILES_PREFIX) | ||||||||||||||||||||||||||||||||||||||||||
| filesline.chomp! | ||||||||||||||||||||||||||||||||||||||||||
| @data.files = filesline.byteslice(FILES_PREFIX.bytesize..).split("\0") | ||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||
| rescue EOFError | ||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -139,6 +152,21 @@ def raw_require_paths # :nodoc: | |||||||||||||||||||||||||||||||||||||||||
| data.require_paths | ||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ## | ||||||||||||||||||||||||||||||||||||||||||
| # Files in the gem, from the files stub line if available, | ||||||||||||||||||||||||||||||||||||||||||
| # otherwise from the full specification. | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| def files | ||||||||||||||||||||||||||||||||||||||||||
| data.files | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+159
to
+160
|
||||||||||||||||||||||||||||||||||||||||||
| def files | |
| data.files | |
| # Files from the stub line only, without falling back to the full spec. | |
| # This accessor does not trigger loading the full specification. | |
| def stubbed_files # :nodoc: | |
| if defined?(@data) && @data.is_a?(StubLine) | |
| @data.files | |
| else | |
| StubLine::NO_FILES | |
| end | |
| end | |
| def files | |
| if defined?(@data) && @data.is_a?(StubLine) && | |
| @data.files.equal?(StubLine::NO_FILES) | |
| # No files were recorded in the stub, fall back to the full specification | |
| to_spec.files | |
| else | |
| data.files | |
| end |
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.
load_defaultsnow registersGem::StubSpecificationinstances into the default-spec path map (viaGem.register_default_spec(stub)). This changes whatGem.find_default_specreturns (it used to be aGem::Specification), and it also breaksGem.find_unresolved_default_spec's current logic (loaded_specs[name] != default_specwill remain true after activation because the loaded spec is a different object than the stub). Consider either adjusting those helper methods to handle stubs (e.g., treat any loaded spec with the same name as resolved), or avoid storing stubs in the map where a full spec is required for API compatibility.