Skip to content

fix(p99): complement uses cardinality not key count — nsfwLevel 900ms→<10ms#200

Merged
JustMaier merged 1 commit intomainfrom
ivy/fix-complement-cardinality
Apr 13, 2026
Merged

fix(p99): complement uses cardinality not key count — nsfwLevel 900ms→<10ms#200
JustMaier merged 1 commit intomainfrom
ivy/fix-complement-cardinality

Conversation

@JustMaier
Copy link
Copy Markdown
Contributor

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 < 5 failed, 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

complement_cardinality < acc_size / 2 || complement_cardinality < 10_000_000

Expected impact

nsfwLevel IN from 900ms → <10ms for ALL queries (was only working for narrow queries before).

Test plan

  • cargo check --features server passes
  • Deploy, check traces: nsfwLevel and_us should drop from 600-1000ms to <10ms
  • Check [IN_COMPLEMENT] log lines fire for nsfwLevel

🤖 Generated with Claude Code

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>
@JustMaier JustMaier merged commit c1367e9 into main Apr 13, 2026
1 check failed
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