Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimize Splitter in the matching delimiter regexp case

Avoid calling MatchData#captures in a tight loop unless there are in
fact any captures.

Also includes a few micro-optimisations, like using Array#concat instead
of Array#push with a splat, and favouring Array#at(-1) over Array#last
when we know the array is not empty.

Overall, this shows an almost three-fold improvement in the relevant
benchmark.
  • Loading branch information...
commit 01593ae85d13f3068b73e2f38be08649a0e49f70 1 parent a0118d4
@leocassarani leocassarani authored
Showing with 7 additions and 3 deletions.
  1. +7 −3 kernel/common/splitter.rb
View
10 kernel/common/splitter.rb
@@ -76,7 +76,11 @@ def self.split(string, pattern, limit)
unless collapsed && (match.full.at(0) == last_match_end)
ret << match.pre_match_from(last_match_end)
- ret.push(*match.captures.compact)
+
+ # length > 1 means there are captures
+ if match.length > 1
+ ret.concat(match.captures.compact)
+ end
end
if collapsed
@@ -98,8 +102,8 @@ def self.split(string, pattern, limit)
end
# Trim from end
- if !ret.empty? and (limit.equal?(undefined) || limit == 0)
- while s = ret.last and s.empty?
+ if limit.equal?(undefined) || limit == 0
+ while s = ret.at(-1) and s.empty?
ret.pop
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.