Skip to content
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

Improve v4 Signer.SignHTTP & Signer.PresignHTTP performance #2956

Conversation

boekkooi-impossiblecloud

Good day,

This PR includes the following (minor) performance improvements when using Signer.SignHTTP & Signer.PresignHTTP.

use sha256.Sum256 to reduce allocations
benchstat sha-6636822440.txt 1.sha-62c09690f2.txt 
goos: linux
goarch: amd64
pkg: github.com/aws/aws-sdk-go-v2/aws/signer/v4
cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics     
                  │ sha-6636822440.txt │        1.sha-62c09690f2.txt        │
                  │       sec/op       │   sec/op     vs base               │
PresignRequest-16          10.58µ ± 1%   10.49µ ± 3%       ~ (p=0.128 n=10)
SignRequest-16             5.628µ ± 1%   5.564µ ± 2%       ~ (p=0.138 n=10)
geomean                    7.716µ        7.641µ       -0.98%

                  │ sha-6636822440.txt │        1.sha-62c09690f2.txt         │
                  │        B/op        │     B/op      vs base               │
PresignRequest-16         10.24Ki ± 0%   10.08Ki ± 0%  -1.53% (p=0.000 n=10)
SignRequest-16            5.073Ki ± 0%   4.917Ki ± 0%  -3.08% (p=0.000 n=10)
geomean                   7.208Ki        7.042Ki       -2.31%

                  │ sha-6636822440.txt │       1.sha-62c09690f2.txt        │
                  │     allocs/op      │ allocs/op   vs base               │
PresignRequest-16           84.00 ± 0%   82.00 ± 0%  -2.38% (p=0.000 n=10)
SignRequest-16              49.00 ± 0%   47.00 ± 0%  -4.08% (p=0.000 n=10)
geomean                     64.16        62.08       -3.24%
avoid []string allocation
benchstat 1.sha-62c09690f2.txt 2.sha-ccdf28e449.txt 
goos: linux
goarch: amd64
pkg: github.com/aws/aws-sdk-go-v2/aws/signer/v4
cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics     
                  │ 1.sha-62c09690f2.txt │        2.sha-ccdf28e449.txt        │
                  │        sec/op        │   sec/op     vs base               │
PresignRequest-16            10.49µ ± 3%   10.45µ ± 3%       ~ (p=0.837 n=10)
SignRequest-16               5.564µ ± 2%   5.534µ ± 2%       ~ (p=0.210 n=10)
geomean                      7.641µ        7.606µ       -0.45%

                  │ 1.sha-62c09690f2.txt │         2.sha-ccdf28e449.txt          │
                  │         B/op         │     B/op      vs base                 │
PresignRequest-16           10.08Ki ± 0%   10.08Ki ± 0%       ~ (p=1.000 n=10) ¹
SignRequest-16              4.917Ki ± 0%   4.917Ki ± 0%       ~ (p=1.000 n=10) ¹
geomean                     7.042Ki        7.042Ki       +0.00%
¹ all samples are equal

                  │ 1.sha-62c09690f2.txt │        2.sha-ccdf28e449.txt         │
                  │      allocs/op       │ allocs/op   vs base                 │
PresignRequest-16             82.00 ± 0%   82.00 ± 0%       ~ (p=1.000 n=10) ¹
SignRequest-16                47.00 ± 0%   47.00 ± 0%       ~ (p=1.000 n=10) ¹
geomean                       62.08        62.08       +0.00%
¹ all samples are equal
avoid unicode.AppendRune by using WriteByte
benchstat 2.sha-ccdf28e449.txt 3.sha-ac435cbd40.txt 
goos: linux
goarch: amd64
pkg: github.com/aws/aws-sdk-go-v2/aws/signer/v4
cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics     
                  │ 2.sha-ccdf28e449.txt │        3.sha-ac435cbd40.txt        │
                  │        sec/op        │   sec/op     vs base               │
PresignRequest-16            10.45µ ± 3%   10.40µ ± 2%       ~ (p=0.436 n=10)
SignRequest-16               5.534µ ± 2%   5.494µ ± 2%       ~ (p=0.197 n=10)
geomean                      7.606µ        7.559µ       -0.62%

                  │ 2.sha-ccdf28e449.txt │         3.sha-ac435cbd40.txt          │
                  │         B/op         │     B/op      vs base                 │
