Replace the multi-pass where() chain in Apply() with a single loop that
evaluates all filter predicates per item and skips immediately on first
mismatch. This eliminates N intermediate slice allocations (one per
active filter) and avoids re-scanning the full dataset for each filter
dimension.
Key changes in filter.go:
- Single loop with continue-on-mismatch for BOGO, category, department,
and query filters — combined categories check scans item.Categories
once for both BOGO and category instead of twice
- Pre-allocate result slice capped at min(len(items), opts.Limit) to
avoid grow-and-copy churn
- Fast-path bypass when no filters are active (just apply limit)
- Break early once limit is reached instead of filtering everything
and truncating after
- Remove the now-unused where() helper function
- Add early-return fast paths to CleanText() for the common case where
input contains no HTML entities or newlines, avoiding unnecessary
html.UnescapeString and ReplaceAll calls
Test coverage:
- filter_equivalence_test.go (new): Reference implementation of the
original multi-pass algorithm with 500 randomized test cases verifying
behavioral equivalence. Includes allocation budget guardrail (<=80
allocs/op for 1k items) to catch accidental regression to multi-pass.
Benchmarks for new vs legacy reference on identical workload.
- filter_test.go: Benchmark comparisons for CleanText on plain text
(fast path) vs escaped HTML (full path), new vs legacy.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>