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 to reuse allocated []byte buffers by getting and putting then into a sync.Pool. Also the function MarshalBuffer was added to the bson package and the addBSON function now uses it. 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 94191 90779 -3.62% BenchmarkInsertMultiple-4 1076423 937569 -12.90% benchmark old allocs new allocs delta BenchmarkInsertSingle-4 50 46 -8.00% BenchmarkInsertMultiple-4 456 242 -46.93% benchmark old bytes new bytes delta BenchmarkInsertSingle-4 2472 1320 -46.60% BenchmarkInsertMultiple-4 191180 29228 -84.71%
- Loading branch information