Repository navigation
perf(weighted_frequency): rescore retained candidates without copying them - #227
Closed
zzylol wants to merge 1 commit into
Conversation
… them With a candidate heap, every update cloned each retained candidate (its identity tuple and hash key) to rebuild the heap, and scoring each candidate collected its row estimates into a fresh Vec. At capacity 100 with two-label Utf8 identities that was about 66 us (CMS) and 77 us (CountSketch) per update in a release build. The update now moves the candidates out of the heap, adds the new key only when it is not already retained, and scores them with one reused buffer (Count-Min takes the row minimum without any buffer). The same benchmark runs at about 33 us and 43 us. Every retained score is still re-read after each update, so the candidates and their scores are unchanged; the golden snapshots still match.
Contributor
Author
|
Closing: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
With a candidate heap,
WeightedFrequency::updateis the per-sample hot path of a keyed top-k build. Each call cloned every retained candidate (its identity tuple and its hash key) to rebuild the heap, and scoring each candidate collected its row estimates into a freshVec. At capacity 100 that is about 200 heap allocations per sample.What
updatereturns the same state as before, about twice as fast at capacity 100. No interface changes.Stacked on #226 (shape and weight checks), which is stacked on #225. Retarget to
mainafter those merge.How
std::mem::take(..).into_vec()) instead of cloning them, and adds the new key only when it is not already retained. Before, the duplicate was added and then dropped by the sort-and-dedup inretain, which keeps the earlier entry, so the result is the same.retainreuses one buffer for every candidate's CountSketch row estimates, and Count-Min takes the row minimum with no buffer.Key code interfaces added / modified
None. (
estimate_key_withis a private helper.)Before this PR
Release build, 200,000 updates over 1,000 two-label
Utf8identities, width 2048, depth 5:After this PR
The remaining cost is rescoring: each update re-hashes every retained key (
capacity x depthhashes) and re-sorts the candidates. Caching each candidate's row cells (not serialized) would remove the hashing atcapacity x depth x 16bytes of memory; that is left as a follow-up.Verification
planner_v1_snapshots_remain_compatible(golden bytes) and every existing weighted-frequency test pass unchanged.retained_scores_track_every_update: on an 8 x 3 shape with 7 keys and capacity 3 (heavy collisions), after each of 60 updates every retained score equals the point estimate and no key is retained twice. It passes before and after this change; it guards the rescoring behavior the optimization must keep.#[ignore]benchmark test run withcargo test --release, removed before commit.cargo fmt --all -- --check;cargo clippy --workspace --all-targets --all-features --locked -- -D warnings.