Skip to content

The unreserved set is decided when this compiles, not when it first runs - #4

Merged
Asmod4n merged 1 commit into
masterfrom
claude/encode-table-at-compile-time
Aug 29, 2026
Merged

Asmod4n merged 1 commit into
masterfrom
claude/encode-table-at-compile-time

Conversation

@Asmod4n

@Asmod4n Asmod4n commented Aug 29, 2026

Copy link
Copy Markdown
Owner

encode_table() filled a 256-byte array on its first call, behind a ready flag. Two mrb_states reaching that on different threads is a data race - benign in effect, since both write the same values, but a race nonetheless - and it costs a branch on every call for something that cannot change.

It is a constexpr std::array now, built by an immediately-invoked lambda. The loops run in the compiler; what reaches the binary is 256 bytes of .rodata. No flag, no branch, nothing to race for.

The static_asserts are not decoration - they are only checkable if the table really is finished before main(), which is the entire claim. They also pin the set itself: 'a', 'Z', '0' and '~' in; space, '/' and 0xff out; '+' out, so it encodes to %2B and never to a space (URI.encode is RFC 3986, not form-urlencoded, and test/uri.rb asserts exactly that); and 66 entries total, which is 26 + 26 + 10 + 4.

Stating the rule also beats stating the result. A written-out table is 256 hand-counted numbers in which somebody has to notice that 45, 46, 95 and 126 are '-', '.', '_' and '~' before the table says anything - the shape this file had before, when it was C and could not do better.

Tests pass under -fsanitize=address,undefined: 2498 total, 0 failures.

encode_table() filled a 256-byte array on its first call, behind a
`ready` flag. Two mrb_states reaching that on different threads is a data
race - benign in effect, since both write the same values, but a race
nonetheless - and it costs a branch on every call for something that
cannot change.

It is a constexpr std::array now, built by an immediately-invoked lambda.
The loops run in the compiler; what reaches the binary is 256 bytes of
.rodata. No flag, no branch, nothing to race for.

The static_asserts are not decoration - they are only checkable if the
table really is finished before main(), which is the entire claim. They
also pin the set itself: 'a', 'Z', '0' and '~' in; space, '/' and 0xff
out; '+' out, so it encodes to %2B and never to a space (URI.encode is
RFC 3986, not form-urlencoded, and test/uri.rb asserts exactly that);
and 66 entries total, which is 26 + 26 + 10 + 4.

Stating the rule also beats stating the result. A written-out table is
256 hand-counted numbers in which somebody has to notice that 45, 46, 95
and 126 are '-', '.', '_' and '~' before the table says anything - the
shape this file had before, when it was C and could not do better.

Tests pass under -fsanitize=address,undefined: 2498 total, 0 failures.

Co-Authored-By: Claude <noreply@anthropic.com>
@Asmod4n
Asmod4n merged commit 1454914 into master Aug 29, 2026
2 checks passed
@Asmod4n
Asmod4n deleted the claude/encode-table-at-compile-time branch August 29, 2026 17:26
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