Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce memory footprint by reusing buffers
This commit changes mongoSocket.Query and addBSON to reuse allocated []byte buffers by getting and putting then into a sync.Pool. Also the function MarshalBuffer was added to the bson package. It behaves in the exact same way to Marshal but instead of allocating a []byte buffer it receives an already existing buffer. Benchmarks show a really nice decrease in memory footprint specially when large messages are being sent to MongoDB, as is the case of inserting multiple documents at once. $ benchcmp before.txt after.txt benchmark old ns/op new ns/op delta BenchmarkInsertSingle-4 101203 95119 -6.01% BenchmarkInsertMultiple-4 1131565 1066556 -5.75% benchmark old allocs new allocs delta BenchmarkInsertSingle-4 50 48 -4.00% BenchmarkInsertMultiple-4 456 343 -24.78% benchmark old bytes new bytes delta BenchmarkInsertSingle-4 2472 1385 -43.97% BenchmarkInsertMultiple-4 191180 32525 -82.99%
- Loading branch information