Skip to content

Auto-optimize regexes with a tiny number of alternations into switches #343

@Dr-Emann

Description

@Dr-Emann

Maybe less impactful with #341, but it should be possible to look at the regex syntax Hir, and simplify simple regexes like http.path ~ "^/abc/123/?$" into http.path == "/abc/123" || http.path == "/abc/123/". Local testing seems to suggest that with any more than 2 alternations, it's faster to leave a regex.

This would require:

  • The AST already parses into a regex. Maybe it shouldn't, maybe we should have our own "HIR" or something, otherwise we'd pay the cost of compiling the regex, then throw that work away to convert it back to string equality.
  • Similarly, we need to handle captures specially: The caller could observe the difference between a regex and string equality comparisons, because Match.captures[0] will be populated for the regex but not for the string equality. An intermediate representation could preserve that behavior by knowing the original match was by regex.

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