Skip to content

C: str_eq tests the first byte before memcmp, inline in its caller - #90

Merged
284km merged 1 commit into
mainfrom
str-eq-first-byte
Nov 24, 2026
Merged

284km merged 1 commit into
mainfrom
str-eq-first-byte

Conversation

@284km

@284km 284km commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

str_eq on 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_eq was static 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_eq itself and 18% in memcmp and its stub. That was about 400 calls of __lang_str_eq per 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] before memcmp(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, and la == 0 answers before a[0].

Backends

C only. The interpreter, LLVM, Wasm and RISC-V are unchanged. The LLVM backend's __lang_str_eq is its own definition.

Effect, on mere-ruby (its largest user; instructions, same source, this compiler against 0c083af)

before after
startup (an empty script) 1,054 M 905 M (−14%)
300k a.at(0).at(1) 8,475 M 7,349 M (−13%)
300k calls of a Ruby method 9,351 M 9,019 M (−3.5%)
ruby's benchmark/so_matrix.rb 221.6 G, 16.4 s 201.7 G, 13.9 s

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.mere is new. It covers:

  • the empty string against itself and against one byte;
  • two strings of one length that differ in the first byte, in the last, and nowhere;
  • a string against itself;
  • a string built at run time against a literal;
  • a NUL inside;
  • a prefix;
  • a chain of same-length literals.

scripts/parity.sh test/parity/str_eq_edges.mere: c MATCH, llvm MATCH, wasm MATCH.

Checks run

  • dune build and dune runtest: exit 0; the suite's summary is 3,305 passed, 0 failed.
  • mere-ruby built with this compiler (its main 79f287ae):
    • corpus 846/846 match ruby 4.0.9;
    • with MERE_RUBY_VM=1, 842 on the VM, 4 fall back, 0 different;
    • vm_check 245/245;
    • tools/bracket_depth_check.sh ok.

Not run: the RISC-V hosted-execution and QEMU checks. This change does not reach that backend.

🤖 Generated with Claude Code

__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 284km left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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 before a[0] is read, an empty string never reaches a[0], and for la >= 1 a 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_inline is on both the forward declaration and the definition, so they agree; nothing in lib/ 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.mere keeps the three backends' answers tied together.
  • git merge-tree against current main: clean. No mere-pr-review-watch inline comments.
  • Local results in the description: dune runtest 3,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
284km merged commit 9dec2ff into main Nov 24, 2026
12 of 16 checks passed
@284km
284km deleted the str-eq-first-byte branch November 24, 2026 05:25
@284km 284km mentioned this pull request Nov 24, 2026
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.
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.

1 participant