Skip to content

iterv2: implement NextPrefix in mergingIterV2#6005

Merged
RaduBerinde merged 2 commits intocockroachdb:masterfrom
RaduBerinde:iterv2-next-prefix
Apr 30, 2026
Merged

iterv2: implement NextPrefix in mergingIterV2#6005
RaduBerinde merged 2 commits intocockroachdb:masterfrom
RaduBerinde:iterv2-next-prefix

Conversation

@RaduBerinde
Copy link
Copy Markdown
Member

iterv2: implement NextPrefix in levelIterV2

Previously, levelIterV2.NextPrefix panicked with "not implemented".
This commit implements it by delegating to the per-file
InterleavingIter, falling back to a level-level SeekGE when the file
iterator exhausts.

The level_iter_v2 randomized test no longer suppresses
TestOpNextPrefix.

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com

iterv2: implement NextPrefix in mergingIterV2

Previously, mergingIterV2.NextPrefix fell back to a full SeekGE on
the merging iter. This commit implements it directly, driving the
per-level NextPrefix fast path where applicable.

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com

Previously, levelIterV2.NextPrefix panicked with "not implemented".
This commit implements it by delegating to the per-file
InterleavingIter, falling back to a level-level SeekGE when the file
iterator exhausts.

The level_iter_v2 randomized test no longer suppresses
TestOpNextPrefix.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@RaduBerinde RaduBerinde requested a review from sumeerbhola April 29, 2026 01:40
@RaduBerinde RaduBerinde requested a review from a team as a code owner April 29, 2026 01:40
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Collaborator

@sumeerbhola sumeerbhola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sumeerbhola reviewed 3 files and all commit messages, and made 2 comments.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on RaduBerinde).


merging_iter_v2.go line 815 at r2 (raw file):

		default:
			// Boundary key, exhausted, or already >= succKey: SeekGE with
			// TrySeekUsingNext.

Do we not bother distinguishing between these cases because for the exhausted or already >= succKey case we are relying on the SeekGE not doing much?
It still seems worthwhile avoiding any calls: we've desired NextPrefix to be almost as fast as Next.


merging_iter_v2.go line 821 at r2 (raw file):

			return nil
		}
	}

I am trying to think through the approach difference between what is happening here and in mergingIter. At a very high-level I think mergingIter.NextPrefix manages to do two things:

  • Incrementally maintain the heap, versus doing an initHeap.
  • Observe that the heap top is >= succKey to avoid positioning any other levels.

In the common case we may be stepping through multiple prefixes in L6, and the slab boundary is far away. Could we use the fact that NextPrefix is called when not at a slab boundary to NextPrefix the heap top, and if it is still at the top and not at the slab boundary we have nothing else to do. Even if the top level changed, but it is not a slab boundary, isn't it sufficient to check that it is >= succKey. The mergingIter does something more involved with levelsPositioned but it may not be necessary in this slab world.

@github-actions
Copy link
Copy Markdown

Potential Bug(s) Detected

The three-stage Claude Code analysis has identified potential bug(s) in this PR that may warrant investigation.

Next Steps:
Please review the detailed findings in the workflow run.

Note: When viewing the workflow output, scroll to the bottom to find the Final Analysis Summary.

After you review the findings, please tag the issue as follows:

  • If the detected issue is real or was helpful in any way, please tag the issue with O-AI-Review-Real-Issue-Found
  • If the detected issue was not helpful in any way, please tag the issue with O-AI-Review-Not-Helpful

Previously, mergingIterV2.NextPrefix fell back to a full SeekGE on
the merging iter. This commit implements it directly, driving the
per-level NextPrefix fast path where applicable.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
Copy link
Copy Markdown
Member Author

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RaduBerinde made 2 comments.
Reviewable status: 2 of 3 files reviewed, 2 unresolved discussions (waiting on sumeerbhola).


merging_iter_v2.go line 815 at r2 (raw file):

Previously, sumeerbhola wrote…

Do we not bother distinguishing between these cases because for the exhausted or already >= succKey case we are relying on the SeekGE not doing much?
It still seems worthwhile avoiding any calls: we've desired NextPrefix to be almost as fast as Next.

Good point, fixed.


merging_iter_v2.go line 821 at r2 (raw file):

Previously, sumeerbhola wrote…

I am trying to think through the approach difference between what is happening here and in mergingIter. At a very high-level I think mergingIter.NextPrefix manages to do two things:

  • Incrementally maintain the heap, versus doing an initHeap.
  • Observe that the heap top is >= succKey to avoid positioning any other levels.

In the common case we may be stepping through multiple prefixes in L6, and the slab boundary is far away. Could we use the fact that NextPrefix is called when not at a slab boundary to NextPrefix the heap top, and if it is still at the top and not at the slab boundary we have nothing else to do. Even if the top level changed, but it is not a slab boundary, isn't it sufficient to check that it is >= succKey. The mergingIter does something more involved with levelsPositioned but it may not be necessary in this slab world.

I added a TODO and will tackle it separately. It's not trivial because if we discover a rangedel when we Next() that level, or the level goes too far, we do need to rebuild the slab. There is a similar TODO in SeekGE(TrySeekUsingNext); both fast paths will likely look similar.

@github-actions
Copy link
Copy Markdown

Potential Bug(s) Detected

The three-stage Claude Code analysis has identified potential bug(s) in this PR that may warrant investigation.

Next Steps:
Please review the detailed findings in the workflow run.

Note: When viewing the workflow output, scroll to the bottom to find the Final Analysis Summary.

After you review the findings, please tag the issue as follows:

  • If the detected issue is real or was helpful in any way, please tag the issue with O-AI-Review-Real-Issue-Found
  • If the detected issue was not helpful in any way, please tag the issue with O-AI-Review-Not-Helpful

Copy link
Copy Markdown
Collaborator

@sumeerbhola sumeerbhola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@sumeerbhola made 1 comment.
Reviewable status: 2 of 3 files reviewed, 2 unresolved discussions (waiting on RaduBerinde).

@RaduBerinde
Copy link
Copy Markdown
Member Author

TFTR!

@RaduBerinde RaduBerinde merged commit d45d7d3 into cockroachdb:master Apr 30, 2026
9 checks passed
@RaduBerinde RaduBerinde deleted the iterv2-next-prefix branch April 30, 2026 00:39
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.

3 participants