Project

General

Profile

Actions

Patch #28940

closed

Use Regexp#match? to reduce allocations of MatchData object

Added by Pavel Rosický over 6 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Performance
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

Description

since Rails 5.1+ we can use match? on a regex class safely even on older rubies
https://github.com/rails/rails/blob/5-1-stable/activesupport/lib/active_support/core_ext/regexp.rb

but the performance benefit is visible only on ruby 2.4+
https://bugs.ruby-lang.org/issues/8110

require 'benchmark/ips'
Benchmark.ips do |x|
  x.report('match') { /test\d/.match 'test5'.freeze }
  x.report('match?') { /test\d/.match? 'test5'.freeze }
  x.compare!
end

Comparison:
match?:  4493322.3 i/s
match:    926754.9 i/s - 4.85x  slower

rake test is about 5% faster


Files

match.patch (34 KB) match.patch Pavel Rosický, 2018-06-04 01:19
match_adapters.patch (3.23 KB) match_adapters.patch Pavel Rosický, 2018-06-04 02:18
match-adapters-v2.diff (1.97 KB) match-adapters-v2.diff Go MAEDA, 2018-06-04 04:53
match-v2.diff (13.6 KB) match-v2.diff Go MAEDA, 2018-06-04 06:54
match_final.patch (27.7 KB) match_final.patch Pavel Rosický, 2018-06-17 18:14
match_final.patch (28.3 KB) match_final.patch r18001 Pavel Rosický, 2019-03-21 23:01

Related issues

Related to Redmine - Patch #28939: replace regexp with casecmpClosed

Actions
Actions

Also available in: Atom PDF