Skip to content

perf: build IdxInsert keys from live registers instead of re-seeking (#663) - #668

Merged
iheitlager merged 2 commits into
mainfrom
fix/663-index-key-register-reuse
Aug 30, 2026
Merged

perf: build IdxInsert keys from live registers instead of re-seeking (#663)#668
iheitlager merged 2 commits into
mainfrom
fix/663-index-key-register-reuse

Conversation

@iheitlager

Copy link
Copy Markdown
Member

Summary

  • INSERT and UPDATE codegen already hold every new/updated column value in col_regs/rowid_reg before writing the row; the secondary-index-key builder (emit_index_key_ops) instead SeekRowid'd back onto the just-written row and re-read those same values via Opcode::Column/Opcode::Rowid — an unnecessary btree seek + page touch per row on every INSERT/UPDATE against an indexed table.
  • Adds emit_index_key_ops_from_regs in src/codegen/index_maintenance.rs, which builds the same key layout via Opcode::Copy straight from the existing value registers, and switches the IdxInsert call sites in insert.rs/update.rs to it, dropping their SeekRowid.
  • IdxDelete call sites (removing a different, already-on-disk row's stale entries — UPDATE's old-row cleanup, and the INSERT OR REPLACE conflict paths) are untouched — they have no pre-existing register run for that row and must stay cursor-based.

Refs: emit_index_key_ops doc comment's stated rationale ("no register-copy opcode in the frozen V2 set") was factually wrong — Opcode::Copy exists and is used in 15+ other codegen sites.

Test plan

  • cargo test --test corpus index_maintenance — all 12 scenarios pass (oracle PRAGMA 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 pass
  • cargo test --test codegen_insert --test codegen_update --test parity — pass
  • cargo clippy --all-targets — clean
  • Manual opcode dump confirms the fix: post-Insert, the program now emits two Copys straight into IdxInsert with no SeekRowid/Column re-read
  • cargo bench --bench crud -- insert_batch_10 ran (high variance from a concurrent bench in another worktree during this run, but no regression)

spend: matched estimate (medium)

Closes #663

…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
iheitlager force-pushed the fix/663-index-key-register-reuse branch from aa7585f to 188d799 Compare August 30, 2026 10:56
@iheitlager
iheitlager merged commit 003c2bd into main Aug 30, 2026
6 checks passed
@iheitlager
iheitlager deleted the fix/663-index-key-register-reuse branch August 30, 2026 11:00
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.

perf: INSERT/UPDATE codegen re-seeks table cursor to rebuild index keys instead of reusing registers

1 participant