Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit 8c4109c

Browse files
author
Emmanuel T Odeke
authored
all: remove unnecessary string([]byte) conversion in fmt.Sprintf yet %s (#663)
Noticed from Orijtech's continuous benchmarking product "Bencher" per https://dashboard.github.orijtech.com/benchmark/3245b8e4bbbd44a597480319aaa4b9fe that there is a bunch of code in the wild that invokes: fmt.Sprintf("%s", string([]byte(...))) yet the "%s" format specifier in fmt has a purpose: %s the uninterpreted bytes of the string or slice as well as using the "%c" format specifier which led to big improvements across every dimension: * CPU time reduction by 11+% (ns/op) * throughput improvement by 13+% (MBs/op) * allocations reduction by 45+% (B/op) * number of allocations reduction by 18+% (alloc/op) Also (*bytes.Buffer).String is invoked when "%q" or "%s" is passed in so no need to use it.
1 parent 50e09bb commit 8c4109c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

autorest/azure/azure.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ func (se ServiceError) Error() string {
6868
if err != nil {
6969
result += fmt.Sprintf(" Details=%v", se.Details)
7070
}
71-
result += fmt.Sprintf(" Details=%v", string(d))
71+
result += fmt.Sprintf(" Details=%s", d)
7272
}
7373

7474
if se.InnerError != nil {
7575
d, err := json.Marshal(se.InnerError)
7676
if err != nil {
7777
result += fmt.Sprintf(" InnerError=%v", se.InnerError)
7878
}
79-
result += fmt.Sprintf(" InnerError=%v", string(d))
79+
result += fmt.Sprintf(" InnerError=%s", d)
8080
}
8181

8282
if se.AdditionalInfo != nil {
8383
d, err := json.Marshal(se.AdditionalInfo)
8484
if err != nil {
8585
result += fmt.Sprintf(" AdditionalInfo=%v", se.AdditionalInfo)
8686
}
87-
result += fmt.Sprintf(" AdditionalInfo=%v", string(d))
87+
result += fmt.Sprintf(" AdditionalInfo=%s", d)
8888
}
8989

9090
return result
@@ -335,13 +335,13 @@ func WithErrorUnlessStatusCode(codes ...int) autorest.RespondDecorator {
335335
b, decodeErr := autorest.CopyAndDecode(encodedAs, resp.Body, &e)
336336
resp.Body = ioutil.NopCloser(&b)
337337
if decodeErr != nil {
338-
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b.String(), decodeErr)
338+
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b, decodeErr)
339339
}
340340
if e.ServiceError == nil {
341341
// Check if error is unwrapped ServiceError
342342
decoder := autorest.NewDecoder(encodedAs, bytes.NewReader(b.Bytes()))
343343
if err := decoder.Decode(&e.ServiceError); err != nil {
344-
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b.String(), err)
344+
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b, err)
345345
}
346346

347347
// for example, should the API return the literal value `null` as the response
@@ -364,7 +364,7 @@ func WithErrorUnlessStatusCode(codes ...int) autorest.RespondDecorator {
364364
rawBody := map[string]interface{}{}
365365
decoder := autorest.NewDecoder(encodedAs, bytes.NewReader(b.Bytes()))
366366
if err := decoder.Decode(&rawBody); err != nil {
367-
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b.String(), err)
367+
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b, err)
368368
}
369369

370370
e.ServiceError = &ServiceError{

0 commit comments

Comments
 (0)