Skip to content

Perf improvements for String.endWith(Regex) - #1884

Merged
bioball merged 5 commits into
apple:mainfrom
bioball:string-ends-with-fixes
Oct 5, 2026
Merged

bioball merged 5 commits into
apple:mainfrom
bioball:string-ends-with-fixes

Conversation

@bioball

@bioball bioball commented Sep 30, 2026 •

Copy link
Copy Markdown
Member
  • Compute a derived regex expression with \z anchor
  • Ensure derived regex can't be messed up with comments (in extended mode) or quote start

Follow up to #1880

* Compute a derived regex expression with `$` anchor
* Keep around current implementation if multiline flag is set
* Ensure derived regex can't be messed up with comments (in extended mode)
@bioball
bioball force-pushed the string-ends-with-fixes branch from fd17d5d to d6607b8 Compare September 30, 2026 17:24

@HT154 HT154 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple problems with the current implementation:

// unclosed \Q, which is valid
res = "x)".endsWith(Regex(#"x\Q)"#))

errors with

java.util.regex.PatternSyntaxException%3A%20Unclosed%20group%20near%20index%2010%0A%28%3F%3Ax%5CQ%29%29%24

–– Pkl Error ––
Unclosed group near index 10
(?:x\Q))$

Here a bunch of cases that are failing:

amends  "pkl:test"

facts {
  ["comment mode"] {
    "zz".endsWith(Regex("(?x)zz"))
    "zz".endsWith(Regex("(?x) zz # comment"))
  }
  ["unicode"] {
    "a\né".endsWith(Regex(#"(?x)\w"#))
    "a\nÉ".endsWith(Regex("(?x)(?i)é"))
    "é".endsWith(Regex(#"\w"#)) // this works
  }
  ["trailing new lines"] {
    !"foo\n".endsWith(Regex("foo"))
    !"foo\r\n".endsWith(Regex("foo"))
    !"foo\n".endsWith("foo") // this works
  }
  ["overlap multiline"] {
    "a\nb".endsWith(Regex(#"(?m)a(?:\nb)??"#))
  }
}

All of these pass on current main. They should be added to the tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catches, all fixed!

I switched to using the \z anchor, which also means that we can get rid of the special-cased (?m) flag handling, which is great.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline flags are now applying to the whole pattern, so flags at the middle/end of a pattern affect the whole thing:

amends "pkl:test"

facts {
  ["global flags"] {
    !"Ab".endsWith(Regex("a(?i)b"))
    !"x\n".endsWith(Regex("x.(?s)"))
    "a b".endsWith(Regex("a b(?x)"))
  }
}

They were all passing before.

String self,
VmRegex regex,
@Cached("regex") VmRegex cachedRegex,
@Cached("computePattern(cachedRegex)") Pattern pattern) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: truffle only caches up to 3 values by default, so if you have code like function hasSuffix(s, r: Regex) = s.endsWith(r) you can easily run out of cache space and recompile all the time.
I think it'd be better to cache the pattern in the VmRegex itself. Which is how it works in Java and most languages (compile once, use everywhere).

@bioball bioball Oct 2, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now sure what you mean by Java? Java's Pattern.compile compiles every time.

Our existing Regex() constructor has the same limitation; i.e. this code can possibly re-compile all the time once it gets past three instances of the cached version:

function makeRegex(r: String) = Regex(r)

It's a tradeoff between memory and CPU here; we can adjust the specialization limit if we need to. But I think the 99% case here is that these regexes will be declared literally, e.g.

foo.endsWith(Regex("bar|baz"))

So, I'm not too concerned about the uncached-variant.

@stackoverflow stackoverflow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bioball
bioball merged commit ba25f48 into apple:main Oct 5, 2026
25 checks passed
@bioball
bioball deleted the string-ends-with-fixes branch October 5, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants