Skip to content

Commit e73a154

Browse files
authored
[JRuby] Optimize scan(): Use strBL.getBegin() + curr instead of currPtr() (#109)
- before: #106 ## Why? 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.33x faster than Regexp as a pattern. ``` $ benchmark-driver benchmark/check_until.yaml Warming up -------------------------------------- regexp 7.421M i/s - 7.378M times in 0.994235s (134.75ns/i) regexp_var 7.302M i/s - 7.307M times in 1.000706s (136.95ns/i) string 12.715M i/s - 12.707M times in 0.999388s (78.65ns/i) string_var 13.575M i/s - 13.533M times in 0.996914s (73.66ns/i) Calculating ------------------------------------- regexp 8.287M i/s - 22.263M times in 2.686415s (120.67ns/i) regexp_var 10.180M i/s - 21.905M times in 2.151779s (98.23ns/i) string 20.148M i/s - 38.144M times in 1.893226s (49.63ns/i) string_var 23.695M i/s - 40.726M times in 1.718753s (42.20ns/i) Comparison: string_var: 23694846.7 i/s string: 20147598.6 i/s - 1.18x slower regexp_var: 10180018.3 i/s - 2.33x slower regexp: 8287384.8 i/s - 2.86x slower ```
1 parent ff2d7af commit e73a154

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,20 +263,18 @@ private IRubyObject extractBegLen(Ruby runtime, int beg, int len) {
263263
private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succptr, boolean getstr, boolean headonly) {
264264
final Ruby runtime = context.runtime;
265265
check(context);
266-
267-
ByteList strBL = str.getByteList();
268-
int strBeg = strBL.getBegin();
269-
270266
clearMatched();
271267

272268
if (restLen() < 0) {
273269
return context.nil;
274270
}
275271

272+
ByteList strBL = str.getByteList();
273+
int currPtr = strBL.getBegin() + curr;
274+
276275
if (regex instanceof RubyRegexp) {
277276
pattern = ((RubyRegexp) regex).preparePattern(str);
278277

279-
int currPtr = currPtr();
280278
int range = currPtr + restLen();
281279

282280
Matcher matcher = pattern.matcher(strBL.getUnsafeBytes(), matchTarget(), range);
@@ -311,12 +309,12 @@ private IRubyObject scan(ThreadContext context, IRubyObject regex, boolean succp
311309
int patternSize = patternBL.realSize();
312310

313311
if (headonly) {
314-
if (ByteList.memcmp(strBL.unsafeBytes(), strBeg + curr, patternBL.unsafeBytes(), patternBL.begin(), patternSize) != 0) {
312+
if (ByteList.memcmp(strBL.unsafeBytes(), currPtr, patternBL.unsafeBytes(), patternBL.begin(), patternSize) != 0) {
315313
return context.nil;
316314
}
317315
setRegisters(patternSize);
318316
} else {
319-
int pos = StringSupport.index(strBL, patternBL, strBeg + curr, patternEnc);
317+
int pos = StringSupport.index(strBL, patternBL, currPtr, patternEnc);
320318
if (pos == -1) {
321319
return context.nil;
322320
}

0 commit comments

Comments
 (0)