Permalink
Browse files

Merge pull request #1027 from pgrzegorczyk/i985

Improves performance of String#[] by 30% when passing Fixnum.
  • Loading branch information...
2 parents 30589b2 + 83341ab commit bbb18ed5fca4887f6ae58365f31d0c9da205765d @hosiawak hosiawak committed Jun 19, 2011
Showing with 9 additions and 0 deletions.
  1. +9 −0 kernel/common/string.rb
View
9 kernel/common/string.rb
@@ -254,6 +254,15 @@ def [](index, other = undefined)
end
case index
+ when Fixnum
+ # The same code as in else section.
+ # Copied here to improve performance because Fixnum index is
+ # often used to iterate through String object.
+ index = Rubinius::Type.coerce_to index, Fixnum, :to_int
+ index = @num_bytes + index if index < 0
+
+ return if index < 0 || @num_bytes <= index
+ return @data[index]
when Regexp
match_data = index.search_region(self, 0, @num_bytes, true)
Regexp.last_match = match_data

0 comments on commit bbb18ed

Please sign in to comment.