Commit cdb160e
committed
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%1 parent b20eaf4 commit cdb160e
3 files changed
+28
-3
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 | + | |
| 687 | + | |
| 688 | + | |
678 | 689 | | |
679 | 690 | | |
680 | 691 | | |
| |||
0 commit comments