Closed naitoh closed 2 weeks ago
@kou
Could you add how to optimize this to the description?
Sorry. I added it to the description.
Thanks. How about using 1 PR for 1 optimization?
It seems that this has 4 optimizations:
(S_RESTLEN(p) < RSTRING_LEN(pattern))
rb_enc_get()
(restLen() < pattern.size())
currPtr()
How about using 1 PR for 1 optimization?
OK, I see.
CRuby
Why?
1. Remove duplicate
if (S_RESTLEN(p) < RSTRING_LEN(pattern)) return Qnil;
checks in!headonly
.A similar check is made within
rb_memsearch()
within!headonly
.https://github.com/ruby/ruby/blob/cf8388f76c4c2ff2f46d0d2aa2cf5186e05ff606/re.c#L251-L256
RSTRING_LEN(pattern)
S_RESTLEN(p)
This means the following :
if (RSTRING_LEN(pattern) > S_RESTLEN(p)) return -1;
Both checks are the same.
2. Removed unnecessary use of
rb_enc_get()
In
rb_strseq_index()
, the result ofrb_enc_check()
is used.https://github.com/ruby/ruby/blob/6c7209cd3788ceec01e504d99057f9d3b396be84/string.c#L4309-L4318
Benchmark
It shows String as a pattern is 1.23x faster than Regexp as a pattern.
JRuby
Why?
1. Remove duplicate
if (restLen() < pattern.size()) return context.nil;
checks in!headonly
.https://github.com/ruby/strscan/blob/d31274f41b7c1e28f23d58cf7bfea03baa818cb7/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java#L371-L373
This means the following :
if (str.size() - curr < pattern.size()) return context.nil;
A similar check is made within
StringSupport#index()
within!headonly
.https://github.com/jruby/jruby/blob/be7815ec02356a58891c8727bb448f0c6a826d96/core/src/main/java/org/jruby/util/StringSupport.java#L1706-L1720
strBL
patternBL
strBeg + curr
This means the following :
if (strBL.realSize() - (strBeg + curr) < patternBL.realSize()) return -1;
Both checks are the same.
2. Use
currPtr()
instead ofstrBeg + curr
. Because they are identical.https://github.com/ruby/strscan/blob/d31274f41b7c1e28f23d58cf7bfea03baa818cb7/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java#L267-L268
https://github.com/ruby/strscan/blob/d31274f41b7c1e28f23d58cf7bfea03baa818cb7/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java#L359-L361
Benchmark
It shows String as a pattern is 2.43x faster than Regexp as a pattern.