Skip to content
Browse files

Cache Gem::Version instances for each version string

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...
1 parent 487195d commit c4223a2145f07fad4d9383b545620fdd380c77bf @jonleighton committed Feb 8, 2013
Showing with 10 additions and 1 deletion.
  1. +10 −1 lib/rubygems/version.rb
View
11 lib/rubygems/version.rb
@@ -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
##
@@ -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.

0 comments on commit c4223a2

Please sign in to comment.
Something went wrong with that request. Please try again.