webrtc icon indicating copy to clipboard operation
webrtc copied to clipboard

Use Sync.Pool throughout for buffers

Open adriancable opened this issue 1 year ago • 1 comments

There are 'hot' places in the code that allocate (and then need to GC) lots of small buffers, e.g. https://github.com/pion/srtp/blob/b2c2ab8afc1745b80378f66014934bd9111140aa/crypto.go#L24

Would it be better to use Sync.Pool?

Some data: pprof / top 20 shows something like this:

 5.64s  0.74% 46.12%     63.84s  8.40%  github.com/pion/srtp/v3.xorBytesCTR
 5.38s  0.71% 47.56%     10.37s  1.36%  github.com/pion/transport/v3/utils/xor.XorBytes
 4.99s  0.66% 48.22%      4.99s  0.66%  github.com/pion/transport/v3/utils/xor.xorBytesNEON32

Since the 'meat' of xorBytesCTR is xor.XorBytes, we would hope to find most of the time spent in xorBytesCTR being spent in xor.XorBytes. But this isn't the case, which points at the allocations being the biggest consumers of CPU. We could probably eliminate this via usage of Sync.Pool. There are probably other places that would benefit too, wherever small buffers are frequently allocated, not just here.

adriancable avatar Aug 15 '24 20:08 adriancable