PresignRequest-16           10.08Ki ± 0%   10.08Ki ± 0%       ~ (p=1.000 n=10) ¹
SignRequest-16              4.917Ki ± 0%   4.917Ki ± 0%       ~ (p=1.000 n=10) ¹
geomean                     7.042Ki        7.042Ki       +0.00%
¹ all samples are equal

                  │ 2.sha-ccdf28e449.txt │        3.sha-ac435cbd40.txt         │
                  │      allocs/op       │ allocs/op   vs base                 │
PresignRequest-16             82.00 ± 0%   82.00 ± 0%       ~ (p=1.000 n=10) ¹
SignRequest-16                47.00 ± 0%   47.00 ± 0%       ~ (p=1.000 n=10) ¹
geomean                       62.08        62.08       +0.00%
¹ all samples are equal

allocate http.Header
benchstat 3.sha-ac435cbd40.txt 4.sha-a2ad4d675e.txt 
goos: linux
goarch: amd64
pkg: github.com/aws/aws-sdk-go-v2/aws/signer/v4
cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics     
                  │ 3.sha-ac435cbd40.txt │        4.sha-a2ad4d675e.txt        │
                  │        sec/op        │   sec/op     vs base               │
PresignRequest-16            10.40µ ± 2%   10.41µ ± 1%       ~ (p=0.927 n=10)
SignRequest-16               5.494µ ± 2%   5.489µ ± 2%       ~ (p=0.724 n=10)
geomean                      7.559µ        7.559µ       +0.00%

                  │ 3.sha-ac435cbd40.txt │         4.sha-a2ad4d675e.txt          │
                  │         B/op         │     B/op      vs base                 │
PresignRequest-16           10.08Ki ± 0%   10.08Ki ± 0%       ~ (p=1.000 n=10) ¹
SignRequest-16              4.917Ki ± 0%   4.917Ki ± 0%       ~ (p=1.000 n=10) ¹
geomean                     7.042Ki        7.042Ki       +0.00%
¹ all samples are equal

                  │ 3.sha-ac435cbd40.txt │        4.sha-a2ad4d675e.txt         │
                  │      allocs/op       │ allocs/op   vs base                 │
PresignRequest-16             82.00 ± 0%   82.00 ± 0%       ~ (p=1.000 n=10) ¹
SignRequest-16                47.00 ± 0%   47.00 ± 0%       ~ (p=1.000 n=10) ¹
geomean                       62.08        62.08       +0.00%
¹ all samples are equal

These changes result in the following benchstats results. (go test -bench=. -benchmem -count=10 -run=^$ ./aws/signer/v4/ was used to generate the reports.)

benchstat sha-6636822440.txt 4.sha-a2ad4d675e.txt 
goos: linux
goarch: amd64
pkg: github.com/aws/aws-sdk-go-v2/aws/signer/v4
cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics     
                  │ sha-6636822440.txt │        4.sha-a2ad4d675e.txt        │
                  │       sec/op       │   sec/op     vs base               │
PresignRequest-16          10.58µ ± 1%   10.41µ ± 1%  -1.60% (p=0.008 n=10)
SignRequest-16             5.628µ ± 1%   5.489µ ± 2%  -2.47% (p=0.001 n=10)
geomean                    7.716µ        7.559µ       -2.03%

                  │ sha-6636822440.txt │        4.sha-a2ad4d675e.txt         │
                  │        B/op        │     B/op      vs base               │
PresignRequest-16         10.24Ki ± 0%   10.08Ki ± 0%  -1.53% (p=0.000 n=10)
SignRequest-16            5.073Ki ± 0%   4.917Ki ± 0%  -3.08% (p=0.000 n=10)
geomean                   7.208Ki        7.042Ki       -2.31%

                  │ sha-6636822440.txt │       4.sha-a2ad4d675e.txt        │
                  │     allocs/op      │ allocs/op   vs base               │
PresignRequest-16           84.00 ± 0%   82.00 ± 0%  -2.38% (p=0.000 n=10)
SignRequest-16              49.00 ± 0%   47.00 ± 0%  -4.08% (p=0.000 n=10)
geomean                     64.16        62.08       -3.24%

Thank you for having a look at this PR and please let me know if there is any way I can improve it.
Have a great day!

@boekkooi-impossiblecloud boekkooi-impossiblecloud requested a review from a team as a code owner January 15, 2025 15:35
@lucix-aws
Copy link
Contributor

