Commit d9af486
committed
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%1 parent 699256f commit d9af486
3 files changed
+27
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
489 | 489 | | |
490 | 490 | | |
491 | 491 | | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
492 | 499 | | |
493 | | - | |
| 500 | + | |
494 | 501 | | |
495 | 502 | | |
496 | 503 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
252 | 259 | | |
253 | 260 | | |
254 | 261 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
372 | 372 | | |
373 | 373 | | |
374 | 374 | | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
375 | 381 | | |
376 | 382 | | |
377 | 383 | | |
378 | 384 | | |
379 | 385 | | |
380 | 386 | | |
381 | | - | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
382 | 391 | | |
383 | 392 | | |
384 | 393 | | |
| |||
674 | 683 | | |
675 | 684 | | |
676 | 685 | | |
677 | | - | |
| 686 | + | |
678 | 687 | | |
679 | 688 | | |
680 | 689 | | |
681 | | - | |
| 690 | + | |
682 | 691 | | |
683 | 692 | | |
684 | 693 | | |
| |||
0 commit comments