Skip to content

Conversation

prestonvasquez
Copy link
Member

@prestonvasquez prestonvasquez commented Sep 12, 2024

GODRIVER-2907

Summary

DRIVERS-2581 makes the mongocrypt_binary_t structure public, meaning we can initialize and assign the data and length directly:

mongocryptBinary := C.mongocrypt_binary_new() // Allocate memory 
if mongocryptBinary == nil {
	return nil
}

addr := (*C.uint8_t)(C.CBytes(data))

mongocryptBinary.data = unsafe.Pointer(addr)
mongocryptBinary.len = C.uint32_t(len(data))

Same with accessing the bytes:

C.GoBytes(unsafe.Pointer(b.wrapped.data), C.int(b.wrapped.len))

Initializing directly:

goos: darwin
goarch: arm64
pkg: go.mongodb.org/mongo-driver/v2/x/mongo/driver/mongocrypt
BenchmarkBulkDecryption
BenchmarkBulkDecryption/threadCount=1
BenchmarkBulkDecryption/threadCount=1-10                     536           2238040 ns/op           40984 B/op          3 allocs/op
testing: BenchmarkBulkDecryption/threadCount=1-10 left GOMAXPROCS set to 1
    binary_test.go:219: thread count: 1, median ops/sec: 447.20
BenchmarkBulkDecryption/threadCount=2
BenchmarkBulkDecryption/threadCount=2-10                    1036           1158261 ns/op           40984 B/op          3 allocs/op
testing: BenchmarkBulkDecryption/threadCount=2-10 left GOMAXPROCS set to 2
    binary_test.go:219: thread count: 2, median ops/sec: 855.48
BenchmarkBulkDecryption/threadCount=8
BenchmarkBulkDecryption/threadCount=8-10                    3078            377137 ns/op           40984 B/op          3 allocs/op
testing: BenchmarkBulkDecryption/threadCount=8-10 left GOMAXPROCS set to 8
    binary_test.go:219: thread count: 8, median ops/sec: 2579.93
BenchmarkBulkDecryption/threadCount=64
BenchmarkBulkDecryption/threadCount=64-10                   3235            356146 ns/op           40988 B/op          3 allocs/op
testing: BenchmarkBulkDecryption/threadCount=64-10 left GOMAXPROCS set to 64
    binary_test.go:219: thread count: 64, median ops/sec: 2718.71
PASS
ok      go.mongodb.org/mongo-driver/v2/x/mongo/driver/mongocrypt        53.710s

Using setter/accessor:

goos: darwin
goarch: arm64
pkg: go.mongodb.org/mongo-driver/v2/x/mongo/driver/mongocrypt
BenchmarkBulkDecryption
BenchmarkBulkDecryption/threadCount=1
BenchmarkBulkDecryption/threadCount=1-10                     541           2221445 ns/op           40984 B/op          3 allocs/op
testing: BenchmarkBulkDecryption/threadCount=1-10 left GOMAXPROCS set to 1
    binary_test.go:219: thread count: 1, median ops/sec: 449.99
BenchmarkBulkDecryption/threadCount=2
BenchmarkBulkDecryption/threadCount=2-10                    1027           1193342 ns/op           40984 B/op          3 allocs/op
testing: BenchmarkBulkDecryption/threadCount=2-10 left GOMAXPROCS set to 2
    binary_test.go:219: thread count: 2, median ops/sec: 858.41
BenchmarkBulkDecryption/threadCount=8
BenchmarkBulkDecryption/threadCount=8-10                    3458            332082 ns/op           40984 B/op          3 allocs/op
testing: BenchmarkBulkDecryption/threadCount=8-10 left GOMAXPROCS set to 8
    binary_test.go:219: thread count: 8, median ops/sec: 1928.17
BenchmarkBulkDecryption/threadCount=64
BenchmarkBulkDecryption/threadCount=64-10                   3604            330883 ns/op           40995 B/op          3 allocs/op
testing: BenchmarkBulkDecryption/threadCount=64-10 left GOMAXPROCS set to 64
    binary_test.go:219: thread count: 64, median ops/sec: 2997.64
PASS
ok      go.mongodb.org/mongo-driver/v2/x/mongo/driver/mongocrypt        53.582s

There does not appear to be extra cost to calling C from Go.

Background & Motivation

Reduce overhead of encryption callbacks by providing bulk-oriented encryption and decryption callbacks

@mongodb-drivers-pr-bot mongodb-drivers-pr-bot bot added the review-priority-low Low Priority PR for Review: within 3 business days label Sep 12, 2024
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Sep 12, 2024

API Change Report

No changes found!


b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
decryptDocForBench(b, crypt, encryptedDoc)
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't run for the prescribed 1 second because Go benchmarks are designed to automatically determine how long to run a benchmark to achieve stable results. From the docs:

The benchmark function must run the target code b.N times. It is called multiple times with b.N adjusted until the benchmark function lasts long enough to be timed reliably.

kevinAlbs
kevinAlbs previously approved these changes Sep 17, 2024
Copy link
Contributor

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

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

LGTM with minor comments.

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@prestonvasquez prestonvasquez merged commit 70d2a91 into mongodb:master Sep 23, 2024
31 of 33 checks passed
@prestonvasquez prestonvasquez deleted the GODRIVER-2907 branch September 23, 2024 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-priority-low Low Priority PR for Review: within 3 business days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants