@@ -13,7 +13,6 @@ import (
13
13
"fmt"
14
14
"strconv"
15
15
"strings"
16
- "sync"
17
16
"time"
18
17
19
18
"go.mongodb.org/mongo-driver/bson"
@@ -318,14 +317,8 @@ func (op Operation) Validate() error {
318
317
return nil
319
318
}
320
319
321
- var memoryPool = sync.Pool {
322
- New : func () interface {} {
323
- // Start with 1kb buffers.
324
- b := make ([]byte , 1024 )
325
- // Return a pointer as the static analysis tool suggests.
326
- return & b
327
- },
328
- }
320
+ // Create a pool of maximum 512 byte slices.
321
+ var memoryPool = newByteSlicePool (512 )
329
322
330
323
// Execute runs this operation.
331
324
func (op Operation ) Execute (ctx context.Context ) error {
@@ -425,17 +418,8 @@ func (op Operation) Execute(ctx context.Context) error {
425
418
conn = nil
426
419
}
427
420
428
- wm := memoryPool .Get ().( * [] byte )
421
+ wm := memoryPool .Get ()
429
422
defer func () {
430
- // Proper usage of a sync.Pool requires each entry to have approximately the same memory
431
- // cost. To obtain this property when the stored type contains a variably-sized buffer,
432
- // we add a hard limit on the maximum buffer to place back in the pool. We limit the
433
- // size to 16MiB because that's the maximum wire message size supported by MongoDB.
434
- //
435
- // Comment copied from https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/fmt/print.go;l=147
436
- if cap (* wm ) > 16 * 1024 * 1024 {
437
- return
438
- }
439
423
memoryPool .Put (wm )
440
424
}()
441
425
for {
@@ -536,7 +520,7 @@ func (op Operation) Execute(ctx context.Context) error {
536
520
}
537
521
538
522
var startedInfo startedInformation
539
- * wm , startedInfo , err = op .createWireMessage (ctx , ( * wm ) [:0 ], desc , maxTimeMS , conn )
523
+ wm , startedInfo , err = op .createWireMessage (ctx , wm [:0 ], desc , maxTimeMS , conn )
540
524
if err != nil {
541
525
return err
542
526
}
@@ -551,12 +535,12 @@ func (op Operation) Execute(ctx context.Context) error {
551
535
op .publishStartedEvent (ctx , startedInfo )
552
536
553
537
// get the moreToCome flag information before we compress
554
- moreToCome := wiremessage .IsMsgMoreToCome (* wm )
538
+ moreToCome := wiremessage .IsMsgMoreToCome (wm )
555
539
556
540
// compress wiremessage if allowed
557
541
if compressor , ok := conn .(Compressor ); ok && op .canCompress (startedInfo .cmdName ) {
558
- b := memoryPool .Get ().( * [] byte )
559
- * b , err = compressor .CompressWireMessage (* wm , ( * b ) [:0 ])
542
+ b := memoryPool .Get ()
543
+ b , err = compressor .CompressWireMessage (wm , b [:0 ])
560
544
memoryPool .Put (wm )
561
545
wm = b
562
546
if err != nil {
@@ -595,7 +579,7 @@ func (op Operation) Execute(ctx context.Context) error {
595
579
if moreToCome {
596
580
roundTrip = op .moreToComeRoundTrip
597
581
}
598
- res , * wm , err = roundTrip (ctx , conn , * wm )
582
+ res , wm , err = roundTrip (ctx , conn , wm )
599
583
600
584
if ep , ok := srvr .(ErrorProcessor ); ok {
601
585
_ = ep .ProcessError (err , conn )
@@ -975,12 +959,12 @@ func (Operation) decompressWireMessage(wm []byte) ([]byte, error) {
975
959
}
976
960
977
961
// Copy msg, which is a subslice of wm. wm will be used to store the return value of the decompressed message.
978
- b := memoryPool .Get ().( * [] byte )
962
+ b := memoryPool .Get ()
979
963
msglen := len (msg )
980
- if len (* b ) < msglen {
981
- * b = make ([]byte , msglen )
964
+ if len (b ) < msglen {
965
+ b = make ([]byte , msglen )
982
966
}
983
- copy (* b , msg )
967
+ copy (b , msg )
984
968
defer func () {
985
969
memoryPool .Put (b )
986
970
}()
@@ -993,7 +977,7 @@ func (Operation) decompressWireMessage(wm []byte) ([]byte, error) {
993
977
Compressor : compressorID ,
994
978
UncompressedSize : uncompressedSize ,
995
979
}
996
- uncompressed , err := DecompressPayload (( * b ) [0 :msglen ], opts )
980
+ uncompressed , err := DecompressPayload (b [0 :msglen ], opts )
997
981
if err != nil {
998
982
return nil , err
999
983
}
0 commit comments