Skip to content

A response and a batch id are built without formatting machinery - #630

Merged
matrixarkai merged 1 commit into
mainfrom
perf/fewer-formats
Nov 13, 2026
Merged

matrixarkai merged 1 commit into
mainfrom
perf/fewer-formats

Conversation

@matrixarkai

Copy link
Copy Markdown
Owner

What the profile said

Callgrind of the journal door put path (three replicas, 1,000 puts of 1 KiB) ranked format! as 21% of all instructions, 25 formatting calls per put, almost all of it building strings that are a name, a separator and a value:

caller instructions share
net::http::write_response 12.1M 7.65% (10 formats per response)
replica_mutation_batch_id 6.35M 4.0% (6,000 calls per 1,000 puts)
net::http::http_date 3.1M 2.0%

The change

  • write_response now renders through render_response, appending bytes and decimal digits directly.
  • http_date writes its fields directly instead of one format! with seven arguments.
  • replica_mutation_batch_id borrows the prefix and builds the id once; it used to copy the prefix into a String and format a second one, twice per replica per put (begin and end).

No stored bytes or protocol bytes change.

Measured

Callgrind, journal door, three replicas, 1,000 puts of 1 KiB, instructions per put:

before after
total 158,203 133,923 (-15.3%)
format_inner, all callers 33.8M 12.6M
write_response 15.8M 2.8M
replica_mutation_batch_id 7.65M 1.3M

Why the output is identical

Each of the three changes keeps the original implementation in its test as an oracle and compares byte for byte:

  • a_rendered_response_is_byte_for_byte_the_formatted_one: 2,240 combinations of 14 statuses (including ones that forbid a body and one with no reason phrase), four header sets (including values containing CR or LF, which are dropped), five body sizes up to 70,000 bytes, keep-alive and HEAD.
  • an_http_date_is_the_formatted_one_over_a_wide_sweep: 20,000 pseudo-random epochs through 2200 plus the leap days, the 4-digit-year boundary (253402300799999 and the next two values), and u64::MAX.
  • a_replica_batch_id_is_the_formatted_one_for_every_shape_of_page_batch_id: 77 shapes: empty, short, unicode, exactly 220 bytes (not hashed), 221 (hashed), 1,000, and replica indexes through usize::MAX.
  • decimal_digits_are_printed_as_the_formatter_prints_them for the digit writer.

Full lib suite: 685 pass. The one failure, the_published_mutation_copy_figures_are_the_ones_this_tree_produces (README 2485 vs tree 2557), fails identically on unmodified main and is untouched here.

🤖 Generated with Claude Code

Profiling the journal door put path ranked string formatting as 21% of its instructions, almost
all of it producing strings that are a name, a separator and a value.

write_response formatted every head line into a temporary String and copied it across: ten
formatting calls per response, 7.65% of instructions per put. It now appends bytes and decimal
digits directly through render_response. http_date wrote a fixed-width date through format!;
it now writes the fields. replica_mutation_batch_id copied its prefix into a String and then
formatted a second one, twice per replica per put; it now borrows the prefix and builds the id
once.

Every output is byte-identical, and each change keeps the original as an oracle in its test: the
response head over 2,240 combinations of status, headers (including values that carry CR or LF),
body size, keep-alive and HEAD; the date over a 20,000-value sweep and the four-digit year
boundary; the batch id over 77 shapes including the 220-byte hashing limit.

Measured with callgrind on the journal door at three replicas, 1,000 puts of 1 KiB:
158,203 -> 133,923 instructions per put (-15.3%). Formatting fell from 33.8M to 12.6M
instructions in total.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@matrixarkai
matrixarkai merged commit e9678b1 into main Nov 13, 2026
5 checks passed
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