fix(p99): complement uses cardinality not key count — nsfwLevel 900ms→<10ms#200
Merged
fix(p99): complement uses cardinality not key count — nsfwLevel 900ms→<10ms#200
Conversation
nsfwLevel has 12 loaded values (6 real + stale), not 6. IN [1,2,4,8,16] = 5 keys, complement = 7 keys. Old condition (7 < 5) failed, falling through to the O(5 × acc_size) union path = 900ms at 100M. New condition: compare total complement CARDINALITY vs accumulator size. The 7 complement bitmaps have maybe ~5M total bits. Subtracting 5M bits from a 100M acc is O(5M) — microseconds. The old union path does 5× AND+OR on 100M bits = O(500M) — seconds. Condition: use complement when complement_cardinality < acc/2 OR complement_cardinality < 10M (absolute threshold for always-cheap). Also removes temporary debug eprintln from PR #199. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
Root cause found via PR #199 debug logs: nsfwLevel has 12 loaded values (6 real + 6 stale), not 6. IN [1,2,4,8,16] = 5 keys, complement = 7 keys. Old condition
7 < 5failed, falling through to the O(5 × 100M) union path = 900ms.Fix: Compare complement cardinality (total bits) vs accumulator size, not key count. The 7 complement bitmaps have ~5M total bits. Subtracting 5M bits from 100M acc is microseconds. The old 5× AND+OR on 100M acc was seconds.
New condition
Expected impact
nsfwLevel IN from 900ms → <10ms for ALL queries (was only working for narrow queries before).
Test plan
cargo check --features serverpasses🤖 Generated with Claude Code