google / benchmark

A microbenchmark support library
Apache License 2.0
8.59k stars 1.57k forks source link

[FR] possible to stop benchmark loop from doing a memory allocation at the end? #1784

Closed jjYBdx4IL closed 2 months ago

jjYBdx4IL commented 2 months ago
                for (auto _ : state) {
                    // check for memory allocation in process()
                    SAVE_MEM_STATE(s1inner);
                    // This code gets timed
                    benchmark::DoNotOptimize(
                        x += iap->process(data)
                    );
                    SAVE_MEM_STATE(s2inner);
                    ASSERT_NO_ALLOCS(s1inner, s2inner);
                }

I'm using the _Crt* tools in MSVC to detect memory allocations in the process function for realtime purposes. Maybe there is a simple way to prevent the memory allocation at the end of the for loop so I can move the allocation detection macros outside of the loop? (when '__' goes out of scope?)

For clarification, the following creates memory snapshots that show an allocation has happened:

                int x = 0;
                _CrtMemState _s1, _s2;
                for (auto _ : state) {
                    //// This code gets timed
                    //benchmark::DoNotOptimize(
                    //    x += iap->process(data) + 1
                    //);
                    _CrtMemCheckpoint(&_s1);
                }
                _CrtMemCheckpoint(&_s2);
                assert(_s1.lTotalCount == _s2.lTotalCount); // fails
LebedevRI commented 2 months ago

Can you perhaps at least tell us what particular allocation is in question here, by using debugger and placing breakpoint on the allocation function or something?