From 682f81dec76b8ac1b5c2ec30af016139d0caa120 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Thu, 7 Dec 2023 10:49:05 -0800 Subject: [PATCH] Reduce allocations (#37) --- lib/compact_index/dependency.rb | 2 +- lib/compact_index/gem_version.rb | 8 +++++--- lib/compact_index/versions_file.rb | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/compact_index/dependency.rb b/lib/compact_index/dependency.rb index 896ca0c..49e9b7b 100644 --- a/lib/compact_index/dependency.rb +++ b/lib/compact_index/dependency.rb @@ -4,7 +4,7 @@ module CompactIndex Dependency = Struct.new(:gem, :version, :platform, :checksum) do # rubocop:disable Lint/StructNewOverride def version_and_platform if platform.nil? || platform == "ruby" - version.dup + version else "#{version}-#{platform}" end diff --git a/lib/compact_index/gem_version.rb b/lib/compact_index/gem_version.rb index 32e46f4..1ef8b02 100644 --- a/lib/compact_index/gem_version.rb +++ b/lib/compact_index/gem_version.rb @@ -5,7 +5,7 @@ module CompactIndex :dependencies, :ruby_version, :rubygems_version) do def number_and_platform if platform.nil? || platform == "ruby" - number.dup + number else "#{number}-#{platform}" end @@ -22,7 +22,7 @@ def <=>(other) end def to_line - line = number_and_platform.dup << " " << deps_line << "|checksum:#{checksum}" + line = "#{number_and_platform} #{deps_line}|checksum:#{checksum}" line << ",ruby:#{ruby_version_line}" if ruby_version && ruby_version != ">= 0" line << ",rubygems:#{rubygems_version_line}" if rubygems_version && rubygems_version != ">= 0" line @@ -47,7 +47,9 @@ def deps_line end def join_multiple(requirements) - requirements.split(", ").sort.join("&") + requirements = requirements.split(", ") + requirements.sort! + requirements.join("&") end end end diff --git a/lib/compact_index/versions_file.rb b/lib/compact_index/versions_file.rb index 085595e..9571980 100644 --- a/lib/compact_index/versions_file.rb +++ b/lib/compact_index/versions_file.rb @@ -36,7 +36,7 @@ def create(gems, timestamp = Time.now.iso8601) private def gem_lines(gems) - gems.reduce("".dup) do |lines, gem| + gems.reduce(+"") do |lines, gem| version_numbers = gem.versions.map(&:number_and_platform).join(",") lines << gem.name << " " << version_numbers <<