Skip to content

perf: optimize it.Find to avoid unnecessary allocations#811

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

perf: optimize it.Find to avoid unnecessary allocations#811
samber merged 2 commits intosamber:masterfrom
d-enk:perf-it-find

Conversation

@d-enk
Copy link
Contributor

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

Replace First(Filter(collection, predicate)) with direct loop implementation.

The direct loop with early return eliminates intermediate iterator creation from Filter() and First() functions.

Benchmark results:

old.txtnew.txt               │
                   │    sec/opsec/op     vs baseItFind/ints_10-4     521.3n ± 14%   157.3n ±  8%  -69.82% (p=0.000 n=8)
ItFind/ints_100-4    1.913µ ± 13%   1.545µ ± 22%  -19.26% (p=0.002 n=8)
ItFind/ints_1000-4   17.07µ ±  7%   12.88µ ±  1%  -24.59% (p=0.000 n=8)
geomean              2.573µ         1.462µ        -43.15%old.txtnew.txt                │
                   │    B/opB/op    vs baseItFind/ints_10-4     192.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItFind/ints_100-4    192.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItFind/ints_1000-4   192.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)

                   │  old.txtnew.txt                 │
                   │ allocs/opallocs/op   vs baseItFind/ints_10-4     7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItFind/ints_100-4    7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItFind/ints_1000-4   7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)

Replace First(Filter(collection, predicate)) with direct loop implementation.

The direct loop with early return eliminates intermediate iterator
creation from Filter() and First() functions.

Benchmark results:
                   │   old.txt    │               new.txt               │
                   │    sec/op    │    sec/op     vs base               │
ItFind/ints_10-4     521.3n ± 14%   157.3n ±  8%  -69.82% (p=0.000 n=8)
ItFind/ints_100-4    1.913µ ± 13%   1.545µ ± 22%  -19.26% (p=0.002 n=8)
ItFind/ints_1000-4   17.07µ ±  7%   12.88µ ±  1%  -24.59% (p=0.000 n=8)
geomean              2.573µ         1.462µ        -43.15%

                   │  old.txt   │               new.txt                │
                   │    B/op    │   B/op    vs base                    │
ItFind/ints_10-4     192.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItFind/ints_100-4    192.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)
ItFind/ints_1000-4   192.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=8)

                   │  old.txt   │                new.txt                 │
                   │ allocs/op  │ allocs/op   vs base                    │
ItFind/ints_10-4     7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItFind/ints_100-4    7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItFind/ints_1000-4   7.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
@samber samber merged commit b33df06 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