Skip to content

Commit b7845f8

Browse files
committed
oauth2: use strings.Builder instead of bytes.Buffer
The former does not make a copy of the accumulated buffer to produce a string. WriteByte() is faster than WriteRune() and we are not appending non-ASCII here.
1 parent cf14319 commit b7845f8

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

google/externalaccount/aws.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package externalaccount
66

77
import (
8-
"bytes"
98
"context"
109
"crypto/hmac"
1110
"crypto/sha256"
@@ -148,13 +147,13 @@ func canonicalHeaders(req *http.Request) (string, string) {
148147
}
149148
sort.Strings(headers)
150149

151-
var fullHeaders bytes.Buffer
150+
var fullHeaders strings.Builder
152151
for _, header := range headers {
153152
headerValue := strings.Join(lowerCaseHeaders[header], ",")
154153
fullHeaders.WriteString(header)
155-
fullHeaders.WriteRune(':')
154+
fullHeaders.WriteByte(':')
156155
fullHeaders.WriteString(headerValue)
157-
fullHeaders.WriteRune('\n')
156+
fullHeaders.WriteByte('\n')
158157
}
159158

160159
return strings.Join(headers, ";"), fullHeaders.String()

oauth2.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package oauth2 // import "golang.org/x/oauth2"
1010

1111
import (
12-
"bytes"
1312
"context"
1413
"errors"
1514
"net/http"
@@ -158,7 +157,7 @@ func SetAuthURLParam(key, value string) AuthCodeOption {
158157
// PKCE), https://www.oauth.com/oauth2-servers/pkce/ and
159158
// https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-09.html#name-cross-site-request-forgery (describing both approaches)
160159
func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string {
161-
var buf bytes.Buffer
160+
var buf strings.Builder
162161
buf.WriteString(c.Endpoint.AuthURL)
163162
v := url.Values{
164163
"response_type": {"code"},

0 commit comments

Comments
 (0)