Skip to content

optimize iterator Release/Init to avoid heap escape#56

Open
mmsqe wants to merge 4 commits intotidwall:masterfrom
mmsqe:ref_iter
Open

optimize iterator Release/Init to avoid heap escape#56
mmsqe wants to merge 4 commits intotidwall:masterfrom
mmsqe:ref_iter

Conversation

@mmsqe
Copy link
Contributor

@mmsqe mmsqe commented Jan 16, 2026

  • prevent heap escape during iterator reuse by replacing *iter = IterG[T]{} with manual field clearing
  • improve performance in high-frequency iterator reuse scenarios
benchstat /private/tmp/bf.txt /private/tmp/af.txt 
seed: 1
goos: darwin
goarch: arm64
pkg: github.com/tidwall/btree
cpu: Apple M4 Max
                                  │ /private/tmp/bf.txt │         /private/tmp/af.txt         │
                                  │       sec/op        │    sec/op     vs base               │
IteratorCreationAlloc/no_reuse-16           39.31n ± 1%   37.68n ± 12%        ~ (p=0.132 n=6)
IteratorCreationAlloc/reuse-16              15.61n ± 1%   12.98n ±  0%  -16.88% (p=0.002 n=6)
IteratorRelease-16                         11.575n ± 1%   9.409n ±  1%  -18.71% (p=0.002 n=6)
IteratorReuse/Recreate-16                   2.160µ ± 1%   2.118µ ±  1%   -1.99% (p=0.002 n=6)
IteratorReuse/Reuse-16                      2.169µ ± 1%   2.142µ ±  1%   -1.22% (p=0.019 n=6)
geomean                                     127.2n        115.8n         -8.91%

mmsqe added 4 commits January 16, 2026 16:49
manual clear iterator fields instead of struct assignment
to prevent heap allocation during high-frequency reuse
test
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