lucix-aws commented Jan 15, 2025

I'm not sure I'm sold on the microsecond/byte-level performance boost for what I think is a pretty drastic hit in terms of readability. Will let others chime in.

@lucix-aws
Copy link
Contributor

lucix-aws commented Jan 15, 2025

If you can express this in terms of some local utility you write that has the readability of Join but leverages string builders under the hood, I'd be more inclined to accept. I'm not even sure that the refactor to strings.Builder does anything, see my newer comment.

t := signingTime.ShortTimeFormat()

var sb strings.Builder
sb.Grow(len(t) + 1 + len(region) + 1 + len(service) + 1 + len(suffix))
Copy link
Contributor

Choose a reason for hiding this comment

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

In fact, is this actually an improvement? strings.Join uses strings.Builder internally.

Choose a reason for hiding this comment

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

It's a minor improvement of around -0.45% in the Benchmark.

I agree that it's a little less readable but I also expect this code to not change any time soon and in our solution we sign a lot for requests and every little improvement helps.

                  │ 1.sha-62c09690f2.txt │        2.sha-ccdf28e449.txt        │
                  │        sec/op        │   sec/op     vs base               │
PresignRequest-16            10.49µ ± 3%   10.45µ ± 3%       ~ (p=0.837 n=10)
SignRequest-16               5.564µ ± 2%   5.534µ ± 2%       ~ (p=0.210 n=10)
geomean                      7.641µ        7.606µ       -0.45%

However, I'm fine removing the related commit from the PR if it's a deal breaker.

Choose a reason for hiding this comment

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

FYI not using strings.Join has some precedence in lookupKey and buildAuthorizationHeader

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's older code and it's something we very much want to get away from. I made a point to do away with all of it in the rewrite in the aws-http-auth module.

@lucix-aws
Copy link
Contributor

lucix-aws commented Jan 15, 2025

Note that if you're using this code outside of the SDK to sign a lot of requests manually, you probably instead want https://pkg.go.dev/github.com/aws/smithy-go/aws-http-auth. That gives you sigv4 and sigv4a without an SDK dependency(although it doesn't have presigning yet).

@lucix-aws
Copy link
Contributor

Thanks again for submitting this pull request. We've discussed this as a team and have decided to close this PR, as we prefer the readability of the existing implementation. The changes apart from the removal of strings.Join do not appear to convey a significant performance boost.

@lucix-aws lucix-aws closed this Jan 17, 2025
@boekkooi-impossiblecloud
Copy link
Author

boekkooi-impossiblecloud commented Jan 18, 2025

Hey @lucix-aws ,

That's to bad. I would have hoped that at least use sha256.Sum256 to reduce allocations would be acceptable but no worries and have a great weekend!

P.S. use sha256.Sum256 to reduce allocations bench stats are below.

benchstat sha-6636822440.txt 1.sha-62c09690f2.txt 
goos: linux
goarch: amd64
pkg: github.com/aws/aws-sdk-go-v2/aws/signer/v4
cpu: AMD Ryzen 7 Pro 7735U with Radeon Graphics     
                  │ sha-6636822440.txt │        1.sha-62c09690f2.txt        │
                  │       sec/op       │   sec/op     vs base               │
PresignRequest-16          10.58µ ± 1%   10.49µ ± 3%       ~ (p=0.128 n=10)
SignRequest-16             5.628µ ± 1%   5.564µ ± 2%       ~ (p=0.138 n=10)
geomean                    7.716µ        7.641µ       -0.98%

                  │ sha-6636822440.txt │        1.sha-62c09690f2.txt         │
                  │        B/op        │     B/op      vs base               │
PresignRequest-16         10.24Ki ± 0%   10.08Ki ± 0%  -1.53% (p=0.000 n=10)
SignRequest-16            5.073Ki ± 0%   4.917Ki ± 0%  -3.08% (p=0.000 n=10)
geomean                   7.208Ki        7.042Ki       -2.31%

                  │ sha-6636822440.txt │       1.sha-62c09690f2.txt        │
                  │     allocs/op      │ allocs/op   vs base               │
PresignRequest-16           84.00 ± 0%   82.00 ± 0%  -2.38% (p=0.000 n=10)
SignRequest-16              49.00 ± 0%   47.00 ± 0%  -4.08% (p=0.000 n=10)
geomean                     64.16        62.08       -3.24%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants