Skip to content

ZJIT: Optimize String#match? #1059

Open

Description

@tekknolagi

This makes a Regexp and does an uncached call from C to match on that object. We can move at least some of this to Ruby probably.

Activity

  1. changed the title [-]ZJIT: Optimize `String#match`[/-] [+]ZJIT: Optimize `String#match?`[/+] on Sep 22, 2026
  2. nirvdrum commented on Sep 25, 2026

    @nirvdrum

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions