You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TruffleRuby already does a good chunk of this is in Ruby. It's done in a few different steps:
String#match? coerces the supplied pattern into a Regexp and then makes the Regexp#match? call. It makes use of two primitives: one for a faster type check and one for the type coercion.
Regexp#match? delegates to an internal helper class called Truffle::RegexpOperations. I'm not sure why it's done this way. I think some of it is a holdover from before primitives were added, but it might for better call site optimization, too. The helper class is so the runtime support code doesn't leak new methods into Regexp.
Truffle::RegexpOperations#match? does the bulk of the work. There's a Symbol check on the str arg, but that's only because this is ultimately shared with Symbol#match?as well. It takes care of bounding the position argument and remapping it if the value is negative. That leads to a trivial check that is known to always be false. Then it maps the character position to byte position because that's what's needed by the regex engine. And, naturally, then there's a primitive to call into the regex engine.
I think a fair chunk of that could be used almost directly.
Activity
[-]ZJIT: Optimize `String#match`[/-][+]ZJIT: Optimize `String#match?`[/+]nirvdrum commentedon Sep 25, 2026
TruffleRuby already does a good chunk of this is in Ruby. It's done in a few different steps:
Regexpand then makes theRegexp#match?call. It makes use of two primitives: one for a faster type check and one for the type coercion.Truffle::RegexpOperations. I'm not sure why it's done this way. I think some of it is a holdover from before primitives were added, but it might for better call site optimization, too. The helper class is so the runtime support code doesn't leak new methods intoRegexp.Symbolcheck on thestrarg, but that's only because this is ultimately shared withSymbol#match?as well. It takes care of bounding the position argument and remapping it if the value is negative. That leads to a trivial check that is known to always befalse. Then it maps the character position to byte position because that's what's needed by the regex engine. And, naturally, then there's a primitive to call into the regex engine.I think a fair chunk of that could be used almost directly.