Skip to content

Commit f99f130

Browse files
authored
Use a pool for document comparisons (#169)
Rather than allocating a fresh buffer for each document, this changeset uses a dynamic buffer pool. This relieves GC pressure when comparing documents, which is particularly relevant during generation 0 (i.e., initial comparison of all documents).
1 parent 67c0de8 commit f99f130

File tree

12 files changed

+674
-3
lines changed

12 files changed

+674
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ require (
4444
github.com/klauspost/compress v1.16.7 // indirect
4545
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
4646
github.com/leodido/go-urn v1.4.0 // indirect
47+
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
4748
github.com/mattn/go-colorable v0.1.13 // indirect
4849
github.com/mattn/go-isatty v0.0.20 // indirect
4950
github.com/mattn/go-runewidth v0.0.9 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQe
6565
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
6666
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
6767
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
68+
github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6cdF0Y8=
69+
github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=
6870
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
6971
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
7072
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=

internal/verifier/compare.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/10gen/migration-verifier/internal/types"
1414
"github.com/10gen/migration-verifier/internal/util"
1515
"github.com/10gen/migration-verifier/option"
16+
pool "github.com/libp2p/go-buffer-pool"
1617
"github.com/pkg/errors"
1718
"go.mongodb.org/mongo-driver/v2/bson"
1819
"go.mongodb.org/mongo-driver/v2/mongo"
@@ -179,6 +180,9 @@ func (verifier *Verifier) compareDocsFromChannels(
179180
return errors.Wrap(err, "failed to compare documents")
180181
}
181182

183+
pool.Put(srcDoc.doc)
184+
pool.Put(dstDoc.doc)
185+
182186
for i := range mismatches {
183187
mismatches[i].SrcTimestamp = option.Some(srcDoc.ts)
184188
mismatches[i].DstTimestamp = option.Some(dstDoc.ts)
@@ -326,6 +330,8 @@ func (verifier *Verifier) compareDocsFromChannels(
326330
SrcTimestamp: option.Some(docWithTs.ts),
327331
},
328332
)
333+
334+
pool.Put(docWithTs.doc)
329335
}
330336

331337
for _, docWithTs := range dstCache {
@@ -343,6 +349,8 @@ func (verifier *Verifier) compareDocsFromChannels(
343349
DstTimestamp: option.Some(docWithTs.ts),
344350
},
345351
)
352+
353+
pool.Put(docWithTs.doc)
346354
}
347355

348356
return results, srcDocCount, srcByteCount, nil
@@ -352,14 +360,21 @@ func getDocIdFromComparison(
352360
docCompareMethod DocCompareMethod,
353361
doc bson.Raw,
354362
) bson.RawValue {
363+
var docID bson.RawValue
364+
355365
switch docCompareMethod {
356366
case DocCompareBinary, DocCompareIgnoreOrder:
357-
return doc.Lookup("_id")
367+
docID = doc.Lookup("_id")
358368
case DocCompareToHashedIndexKey:
359-
return doc.Lookup(docKeyInHashedCompare, "_id")
369+
docID = doc.Lookup(docKeyInHashedCompare, "_id")
360370
default:
361371
panic("bad doc compare method: " + docCompareMethod)
362372
}
373+
374+
// We clone the value because the document might be from a pool.
375+
docID.Value = slices.Clone(docID.Value)
376+
377+
return docID
363378
}
364379

365380
func getDocKeyValues(
@@ -527,11 +542,14 @@ func iterateCursorToChannel(
527542
return errors.Wrap(err, "reading cluster time from session")
528543
}
529544

545+
buf := pool.Get(len(cursor.Current))
546+
copy(buf, cursor.Current)
547+
530548
err = chanutil.WriteWithDoneCheck(
531549
sctx,
532550
writer,
533551
docWithTs{
534-
doc: slices.Clone(cursor.Current),
552+
doc: buf,
535553
ts: clusterTime,
536554
},
537555
)

vendor/github.com/libp2p/go-buffer-pool/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/libp2p/go-buffer-pool/LICENSE-BSD

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/libp2p/go-buffer-pool/README.md

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)