`b.Loop` disables inlining, specifically.
I assume in your code, `WriteFrames` gets inlined when you use `for range
b.N`. After inlinining `WriteFrames`, the iterator function itself also
gets inlined, as it directly `range`s over it. After that inlining step,
the iterator function doesn't need to be allocated.
With `for b.Loop()`, `WriteFrames` itself does not get inlined, meaning the
function needs to be allocated to get passed as an argument.

On Fri, 1 Aug 2025 at 22:39, Pierre Durand <pierredur...@gmail.com> wrote:

> I think it's related to the fact that b.Loop() disables some
> optimizations, but I'm not sure.
>
> Le vendredi 1 août 2025 à 22:33:56 UTC+2, Pierre Durand a écrit :
>
>> I can reproduce the issue with this code
>> https://github.com/pierrre/go-libs/blob/31bec3f12a86382924cc0b486c2008db062a14bd/runtimeutil/runtimeutil_test.go#L101-L118
>> Sorry I can't write a smaller code snippet, because I think it's related
>> to escape analysis, and I don't fully understand it.
>>
>> As you can see, I'm benchmarking the same function WriteFrames() with the
>> old and the new benchmarking methods.
>>
>> When I run the benchmark, here is the result
>>
>> go test -v -run=^$ -bench="^BenchmarkWriteFrames" -benchmem ./runtimeutil
>> goos: linux
>> goarch: amd64
>> pkg: github.com/pierrre/go-libs/runtimeutil
>> cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
>> BenchmarkWriteFrames
>> BenchmarkWriteFrames-12           366825              3091 ns/op
>>       0 B/op          0 allocs/op
>> BenchmarkWriteFramesNew
>> BenchmarkWriteFramesNew-12        328620              3445 ns/op
>>      80 B/op          4 allocs/op
>> PASS
>> ok      github.com/pierrre/go-libs/runtimeutil  2.303s
>>
>>
>>
>> Le ven. 1 août 2025 à 22:04, Junyang Shao <shaoj...@google.com> a écrit :
>> >
>> > Hello Pierre,
>> >
>> > Thank you for bringing up this issue.
>> >
>> > May you share the code snippet that triggers this behavior? Thanks.
>>
>> >
>> > On Fri, Aug 1, 2025 at 12:50 PM Pierre Durand <pierre...@gmail.com>
>> wrote:
>> >>
>> >> Hello
>> >>
>> >> I noticed a weird behavior when I'm benchmarking with testing.B.Loop()
>> code that uses iterators .
>> >> The benchmark shows allocations where I start to iterate the iterator,
>> and where I declare variables (before the loop) that are used inside the
>> iterator loop.
>> >> I know that my code is not doing any allocation, so it's strange.
>> >> If I change my benchmark to use the old "range b.N", then it doesn't
>> show this strange behavior.
>> >> If I check allocations with "testing.AllocsPerRun", I don't see any
>> allocation.
>> >>
>> >> Is that a know issue ? Should I open a bug ?
>> >> What should I do ? Use the old benchmarking method ?
>> >>
>> >> Regards
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> Groups "golang-nuts" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> an email to golang-nuts...@googlegroups.com.
>> >> To view this discussion visit
>> https://groups.google.com/d/msgid/golang-nuts/70456dc1-3380-40fd-951d-e52275bc48a5n%40googlegroups.com
>> .
>> >
>> >
>> >
>> > --
>> > Thanks,
>> > Junyang
>>
>>
>>
>> --
>> Pierre Durand
>>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion visit
> https://groups.google.com/d/msgid/golang-nuts/f7519a16-a4cd-4d98-8559-bc1cfe5c8fdfn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/f7519a16-a4cd-4d98-8559-bc1cfe5c8fdfn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGa8nU4nGXa%2BLzgVa7T-dvEkjbS9LrpuSnWFJ6EhDHZdg%40mail.gmail.com.

Reply via email to