Skip to content

Commit

Permalink
update cache checksums to decrease string allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklynhma authored and martinemde committed May 24, 2024
1 parent 07ba11f commit 85371a9
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions bundler/lib/bundler/compact_index_client/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,9 @@ def versions_etag_path
end

def checksums
checksums = {}

lines(versions_path).each do |line|
name, _, checksum = line.split(" ", 3)
checksums[name] = checksum
lines(versions_path).each_with_object({}) do |line, checksums|
parse_version_checksum(line, checksums)
end

checksums
end

def dependencies(name)
Expand Down Expand Up @@ -106,6 +101,20 @@ def parse_gem(line)
@dependency_parser.parse(line)
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
[
directory.join("info"),
Expand Down

0 comments on commit 85371a9

Please sign in to comment.