Skip to content

Commit

Permalink
Cache Gem::Version instances for each version string
Browse files Browse the repository at this point in the history
This prevents having numerous instances of the same value object. In
particular, the use of String#scan in Version#segments is a hot spot. We
could perform the caching there but it seems reasonable to cache the
entire object.
  • Loading branch information
jonleighton committed Feb 8, 2013
1 parent 487195d commit c4223a2
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rubygems/version.rb
Expand Up @@ -153,7 +153,10 @@ class Gem::Version
##
# A string representation of this Version.

attr_reader :version
def version
@version.dup
end

alias to_s version

##
Expand Down Expand Up @@ -183,6 +186,12 @@ def self.create input
end
end

@@all = {}

def self.new version
@@all[version] ||= super
end

##
# Constructs a Version from the +version+ string. A version string is a
# series of digits or ASCII letters separated by dots.
Expand Down

0 comments on commit c4223a2

Please sign in to comment.