Skip to content

A string write that does not return the old value does not read it - #3160

Open
matrixarkai wants to merge 1 commit into
mainfrom
perf/a-string-write-that-returns-nothing-reads-nothing
Open

matrixarkai wants to merge 1 commit into
mainfrom
perf/a-string-write-that-returns-nothing-reads-nothing

Conversation

@matrixarkai

@matrixarkai matrixarkai commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

SET, SET NX, SET XX and SET KEEPTTL reach the shard as one conditional write. It read the key's whole old value into a Vec before deciding anything and dropped it unread unless the caller asked for it back: a copy of every byte of the value about to be replaced, and a block-store read when the old frame was not cached. An unconditional write that returns nothing now reads nothing, and NX/XX ask whether the key is there from its row in memory (no block read). A write that asks for the old value (GETSET, SET .. GET) still reads it.

No stored byte, log record or wire byte changes; the write that lands writes what it wrote. One behaviour differs, and only for a damaged store: a key whose row exists but whose block cannot be read now counts as present for NX/XX.

Measured (counting allocator, release, alloc-probe, one execute per operation, 40 operations over 8 existing keys; before = the replaced read selected by a temporary switch in a scratch build of the same tree, after = this change):

value SET over existing, allocs/op bytes/op before bytes/op after
64 B 71.0 -> 48.1 4,883 3,435
4 KiB 71.9 -> 49.1 29,186 15,632
64 KiB 71.9 -> 49.1 399,476 200,163
1 MiB 73.9 -> 51.2 8,552,970 5,381,351 (-37%)

SET .. KEEPTTL is the same figure. The floor is the plain StringSet command (1 MiB: 5,380,356 bytes, 46.4 allocs): a SET over an existing key now costs what a SET always cost. In CI, over a 256 KiB value: SET 790,760 bytes (the write that asks for the old value, the control, 1,594,882), XX 790,760, a refused NX 262,151 where it read the old value before (1,049,807 in the earlier push of this branch).

Plant: #3169 restores the old read under the same guard; the guard must fail with the old counts (CI run linked there). Guards, pinned by exact name in rust-ci.yml: a_conditional_write_answers_what_it_always_answered (every condition x present/absent x return-old x KEEPTTL, against known answers, the deadline, and a reload), the_value_a_write_leaves_is_the_value_a_read_returns (nine widths either side of the store's size classes, over each other), and the probe a_string_overwrite_does_not_copy_the_old_value, whose control is the write that asks for the old value.

Not measured: instructions; wall time; a cold-cache NX/XX (the row answer reads no block at all, so there is nothing left to time). A first variant read the block through the cache's shared bytes for NX/XX and was measured worse on a cold frame (a copy into an Arc), which is why presence is the row. Net about 25 lines of code, 300 of test.

🤖 Generated with Claude Code

@matrixarkai
matrixarkai force-pushed the perf/a-string-write-that-returns-nothing-reads-nothing branch from 3b66d15 to 915de33 Compare November 15, 2026 23:46
@matrixarkai
matrixarkai force-pushed the perf/a-string-write-that-returns-nothing-reads-nothing branch from 6a777f2 to 93e9a41 Compare November 16, 2026 02:53
@matrixarkai
matrixarkai marked this pull request as ready for review November 16, 2026 02:56
SET, SET NX, SET XX and SET KEEPTTL reach the shard as one conditional write, which read the
key's whole old value into a Vec before deciding anything and dropped it unread unless the
caller asked for it back. An unconditional write that returns nothing now reads nothing, and NX
and XX ask whether the key is there from its row, with no block read.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@matrixarkai
matrixarkai force-pushed the perf/a-string-write-that-returns-nothing-reads-nothing branch from 93e9a41 to 79591d1 Compare November 16, 2026 04:29
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