Skip to content

perf: optimize it.ContainsBy/EveryBy/SomeBy/NoneBy to avoid unnecessary allocations#812

Merged
samber merged 2 commits intosamber:masterfrom
d-enk:perf-it-intersect
Feb 24, 2026
Merged

perf: optimize it.ContainsBy/EveryBy/SomeBy/NoneBy to avoid unnecessary allocations#812
samber merged 2 commits intosamber:masterfrom
d-enk:perf-it-intersect

Conversation

@d-enk
Copy link
Contributor

@d-enk d-enk commented Feb 24, 2026

Replace intermediate iterator chains with direct loops and early returns.

The direct loops with early return eliminate intermediate iterator creation from Filter(), Reject(), IsNotEmpty() and IsEmpty() functions.

Benchmark results (benchstat):

old.txtnew.txt               │
                         │    sec/opsec/op     vs baseItContainsBy/ints_10-4      807.2n ± 54%   151.0n ±  6%  -81.29% (p=0.000 n=8)
ItContainsBy/ints_100-4     2.447µ ± 14%   1.531µ ± 11%  -37.42% (p=0.000 n=8)
ItContainsBy/ints_1000-4    17.67µ ±  4%   13.85µ ±  4%  -21.64% (p=0.000 n=8)
ItEveryBy/ints_10-4        1022.5n ± 63%   208.0n ± 34%  -79.66% (p=0.000 n=8)
ItEveryBy/ints_100-4        5.640µ ± 36%   1.542µ ±  9%  -72.67% (p=0.000 n=8)
ItEveryBy/ints_1000-4       52.22µ ± 54%   19.29µ ± 16%  -63.06% (p=0.000 n=8)
ItSomeBy/ints_10-4         2227.5n ± 43%   187.1n ± 14%  -91.60% (p=0.000 n=8)
ItSomeBy/ints_100-4         4.611µ ± 22%   1.691µ ± 10%  -63.32% (p=0.000 n=8)
ItSomeBy/ints_1000-4        39.41µ ± 27%   22.61µ ± 24%  -42.63% (p=0.000 n=8)
ItNoneBy/ints_10-4         1657.5n ± 34%   196.3n ± 21%  -88.15% (p=0.000 n=8)
ItNoneBy/ints_100-4         4.503µ ± 20%   1.743µ ± 12%  -61.30% (p=0.000 n=8)
ItNoneBy/ints_1000-4        29.39µ ± 29%   16.66µ ± 15%  -43.32% (p=0.000 n=8)
geomean                     5.591µ         1.747µ        -68.76%old.txtnew.txt                │
                         │    B/opB/op    vs baseItContainsBy/ints_10-4     200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItContainsBy/ints_100-4    200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItContainsBy/ints_1000-4   200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_10-4        184.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_100-4       184.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_1000-4      184.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_10-4         200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_100-4        200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_1000-4       200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_10-4         200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_100-4        200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_1000-4       200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)

                         │  old.txtnew.txt                 │
                         │ allocs/opallocs/op   vs baseItContainsBy/ints_10-4     7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItContainsBy/ints_100-4    7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItContainsBy/ints_1000-4   7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_10-4        6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_100-4       6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_1000-4      6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_10-4         7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_100-4        7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_1000-4       7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_10-4         7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_100-4        7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_1000-4       7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)

…ssary allocations

Replace intermediate iterator chains with direct loops and early returns.

The direct loops with early return eliminate intermediate iterator
creation from Filter(), Reject(), IsNotEmpty() and IsEmpty() functions.

Benchmark results (benchstat):
                         │    old.txt    │               new.txt               │
                         │    sec/op     │    sec/op     vs base               │
ItContainsBy/ints_10-4      807.2n ± 54%   151.0n ±  6%  -81.29% (p=0.000 n=8)
ItContainsBy/ints_100-4     2.447µ ± 14%   1.531µ ± 11%  -37.42% (p=0.000 n=8)
ItContainsBy/ints_1000-4    17.67µ ±  4%   13.85µ ±  4%  -21.64% (p=0.000 n=8)
ItEveryBy/ints_10-4        1022.5n ± 63%   208.0n ± 34%  -79.66% (p=0.000 n=8)
ItEveryBy/ints_100-4        5.640µ ± 36%   1.542µ ±  9%  -72.67% (p=0.000 n=8)
ItEveryBy/ints_1000-4       52.22µ ± 54%   19.29µ ± 16%  -63.06% (p=0.000 n=8)
ItSomeBy/ints_10-4         2227.5n ± 43%   187.1n ± 14%  -91.60% (p=0.000 n=8)
ItSomeBy/ints_100-4         4.611µ ± 22%   1.691µ ± 10%  -63.32% (p=0.000 n=8)
ItSomeBy/ints_1000-4        39.41µ ± 27%   22.61µ ± 24%  -42.63% (p=0.000 n=8)
ItNoneBy/ints_10-4         1657.5n ± 34%   196.3n ± 21%  -88.15% (p=0.000 n=8)
ItNoneBy/ints_100-4         4.503µ ± 20%   1.743µ ± 12%  -61.30% (p=0.000 n=8)
ItNoneBy/ints_1000-4        29.39µ ± 29%   16.66µ ± 15%  -43.32% (p=0.000 n=8)
geomean                     5.591µ         1.747µ        -68.76%

                         │  old.txt   │               new.txt                │
                         │    B/op    │   B/op    vs base                    │
ItContainsBy/ints_10-4     200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItContainsBy/ints_100-4    200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItContainsBy/ints_1000-4   200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_10-4        184.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_100-4       184.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_1000-4      184.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_10-4         200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_100-4        200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_1000-4       200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_10-4         200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_100-4        200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_1000-4       200.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)

                         │  old.txt   │                new.txt                 │
                         │ allocs/op  │ allocs/op   vs base                    │
ItContainsBy/ints_10-4     7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItContainsBy/ints_100-4    7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItContainsBy/ints_1000-4   7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_10-4        6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_100-4       6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItEveryBy/ints_1000-4      6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_10-4         7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_100-4        7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItSomeBy/ints_1000-4       7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_10-4         7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_100-4        7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItNoneBy/ints_1000-4       7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
@d-enk d-enk changed the title perf: optimize it.ContainsBy, EveryBy, SomeBy, NoneBy to avoid unnece… perf: optimize it.ContainsBy/EveryBy/SomeBy/NoneBy to avoid unnecessary allocations Feb 24, 2026
@samber samber merged commit d090134 into samber:master Feb 24, 2026
1 check passed
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.

2 participants