[rubygems/rubygems] update cache checksums to decrease string allocations

https://github.com/rubygems/rubygems/commit/85371a961a
This commit is contained in:
Jacklyn Ma 2024-05-08 12:45:16 -04:00 коммит произвёл git
Родитель c7281e2d67
Коммит 4d0c5486a2
1 изменённых файлов: 16 добавлений и 7 удалений

Просмотреть файл

@ -55,14 +55,9 @@ module Bundler
end end
def checksums def checksums
checksums = {} lines(versions_path).each_with_object({}) do |line, checksums|
parse_version_checksum(line, checksums)
lines(versions_path).each do |line|
name, _, checksum = line.split(" ", 3)
checksums[name] = checksum
end end
checksums
end end
def dependencies(name) def dependencies(name)
@ -106,6 +101,20 @@ module Bundler
@dependency_parser.parse(line) @dependency_parser.parse(line)
end end
# This is mostly the same as `split(" ", 3)` but it avoids allocating extra objects.
# This method gets called at least once for every gem when parsing versions.
def parse_version_checksum(line, checksums)
line.freeze # allows slicing into the string to not allocate a copy of the line
name_end = line.index(" ")
checksum_start = line.index(" ", name_end + 1) + 1
checksum_end = line.size - checksum_start
# freeze name since it is used as a hash key
# pre-freezing means a frozen copy isn't created
name = line[0, name_end].freeze
checksum = line[checksum_start, checksum_end]
checksums[name] = checksum
end
def info_roots def info_roots
[ [
directory.join("info"), directory.join("info"),