Repository navigation
C: str_eq tests the first byte before memcmp, inline in its caller - #90
Merged
Merged
Conversation
__lang_str_eq was left out of line in a large program, so a test of a name against a literal was a call, and a second call -- memcmp, through the linker's stub -- whenever the two lengths agreed. A dispatcher that tests a name against a chain of literals pays both for each link: a sample of mere-ruby calling a builtin method had 44% of its time in the two. The function is now always_inline and compares the first byte before memcmp. A test that fails on the length is two loads and a compare in the caller; one that fails on the first byte never calls memcmp. An empty string is answered by its length, before any byte is read. test/parity/str_eq_edges.mere has the edges on all four backends.
284km
commented
Nov 24, 2026
284km
left a comment
Collaborator
Author
There was a problem hiding this comment.
Reviewed 2ff7b93. No Critical / Major / Minor findings.
- The new test
la == __lang_str_size(b) && (la == 0 || (a[0] == b[0] && memcmp(...)))is equivalent to the old one: the lengths are equal beforea[0]is read, an empty string never reachesa[0], and forla >= 1a mismatch in the first byte is exactly a memcmp mismatch at offset 0. Strings with an interior NUL are still compared over their full header length (the parity test covers that and the prefix cases). always_inlineis on both the forward declaration and the definition, so they agree; nothing inlib/takes the address of__lang_str_eq(all uses are direct calls), so no out-of-line copy is needed and GCC/clang will not hit "inlining failed" at a call site.- Only the C backend changes;
test/parity/str_eq_edges.merekeeps the three backends' answers tied together. git merge-treeagainst current main: clean. No mere-pr-review-watch inline comments.- Local results in the description:
dune runtest3,305 passed / 0 failed, the new parity test MATCH on c / llvm / wasm, and mere-ruby (main 79f287ae) built with this Mere: corpus 846/846, VM 842 + 4 fallback / 0 different, vm_check 245/245. Startup −14 %, so_matrix 16.4 s → 13.9 s, binary +4 %.
Meets the merge criteria; squash-merging without waiting for CI.
284km
added a commit
that referenced
this pull request
Nov 24, 2026
#90 added test/parity/str_eq_edges.mere and left the README's count at 255; first_run_check compares the two.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
str_eqon the C backend compares the first byte before calling memcmp, and is inlined in its caller. This follows v0.1.705, which made it compare the lengths first.Motivation
__lang_str_eqwasstatic inline, but in a large program the C compiler left it out of line. A test of a name against a literal was then a call. Whenever the two lengths agreed, it made a second call, to memcmp through the linker's stub:"at"against"==","<<",">=".A dispatcher that tests a name against a chain of literals pays both for each link. A sample of mere-ruby calling a builtin method (
a.at(0)) had 44% of its time in the two: 26% in__lang_str_eqitself and 18% in memcmp and its stub. That was about 400 calls of__lang_str_eqper Ruby call, about 75 of them reaching memcmp.Change
lib/codegen_c.ml, the runtime's__lang_str_eq:__attribute__((always_inline)), on the definition and the forward declaration;a[0] == b[0]beforememcmp(a, b, la).A test that fails on the length is two loads and a compare in the caller. One that fails on the first byte never calls memcmp.
An empty string's first byte is not read. The expression is
la == lb && (la == 0 || (a[0] == b[0] && memcmp(...) == 0)). The lengths are compared first, andla == 0answers beforea[0].Backends
C only. The interpreter, LLVM, Wasm and RISC-V are unchanged. The LLVM backend's
__lang_str_eqis its own definition.Effect, on mere-ruby (its largest user; instructions, same source, this compiler against 0c083af)
a.at(0).at(1)The binary grows 4%, from 14.18 MB to 14.74 MB: 3,448 call sites are now inline.
Tests
test/parity/str_eq_edges.mereis new. It covers:scripts/parity.sh test/parity/str_eq_edges.mere: c MATCH, llvm MATCH, wasm MATCH.Checks run
dune buildanddune runtest: exit 0; the suite's summary is 3,305 passed, 0 failed.tools/bracket_depth_check.shok.Not run: the RISC-V hosted-execution and QEMU checks. This change does not reach that backend.
🤖 Generated with Claude Code