Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improves performance of String#[] method.

Improves performance of String#[] method when passing Fixnum.
  • Loading branch information...
commit 83341ab4ead5a00964eb34ade0e721fd6b4478b4 1 parent 5cb7a7d
@pgrzegorczyk pgrzegorczyk authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.