Skip to content
This repository
Browse code

Merge pull request #1027 from pgrzegorczyk/i985

Improves performance of String#[] by 30% when passing Fixnum.
  • Loading branch information...
commit bbb18ed5fca4887f6ae58365f31d0c9da205765d 2 parents 30589b2 + 83341ab
Karol Hosiawa hosiawak authored

Showing 1 changed file with 9 additions and 0 deletions. Show diff stats Hide diff stats

  1. +9 0 kernel/common/string.rb
9 kernel/common/string.rb
@@ -254,6 +254,15 @@ def [](index, other = undefined)
254 254 end
255 255
256 256 case index
  257 + when Fixnum
  258 + # The same code as in else section.
  259 + # Copied here to improve performance because Fixnum index is
  260 + # often used to iterate through String object.
  261 + index = Rubinius::Type.coerce_to index, Fixnum, :to_int
  262 + index = @num_bytes + index if index < 0
  263 +
  264 + return if index < 0 || @num_bytes <= index
  265 + return @data[index]
257 266 when Regexp
258 267 match_data = index.search_region(self, 0, @num_bytes, true)
259 268 Regexp.last_match = match_data

0 comments on commit bbb18ed

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