Skip to content

Commit

Permalink
all: remove unnecessary string([]byte) conversion in fmt.Sprintf yet …
Browse files Browse the repository at this point in the history
…%s (Azure#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.
  • Loading branch information
Emmanuel T Odeke authored Dec 9, 2021
1 parent 50e09bb commit 8c4109c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions autorest/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ func (se ServiceError) Error() string {
if err != nil {
result += fmt.Sprintf(" Details=%v", se.Details)
}
result += fmt.Sprintf(" Details=%v", string(d))
result += fmt.Sprintf(" Details=%s", d)
}

if se.InnerError != nil {
d, err := json.Marshal(se.InnerError)
if err != nil {
result += fmt.Sprintf(" InnerError=%v", se.InnerError)
}
result += fmt.Sprintf(" InnerError=%v", string(d))
result += fmt.Sprintf(" InnerError=%s", d)
}

if se.AdditionalInfo != nil {
d, err := json.Marshal(se.AdditionalInfo)
if err != nil {
result += fmt.Sprintf(" AdditionalInfo=%v", se.AdditionalInfo)
}
result += fmt.Sprintf(" AdditionalInfo=%v", string(d))
result += fmt.Sprintf(" AdditionalInfo=%s", d)
}

return result
Expand Down Expand Up @@ -335,13 +335,13 @@ func WithErrorUnlessStatusCode(codes ...int) autorest.RespondDecorator {
b, decodeErr := autorest.CopyAndDecode(encodedAs, resp.Body, &e)
resp.Body = ioutil.NopCloser(&b)
if decodeErr != nil {
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b.String(), decodeErr)
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b, decodeErr)
}
if e.ServiceError == nil {
// Check if error is unwrapped ServiceError
decoder := autorest.NewDecoder(encodedAs, bytes.NewReader(b.Bytes()))
if err := decoder.Decode(&e.ServiceError); err != nil {
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b.String(), err)
return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b, err)
}

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

e.ServiceError = &ServiceError{
Expand Down

0 comments on commit 8c4109c

Please sign in to comment.