godaddy / asherah

Asherah is a multi-language, cross-platform application encryption SDK
MIT License
75 stars 47 forks source link

[go] KeyCache Locking Improvements #1006

Closed aka-bo closed 9 months ago

aka-bo commented 9 months ago

To help us get this pull request reviewed and merged quickly, please be sure to include the following items:

PR Type

What kind of change does this PR introduce?

Backward Compatibility

Is this change backward compatible with the most recently released version? Does it introduce changes which might change the user experience in any way? Does it alter the API in any way?

What's new?

Improved overall performance of KeyCache by ~26%. This was accomplished by implementing a fast path in GetOrLoad by using an RLock for the initial cache read.

Benchmarks

go test -race -bench='^BenchmarkKeyCache_*' -run=^$ -v --tags=race_tests -cpu=12 -count 6 github.com/godaddy/asherah/go/appencryption | tee benchmark.v0.4.0.out

applied changes and rerun...

go test -race -bench='^BenchmarkKeyCache_*' -run=^$ -v --tags=race_tests -cpu=12 -count 6 github.com/godaddy/asherah/go/appencryption | tee benchmark.v0.4.1.out

compared using benchstat...

$ benchstat benchmark.v0.4.0.out benchmark.v0.4.1.out
goos: darwin
goarch: arm64
pkg: github.com/godaddy/asherah/go/appencryption
                                                               │ benchmark.v0.4.0.out │        benchmark.v0.4.1.out        │
                                                               │        sec/op        │   sec/op     vs base               │
KeyCache_GetOrLoad_MultipleThreadsReadExistingKey-12                      5.649µ ± 2%   2.276µ ± 8%  -59.72% (p=0.002 n=6)
KeyCache_GetOrLoad_MultipleThreadsWriteSameKey-12                         7.548µ ± 2%   3.966µ ± 1%  -47.46% (p=0.002 n=6)
KeyCache_GetOrLoad_MultipleThreadsWriteUniqueKeys-12                      28.60µ ± 1%   32.86µ ± 3%  +14.90% (p=0.002 n=6)
KeyCache_GetOrLoad_MultipleThreadsReadRevokedKey-12                       7.780µ ± 2%   5.384µ ± 2%  -30.80% (p=0.002 n=6)
KeyCache_GetOrLoad_MultipleThreadsRead_NeedReloadKey-12                   4.105µ ± 1%   1.797µ ± 2%  -56.22% (p=0.002 n=6)
KeyCache_GetOrLoad_MultipleThreadsReadUniqueKeys-12                       7.109µ ± 4%   2.582µ ± 1%  -63.68% (p=0.002 n=6)
KeyCache_GetOrLoadLatest_MultipleThreadsReadExistingKey-12                9.470µ ± 1%   9.471µ ± 1%        ~ (p=0.937 n=6)
KeyCache_GetOrLoadLatest_MultipleThreadsWriteSameKey-12                   13.90µ ± 1%   13.81µ ± 1%        ~ (p=0.420 n=6)
KeyCache_GetOrLoadLatest_MultipleThreadsWriteUniqueKey-12                 21.63µ ± 1%   21.68µ ± 1%        ~ (p=0.818 n=6)
KeyCache_GetOrLoadLatest_MultipleThreadsReadStaleRevokedKey-12            375.9µ ± 1%   374.6µ ± 2%        ~ (p=0.818 n=6)
KeyCache_GetOrLoadLatest_MultipleThreadsReadRevokedKey-12                 8.244µ ± 3%   8.192µ ± 3%        ~ (p=0.818 n=6)
KeyCache_GetOrLoadLatest_MultipleThreadsReadUniqueKeys-12                 12.59µ ± 2%   12.56µ ± 3%        ~ (p=0.734 n=6)
geomean                                                                   13.30µ        9.826µ       -26.14%