Skip to content

docs(adr): draft ADR-0028, what a string index counts (#831) - #844

Merged
hyperpolymath merged 9 commits into
mainfrom
claude/adr-string-index-units-831
Dec 1, 2026
Merged

hyperpolymath merged 9 commits into
mainfrom
claude/adr-string-index-units-831

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Depends on: #829

Refs #831. This PR does not close #831: the owner decides.

What this is

A draft decision record, docs/decisions/0028-string-index-units.adoc, plus one line in docs/NAVIGATION.adoc. It changes no semantics and no code.

It depends on #829 only because it cites docs/UNICODE-STRINGS.adoc, which that PR adds.

Usage (main 4248912, 575 tracked .affine files)

Call sites outside comment lines:

Builtin stdlib/ satellites tests/ + test/
len 51 48 33
string_length 1 0 25
string_get 11 0 0
string_sub 23 0 13
string_char_code_at 6 0 39
char_to_int 8 0 1
  • 48 of the 132 len sites are on strings (29 stdlib, 19 tests). All 48 satellite sites are array lengths, so no satellite indexes a string.
  • No tracked program applies an index builtin to a non-ASCII literal. That is why no test caught the divergence.
  • Byte-oriented stdlib code is silently wrong on Bun-ESM today. encoding::to_base64("é") is w6k= on core wasm (correct) and 6Q== on Bun; to_base64("🦜") is 8J+mnA== on wasm and P5w= on Bun.

Bun benchmark (Bun 1.4.4, median of three runs)

Workload UTF-16 today octets, cached view octets, Uint8Array strings code points, cached
scan 100k ASCII (len once, index every position) 323 µs 609 µs 533 µs 691 µs
scan 100k mixed 323 µs 620 µs 402 µs 466 µs
rebuild 1k mixed by slice and ++ 13.7 µs 76.1 µs 225 µs 30.6 µs
200 appends with len after each, 1k ASCII 3.5 µs 474 µs (264 µs with Buffer.byteLength) 290 µs 5.96 ms
first len of a 100k mixed string 52 ns 42 µs (11.6 µs with Buffer.byteLength) 37 ns 812 µs

Re-encoding on every call (no cache) makes the scan loop 190 to 1 400 times slower at 1k, so it is ruled out. The full table, the candidates and the limits of the measurement are in the record.

wasm

A boundary check costs at most about 0.6 ns per checked offset (43.2 M index operations: 51–58 ms without the check, 76–87 ms with it). Code-point indexing at size could not be measured: core wasm overwrites its own literals after about 1 KiB of allocation, a bug #829 already records.

Options and recommendation

Rated 1 (worst) to 5 (best).

Option Elegance / correctness Dependability / security
A. Octets everywhere, loud failure off a character boundary 4 4
B. Code points everywhere 3 3
C. Keep per-backend meaning, document, deprecate string len 1 2
D. String as a Uint8Array on Bun 3 3
E. Unconventional: Bytes for byte code, and no Int index into text (opaque offsets) 5 5

Recommended: A now, with E recorded as the direction of travel. A makes the backends agree, makes base64/hex/JSON escaping correct on Bun, and gives the DEED printer proof a single measure. Its migration here is small: 4 slice sites and 11 string_get sites in the stdlib, no satellite changes, no existing test expectation changes. Its cost is about 2 times on scan loops at size, and len is no longer O(1) on Bun. E is the more final answer (unit confusion becomes a type error) but is the largest migration; A's offsets are the values E's offset type would wrap.

Char as a Unicode scalar value follows from A but is a separate change.

Things for the owner to know

  • The benchmark is a JavaScript listing inside the ADR, not a tracked .mjs file, because the policy bans new hand-written JS. It measures candidate text the compiler would emit. Say if you would rather it were removed or kept elsewhere.
  • Not measured: estate code outside this repository (DEED printer, kyX); a workload that alternates between two strings, which defeats a one-entry cache; anything on the interpreter that needs a loop, because let mut i = 0; i = i + 1; stops with Expected mutable reference there (the known gap in docs/CAPABILITY-MATRIX.adoc).
  • The len string/array split was done by reading each site, not by the type checker.

Checks

  • just guard passes (doc-truthing, deed gates, soundness ledger, capability anchors).
  • asciidoctor --failure-level WARN renders the record with no warnings.
  • No code changes, so the test suites were not re-run on this branch.

Generated by Claude Code

A draft decision record for the owner. It changes no semantics.

- Usage of len, string_get, string_sub, string_char_code_at and
  char_to_int across the repository, the stdlib and the satellites,
  with the len sites classified by argument type.
- A Bun benchmark of candidate lowerings (UTF-16 units, octets with and
  without a cached UTF-8 view, code points, a Uint8Array representation)
  and a wasm measurement of the boundary check.
- Five options with migration cost, Char interaction, wasm cost and
  proof-friendliness, rated, with a recommendation.

Refs #831

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J4QvDhJog6g9kprphCAadr
@gitnotebooks

gitnotebooks Bot commented Dec 1, 2026

Copy link
Copy Markdown

@hyperpolymath
hyperpolymath enabled auto-merge (squash) December 1, 2026 18:36
@hyperpolymath
hyperpolymath enabled auto-merge (squash) December 1, 2026 18:37
@hyperpolymath
hyperpolymath enabled auto-merge (squash) December 1, 2026 19:17
Repository owner deleted a comment from chatgpt-codex-connector Bot Dec 1, 2026
Repository owner deleted a comment from chatgpt-codex-connector Bot Dec 1, 2026
Repository owner deleted a comment from coderabbitai Bot Dec 1, 2026
Repository owner deleted a comment from coderabbitai Bot Dec 1, 2026
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 48 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 0fe3f49d-5777-4adc-aea9-410550e459c9
Commits

Reviewing files that changed from the base of the PR and between 0af0ab8 and c8c4a44.

Files selected for processing: 2

  • docs/NAVIGATION.adoc
  • docs/decisions/0028-string-index-units.adoc
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@hyperpolymath
hyperpolymath merged commit 464f7c6 into main Dec 1, 2026
9 checks passed
@hyperpolymath
hyperpolymath deleted the claude/adr-string-index-units-831 branch December 1, 2026 19:44
@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

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.

semantics: string indices mean octets on interp/wasm but UTF-16 units on Bun-ESM (owner decision)

2 participants