perf: build IdxInsert keys from live registers instead of re-seeking (#663) - #668
Merged
Conversation
…663) INSERT and UPDATE codegen already hold the new row's column values in col_regs/rowid_reg before writing it; re-seeking the table cursor and re-reading those same values via Column/Rowid to build the secondary index keys was a wasted btree seek + page touch per indexed write. emit_index_key_ops_from_regs builds the key via Opcode::Copy from the existing registers instead. IdxDelete paths (removing a different, already-on-disk row's entries) are untouched. spend: matched estimate (medium)
Also backfills the 0.18.9 changelog entry missing for #660 (SorterInsert register-source read), which was merged before 0.18.9's release commit but never got its own changelog line.
iheitlager
force-pushed
the
fix/663-index-key-register-reuse
branch
from
August 30, 2026 10:56
aa7585f to
188d799
Compare
2 tasks
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.
Summary
col_regs/rowid_regbefore writing the row; the secondary-index-key builder (emit_index_key_ops) insteadSeekRowid'd back onto the just-written row and re-read those same values viaOpcode::Column/Opcode::Rowid— an unnecessary btree seek + page touch per row on every INSERT/UPDATE against an indexed table.emit_index_key_ops_from_regsinsrc/codegen/index_maintenance.rs, which builds the same key layout viaOpcode::Copystraight from the existing value registers, and switches theIdxInsertcall sites ininsert.rs/update.rsto it, dropping theirSeekRowid.IdxDeletecall sites (removing a different, already-on-disk row's stale entries — UPDATE's old-row cleanup, and theINSERT OR REPLACEconflict paths) are untouched — they have no pre-existing register run for that row and must stay cursor-based.Refs:
emit_index_key_opsdoc comment's stated rationale ("no register-copy opcode in the frozen V2 set") was factually wrong —Opcode::Copyexists and is used in 15+ other codegen sites.Test plan
cargo test --test corpus index_maintenance— all 12 scenarios pass (oraclePRAGMA integrity_check+ indexed-lookup verification)cargo test --test corpus --test vdbe_write_opcodes --test vdbe_cursor_positioning --test vdbe_cursor_sorter— 380 + opcode-shape tests passcargo test --test codegen_insert --test codegen_update --test parity— passcargo clippy --all-targets— cleanInsert, the program now emits twoCopys straight intoIdxInsertwith noSeekRowid/Columnre-readcargo bench --bench crud -- insert_batch_10ran (high variance from a concurrent bench in another worktree during this run, but no regression)spend: matched estimate (medium)
Closes #663