Skip to content

perf(sqlite-native): avoid cloning cached read chunks#4634

Draft
NathanFlurry wants to merge 1 commit into04-12-perf_sqlite-native_gate_kv_operation_labelsfrom
04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks
Draft

perf(sqlite-native): avoid cloning cached read chunks#4634
NathanFlurry wants to merge 1 commit into04-12-perf_sqlite-native_gate_kv_operation_labelsfrom
04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 13, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude bot commented Apr 13, 2026

Review: perf(sqlite-native): avoid cloning cached read chunks\n\nThe optimization is correct and well-targeted.\n\n### What the change does\n\n1. Eliminates buffered.clone() and cached.clone() for dirty-buffer and read-cache hits by storing &[u8] slices instead of owned Vec<u8> copies.\n2. Defers the read-cache population until after the chunk-copy loop, decoupling it from the KV response handling.\n3. Cleans up comment organization and restructures the lookup chain.\n\n### Correctness\n\nThe code is correct. Two invariants make it safe:\n\n- Disjoint key sets: buffered_chunks stores slices from read_cache for chunks that were cache hits. The subsequent read_cache.insert loop only writes newly fetched chunks (cache misses). No key overlap means no existing Vec is dropped while buffered_chunks slices are still live.\n- Vec heap stability: HashMap<Vec<u8>, Vec<u8>> stores values inline as Vec<u8> structs. On HashMap resize the struct is moved but the heap buffer it points to is not. So as_slice() survives a HashMap grow.\n\nThese invariants hold but are non-obvious. A brief comment near the buffered_chunks declaration explaining why the slices remain valid through the later read_cache.as_mut() call would help future readers.\n\n### Minor: double BTreeMap lookup for dirty-buffer chunks\n\nThe new code does contains_key in the prep loop then get in the retrieval loop: two O(log n) lookups. This could be collapsed by storing the borrow directly:\n\nrust\nif state.batch_mode {\n if let Some(buffered) = state.dirty_buffer.get(&(chunk_idx as u32)) {\n buffered_chunks.insert(chunk_idx, buffered.as_slice()); // no clone\n continue;\n }\n}\n\n\nThis eliminates the if state.batch_mode branch in the retrieval loop too. Not a blocker.\n\n### Nit: shadowed variable name\n\nread_start is declared twice in kv_io_read (line 376 as Instant::now(), line 442 as a chunk byte offset). Predates this PR but worth a follow-up.\n\n---\n\nOverall: Sound optimization with cleaner structure. Main ask is a comment near buffered_chunks documenting the slice-lifetime safety invariant.

@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_gate_kv_operation_labels branch from 5482e7d to bde5c19 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch 2 times, most recently from ff117f9 to e25c1b6 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_gate_kv_operation_labels branch from 13c73bd to 768d9da Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-perf_sqlite-native_avoid_cloning_cached_read_chunks branch from e25c1b6 to 532364f Compare April 13, 2026 07:03
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