Skip to content

perf: optimize it.CountBy by removing Filter iterator chain - #813

Merged
samber merged 1 commit into
samber:masterfrom
d-enk:perf-it-count-by
Feb 24, 2026
Merged

samber merged 1 commit into
samber:masterfrom
d-enk:perf-it-count-by

Conversation

@d-enk

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

Copy link
Copy Markdown
Contributor
  • Remove unnecessary Filter(collection, predicate) chain
  • Use direct loop with predicate check instead
  • Results: -36.68% time geomean, -100% B/op, -100% allocs/op

Benchstat results:

                      │   old.txt    │              new.txt               │
                      │    sec/op    │   sec/op     vs base               │
ItCountBy/ints_10-4     433.0n ± 23%   186.2n ± 5%  -56.99% (p=0.000 n=8)
ItCountBy/ints_100-4    2.487µ ± 27%   1.678µ ± 6%  -32.55% (p=0.000 n=8)
ItCountBy/ints_1000-4   20.16µ ±  8%   17.64µ ± 4%  -12.50% (p=0.005 n=8)
geomean                 2.790µ         1.766µ       -36.68%

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

                      │  old.txt   │                new.txt                 │
                      │ allocs/op  │ allocs/op   vs base                    │
ItCountBy/ints_10-4     6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItCountBy/ints_100-4    6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)
ItCountBy/ints_1000-4   6.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=8)

- Remove unnecessary Filter(collection, predicate) chain
- Use direct loop with predicate check instead
- Results: -36.68% time geomean, -100% B/op, -100% allocs/op

Benchstat results:
                      │   old.txt    │              new.txt               │
                      │    sec/op    │   sec/op     vs base               │
ItCountBy/ints_10-4     433.0n ± 23%   186.2n ± 5%  -56.99% (p=0.000 n=8)
ItCountBy/ints_100-4    2.487µ ± 27%   1.678µ ± 6%  -32.55% (p=0.000 n=8)
ItCountBy/ints_1000-4   20.16µ ±  8%   17.64µ ± 4%  -12.50% (p=0.005 n=8)
geomean                 2.790µ         1.766µ       -36.68%

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

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