Skip to content

Reduce allocations in Ipv4L3Protocol and Ipv6L3Protocol by reusing callbacks, and in Buffer allocations by overprovisioning memory.

Gabriel Ferreira requested to merge Gabrielcarvfer/ns-3-dev:ReduceAllocs into master

I was profiling wifi-he-network with my Windows 11 laptop and noticed pretty much 60% of CPU time was spent allocating memory (in the desktop, it is about 10%). Crazy ain't it?

It is due to the terrible memory allocator used by default.

This MR adds a few patches to try to mitigate the issue:

  1. Reuse routing callbacks in Ipv4L3Protocol::Receive
    • Saves up ~30 million allocations in wifi-he-network --simulationTime=0.25 --frequency=5 --useRts=0 --minExpectedThroughput=6 --maxExpectedThroughput=844
  2. Over-provision 100 bytes in the Buffer allocations (it is very likely packets will receive headers, so why not preallocate the space for them?)
    • Saves up ~10 million allocations in wifi-he-network --simulationTime=0.25 --frequency=5 --useRts=0 --minExpectedThroughput=6 --maxExpectedThroughput=844

End result: from 147.695.545 allocs down to 103.520.590, ~30% reduction. 😄

P.S.: We could save another 20 million allocs by replacing Ptr with Packet. Stop making a new allocation + heap copy + instruction cache misses calling the constructor and destructor of Ptr, and switching to virtually free stack copies. But it is very intrusive and will take some more time, but I don't want to keep blocking this.

Edited by Gabriel Ferreira

Merge request reports