-
Notifications
You must be signed in to change notification settings - Fork 228
Implement json.encode_indent #615
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
Conversation
See bazel.build/rules/lib/core/json#encode_indent Fixes google#614
adonovan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks.
lib/json/json.go
Outdated
| if err != nil { | ||
| return nil, err | ||
| } | ||
| buf := new(bytes.Buffer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strings.Builder
will save an allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json.Encode is built around growing a bytes.Buffer; I think making a version that appends to a strings.Builder is a bit out of scope of this pr (although potentially useful).
But you are right that allocating buf via new is a waste - fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I meant was that bytes.Buffer is more efficient when you want the final result as a []byte slice (buf.Bytes()), and strings.Builder is more efficient when you want the result as a string (buf.String()). (The difference is one allocation of a copy in each case.)
Since here we want String, we should use strings.Builder; no other change is needed.
BTW, allocating buf via new(T) or var x T in fact makes no difference: all that matters is whether the address of the variable escapes, which is independent of how you create the variable (unlike C++, where the syntax determines heap vs. stack allocation).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently starlark-go relies on json.Indent for indenting, which writes its output into a *bytes.Buffer: https://pkg.go.dev/encoding/json#Indent
Maybe I'm missing something obvious, but I don't see how it can be efficiently made to write into a strings.Builder.
BTW, allocating
bufvianew(T)orvar x Tin fact makes no difference
Thank you for correcting my wrong mental model of Go's allocator!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good point, the standard encoding/json package is indeed a constraint. This is fine.
See https://bazel.build/rules/lib/core/json#encode_indent
Fixes #614