Skip to content

perf(js): avoid rebuilding strings in String.contains - #1628

Merged
Lorenzobattistela merged 1 commit into
bendlang:mainfrom
sebastian-griego:fix/js-string-contains
Oct 16, 2026
Merged

Lorenzobattistela merged 1 commit into
bendlang:mainfrom
sebastian-griego:fix/js-string-contains

Conversation

@sebastian-griego

Copy link
Copy Markdown
Contributor

String.contains rebuilds the remaining input before each prefix check. On the JavaScript backend, this makes even an absent one-character search quadratic. A local search for "b" in 1,024,000 "a" characters took 7,209 ms.

Add a JS intrinsic that checks prefixes at Bend Char boundaries and advances an index through the original string. Reuse char_len with an optional offset. This preserves arbitrary U32 Char codes and prevents matches inside their packed representation. Base definitions and their proof reductions stay unchanged.

On Apple M5 Max with Bun 1.3.14, the same search took 5.45 ms with the change. Both figures are medians of three calls; they exclude input creation and compiler startup. This removes suffix reconstruction, but repeated long prefixes can still require O(N*M) comparisons.

Add two Bend regressions: character and partial-application cases with an unfolding proof, and a two-million-character absent search under the existing test gate timeout.

Validation:

  • New regressions pass through the CLI, emitted Bun/Node, and native C. The long search exceeds the existing five-second limit on unmodified main and passes with the change.
  • 15 existing String/Char fixtures pass on emitted Bun and Node.
  • 50,625 U32-token substring cases pass against an independent reference on each runtime.
  • C output is byte-identical for two probes.
  • Repository gate: 54/54. comp.ts: 64,185 tokens, below the unchanged 66,000 cap.
  • TypeScript diagnostics match unmodified main. The full cluster gates were not run.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 16, 2026
@Lorenzobattistela
Lorenzobattistela merged commit bc01c9d into bendlang:main Oct 16, 2026
1 check passed
nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Oct 19, 2026
comp.ts keeps this branch's structure, with upstream's changes ported in:
bendlang#1615's frame-slot write-back in bind_pop; bendlang#1617's named memo functions
(open_of, spine_of, uses_of), per-file literal steps keyed by string, and
lay_at memoized per type key (FL.ty_opens, keyed by ty_key); bendlang#1625's
F32.read regex; bendlang#1628's string_contains.

bendlang#1617's per-def PROBES reset is left out: here probes and the term caches
live as long as the file and units emit again across sweeps, so a reset
would reuse probe indices still held in FL.opens and FL.uses.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 20, 2026
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.

2 participants