Skip to content

Kv: drop double-scan in kv_idx_lower_bound on miss#333

Open
heifner wants to merge 1 commit into
masterfrom
perf/kv-idx-lower-bound-no-double-scan
Open

Kv: drop double-scan in kv_idx_lower_bound on miss#333
heifner wants to merge 1 commit into
masterfrom
perf/kv-idx-lower-bound-no-double-scan

Conversation

@heifner
Copy link
Copy Markdown
Contributor

@heifner heifner commented May 13, 2026

Summary

apply_context::kv_idx_lower_bound issued a second lower_bound((code, table_id)) after the keyed seek missed, just to detect whether the table had any rows at all. Replace with a std::prev(itr) peek guarded by itr != idx.begin(). When the seek misses inside (code, table_id), all rows of the table (if any) lie strictly before itr in the index, so the table is non-empty iff prev(itr) is in our (code, table_id). The itr == begin() guard covers both the empty-index case (begin() == end() == itr) and the "lower_bound landed on the very first row of a later table" case.

Consensus

Pure host-side optimization. Return value and slot allocation are bit-identical for every input. The only thing that changes is the number of B-tree hops on the miss path. CPU time is subjective in Sysio, so contracts cannot observe a difference.

When the secondary-index seek misses inside (code, table_id) -- either
the index ended or landed past our (code, table_id) pair -- the old
code issued a second lower_bound((code, table_id)) just to decide
whether the table is empty. All rows of (code, table_id), if any, must
lie strictly before the first-miss iterator, so the table is non-empty
iff prev(itr) is in (code, table_id), with itr == idx.begin()
short-circuiting to "no rows before."

Return value and slot allocation are bit-identical for every input;
this saves a B-tree hop per end-of-range secondary scan. Pure
host-side optimization, not a consensus change -- contracts cannot
observe a difference beyond CPU time, which is subjective in Sysio.

Covered by existing kv_api_tests (idx_lower_bound + testidxempty for
the empty-table -1 path) and the kv_intrinsic_probe_tests suite via
PR #308 (lbpast + lbempty probes).
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