Skip to content

optimize allocations in redactStartedInformationCmd #2129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions x/mongo/driver/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,43 @@ type ResponseInfo struct {
}

func redactStartedInformationCmd(info startedInformation) bson.Raw {
intLen := func(n int) int {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this approach is OK, where should this utility function be placed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach looks good. I don't see any other uses for this logic currently, so I think it's fine to keep it as an anonymous function here for now. If we end up using this logic more in the future, we can move it into a shared package.

if n == 0 {
return 1 // Special case: 0 has one digit
}
count := 0
for n > 0 {
n /= 10
count++
}
return count
}

var cmdCopy bson.Raw

// Make a copy of the command. Redact if the command is security
// sensitive and cannot be monitored. If there was a type 1 payload for
// the current batch, convert it to a BSON array
if !info.redacted {
cmdCopy = make([]byte, 0, len(info.cmd))
cmdLen := len(info.cmd)
for _, seq := range info.documentSequences {
cmdLen += 7 // 2 (header) + 4 (array length) + 1 (array end)
cmdLen += len(seq.identifier)
data := seq.data
i := 0
for {
doc, rest, ok := bsoncore.ReadDocument(data)
if !ok {
break
}
data = rest
cmdLen += len(doc)
cmdLen += intLen(i)
i++
}
}

cmdCopy = make([]byte, 0, cmdLen)
cmdCopy = append(cmdCopy, info.cmd...)

if len(info.documentSequences) > 0 {
Expand Down Expand Up @@ -1808,7 +1838,6 @@ func (op Operation) createReadPref(desc description.SelectedServer, isOpQuery bo
// TODO if supplied readPreference was "overwritten" with primary in description.selectForReplicaSet.
if desc.Server.Kind == description.ServerKindStandalone || (isOpQuery &&
desc.Server.Kind != description.ServerKindMongos) ||

op.Type == Write || (op.IsOutputAggregate && desc.Server.WireVersion.Max < 13) {
// Don't send read preference for:
// 1. all standalones
Expand Down
28 changes: 28 additions & 0 deletions x/mongo/driver/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"
"context"
"errors"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -1063,3 +1064,30 @@ func TestMarshalBSONWriteConcern(t *testing.T) {
})
}
}

func BenchmarkRedactStartedInformationCmd(b *testing.B) {
for _, size := range []int{0, 1, 5, 10, 100, 1000} {
info := startedInformation{
cmd: make([]byte, 100),
documentSequences: make([]struct {
identifier string
data []byte
}, size),
}
for i := 0; i < size; i++ {
info.documentSequences[i] = struct {
identifier string
data []byte
}{
identifier: strconv.Itoa(i),
data: make([]byte, 100),
}
}
b.Run(strconv.Itoa(size), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
redactStartedInformationCmd(info)
}
})
}
}
Loading