Skip to content

fbcode/internal_repo_rocksdb/repo/table/merging_iterator.cc (#14915)#14915

Open
pdillinger wants to merge 1 commit into
facebook:mainfrom
pdillinger:export-D110484400
Open

fbcode/internal_repo_rocksdb/repo/table/merging_iterator.cc (#14915)#14915
pdillinger wants to merge 1 commit into
facebook:mainfrom
pdillinger:export-D110484400

Conversation

@pdillinger

@pdillinger pdillinger commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary: Pull Request resolved: #14915

Differential Revision: D110484400

@meta-cla meta-cla Bot added the CLA Signed label Jul 2, 2026
@meta-codesync

meta-codesync Bot commented Jul 2, 2026

Copy link
Copy Markdown

@pdillinger has exported this pull request. If you are a Meta employee, you can view the originating Diff in D110484400.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

✅ clang-tidy: No findings on changed lines

Completed in 69.9s.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codex Code Review - OBSOLETE

Superseded by a newer AI review. Expand to see the original review.

🟡 Codex Code Review

Auto-triggered after CI passed — reviewing commit fc7a967


Codex review failed before producing findings.

WARNING: proceeding, even though we could not create PATH aliases: Refusing to create helper binaries under temporary dir "/tmp" (codex_home: AbsolutePathBuf("/tmp/codex-home"))
error: the argument '--base <BRANCH>' cannot be used with '[PROMPT]'

Usage: codex exec review --commit <SHA> --base <BRANCH> --title <TITLE> --model <MODEL> --config <key=value> --dangerously-bypass-approvals-and-sandbox --output-last-message <FILE> [PROMPT]

For more information, try '--help'.

ℹ️ About this response

Generated by Codex CLI.
Review methodology: claude_md/code_review.md

Limitations:

  • Codex may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /codex-review [context] — Request a code review
  • /codex-query <question> — Ask about the PR or codebase

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

Claude Code Review - OBSOLETE

Superseded by a newer AI review. Expand to see the original review.

✅ Claude Code Review

Auto-triggered after CI passed — reviewing commit fc7a967


Summary

Small, well-motivated change that replaces std::vector<HeapItem> with autovector<HeapItem> in MergingIterator, eliminating a heap allocation for the common case of <= 8 child iterators. Pointer stability (required by the heap which stores HeapItem*) is preserved because autovector does not relocate stack-resident elements on resize() or emplace_back(). The TODO comment it resolves mentioned "a larger reserved size," but the default kSize=8 is reasonable for typical LSM configurations.

High-severity findings (0):

No high-severity findings.

Full review (click to expand)

Findings

🟡 MEDIUM

M1. TODO asked for "larger reserved size" but default kSize=8 is used -- merging_iterator.cc:614
  • Issue: The removed TODO comment says "We could use an autovector with a larger reserved size." The change uses autovector<HeapItem> with the default kSize=8. The TODO's intent may have been to use a larger stack reservation (e.g., autovector<HeapItem, 16>) to cover more levels without heap fallback.
  • Root cause: Ambiguous TODO wording. 8 is likely sufficient for most configurations (typical LSM has ~7 levels + 1-2 memtables), but workloads with many L0 files or column families using MergeIteratorBuilder::AddIterator could exceed 8.
  • Suggested fix: Either use a slightly larger kSize (e.g., 12 or 16) to better match the TODO's intent, or confirm that 8 is the deliberate choice. Each HeapItem is roughly 80-100 bytes, so kSize=8 adds ~640-800 bytes inline and kSize=16 would add ~1.3-1.6 KB -- both acceptable since MergingIterator is heap-allocated.

🟢 LOW / NIT

L1. pinned_heap_item_ is still std::vector<HeapItem> -- merging_iterator.cc:625
  • Issue: The sibling member pinned_heap_item_ remains std::vector<HeapItem> with the same cardinality (one per level). The same optimization may apply.
  • Suggested fix: Consider converting in a follow-up if beneficial.

Cross-Component Analysis

Pointer stability: The heap stores HeapItem* via &children_[level]. autovector does not relocate already-constructed elements on resize() or emplace_back(), and children_ is not resized during iteration. All pointers remain stable. Safe.

Move safety: MergingIterator is heap-allocated and never moved, so autovector's inline buffer address stability is maintained.

Positive Observations

  • Directly addresses an existing TODO.
  • Eliminates a heap allocation in the common case, improving locality.
  • Drop-in replacement -- autovector supports all operations used (resize, emplace_back, operator[], size).
  • Clean, minimal diff.

ℹ️ About this response

Generated by Claude Code.
Review methodology: claude_md/code_review.md

Limitations:

  • Claude may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /claude-review [context] — Request a code review
  • /claude-query <question> — Ask about the PR or codebase

…#14915)

Summary: Pull Request resolved: facebook#14915

Differential Revision: D110484400
@meta-codesync meta-codesync Bot changed the title fbcode/internal_repo_rocksdb/repo/table/merging_iterator.cc fbcode/internal_repo_rocksdb/repo/table/merging_iterator.cc (#14915) Jul 6, 2026
@pdillinger pdillinger force-pushed the export-D110484400 branch from fc7a967 to 084c594 Compare July 6, 2026 14:13
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

🟡 Codex Code Review

Auto-triggered after CI passed — reviewing commit 084c594


Codex review failed before producing findings.

WARNING: proceeding, even though we could not create PATH aliases: Refusing to create helper binaries under temporary dir "/tmp" (codex_home: AbsolutePathBuf("/tmp/codex-home"))
error: the argument '--base <BRANCH>' cannot be used with '[PROMPT]'

Usage: codex exec review --commit <SHA> --base <BRANCH> --title <TITLE> --model <MODEL> --config <key=value> --dangerously-bypass-approvals-and-sandbox --output-last-message <FILE> [PROMPT]

For more information, try '--help'.

ℹ️ About this response

Generated by Codex CLI.
Review methodology: claude_md/code_review.md

Limitations:

  • Codex may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /codex-review [context] — Request a code review
  • /codex-query <question> — Ask about the PR or codebase

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

✅ Claude Code Review

Auto-triggered after CI passed — reviewing commit 084c594


Summary

Clean, low-risk optimization that replaces std::vector<HeapItem> with autovector<HeapItem> for children_ in MergingIterator, fulfilling an existing TODO. The change avoids heap allocation for the common case of <=8 sub-iterators (levels). Pointer stability analysis confirms correctness: heap pointers into children_ are only taken after all resizing is complete.

High-severity findings (0):
No high-severity findings.

Full review (click to expand)

Findings

🟡 MEDIUM

M1. Stack size increase for arena-allocated MergingIterator -- table/merging_iterator.cc:614
  • Issue: autovector<HeapItem> with default kSize=8 adds an inline buffer of 8 * sizeof(HeapItem) (~640-800 bytes) to MergingIterator's footprint. MergingIterator is typically arena-allocated (line 1667), so this doesn't increase actual stack usage, but it does increase the arena allocation size.
  • Root cause: The TODO comment (now removed) mentioned "a larger reserved size," but the default kSize=8 is used. For most LSM configurations (L0 files + L1-L6 = typically 7-10 levels), 8 is a reasonable default.
  • Suggested fix: This is acceptable as-is. The arena allocation increase is modest and the performance benefit (avoiding heap allocation + better locality for small iterator counts) outweighs it. If the team wants to optimize further, a smaller kSize (e.g., 4) could be considered, or a larger one if >8 children are common.
M2. pinned_heap_item_ remains std::vector -- table/merging_iterator.cc:625
  • Issue: pinned_heap_item_ has the same type (std::vector<HeapItem>) and similar access patterns as children_. If the optimization rationale applies to children_, it likely applies to pinned_heap_item_ as well. The sizes are always equal (assert(range_tombstone_iters_.size() == children_.size()) at line 111).
  • Suggested fix: Consider converting pinned_heap_item_ to autovector<HeapItem> in a follow-up change for consistency, if benchmarks support it.

🟢 LOW / NIT

L1. Removed TODO was slightly different from what was implemented -- table/merging_iterator.cc:613
  • Issue: The removed TODO said "We could use an autovector with a larger reserved size." The implementation uses the default kSize=8 rather than a "larger" reserved size. This is likely fine since 8 is already larger than the typical number of levels.
  • Suggested fix: No action needed -- the default is sensible.

Cross-Component Analysis

Context Does code execute? Assumptions hold? Action needed?
WritePreparedTxnDB YES YES -- children_ usage unchanged safe
ReadOnly DB YES YES -- same container semantics safe
User-defined timestamps YES YES -- no impact on key comparison safe
Arena allocation (common) YES YES -- inline buffer is in arena memory safe
Non-arena allocation (NewMergingIterator) YES YES -- inline buffer on heap via new safe
>8 sub-iterators YES YES -- overflow to std::vector safe
AddIterator after construction YES YES -- emplace_back supported, heap rebuilt on next Seek safe

Pointer stability verification:

  • Heap stores HeapItem* pointers into children_
  • Pointers are established during Seek*() calls, after all resize()/emplace_back() operations
  • AddIterator() (the only post-construction mutator) sets current_ = nullptr, forcing heap reconstruction via Seek*()
  • autovector stack-buffer elements have stable addresses (embedded in the object)
  • autovector overflow-vector elements follow std::vector stability rules (stable between reallocations)
  • No concurrent modification of children_ occurs

Positive Observations

  • Fulfills an existing TODO with an appropriate solution
  • The autovector default kSize=8 is well-suited for typical RocksDB configurations
  • The change is minimal and surgical -- only the type and include are modified
  • autovector is a well-tested RocksDB utility already used extensively in the codebase (including within this same file at line 833 for prefetched_target)

ℹ️ About this response

Generated by Claude Code.
Review methodology: claude_md/code_review.md

Limitations:

  • Claude may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /claude-review [context] — Request a code review
  • /claude-query <question> — Ask about the PR or codebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant