-
Notifications
You must be signed in to change notification settings - Fork 18
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
Drop Options argument from user-declared methods and functions #163
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,35 +33,6 @@ var ( | |
// export exposes internal functionality of the "jsontext" package. | ||
var export = jsontext.Internal.Export(&internal.AllowInternalUse) | ||
|
||
// mayReuseOpt reuses coderOpts if joining opts with the coderOpts | ||
// would produce the equivalent set of options. | ||
func mayReuseOpt(coderOpts *jsonopts.Struct, opts []Options) *jsonopts.Struct { | ||
switch len(opts) { | ||
// In the common case, the caller plumbs down options from the caller's caller, | ||
// which is usually the [jsonopts.Struct] constructed by the top-level arshal call. | ||
case 1: | ||
o, _ := opts[0].(*jsonopts.Struct) | ||
if o == coderOpts { | ||
return coderOpts | ||
} | ||
// If the caller provides no options, then just reuse the coder's options, | ||
// which may contain both marshaling/unmarshaling and encoding/decoding flags. | ||
case 0: | ||
return coderOpts | ||
} | ||
return nil | ||
} | ||
|
||
var structOptionsPool = &sync.Pool{New: func() any { return new(jsonopts.Struct) }} | ||
|
||
func getStructOptions() *jsonopts.Struct { | ||
return structOptionsPool.Get().(*jsonopts.Struct) | ||
} | ||
func putStructOptions(o *jsonopts.Struct) { | ||
*o = jsonopts.Struct{} | ||
structOptionsPool.Put(o) | ||
} | ||
|
||
// Marshal serializes a Go value as a []byte according to the provided | ||
// marshal and encode options (while ignoring unmarshal or decode options). | ||
// It does not terminate the output with a newline. | ||
|
@@ -231,15 +202,13 @@ func MarshalWrite(out io.Writer, in any, opts ...Options) (err error) { | |
// See [Marshal] for details about the conversion of a Go value into JSON. | ||
func MarshalEncode(out *jsontext.Encoder, in any, opts ...Options) (err error) { | ||
xe := export.Encoder(out) | ||
mo := mayReuseOpt(&xe.Struct, opts) | ||
if mo == nil { | ||
mo = getStructOptions() | ||
defer putStructOptions(mo) | ||
*mo = xe.Struct // initialize with encoder options before joining | ||
mo.JoinWithoutCoderOptions(opts...) | ||
if len(opts) > 0 { | ||
optsOriginal := xe.Struct | ||
defer func() { xe.Struct = optsOriginal }() | ||
xe.Struct.JoinWithoutCoderOptions(opts...) | ||
} | ||
err = marshalEncode(out, in, mo) | ||
if err != nil && mo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) { | ||
err = marshalEncode(out, in, &xe.Struct) | ||
if err != nil && xe.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) { | ||
return internal.TransformMarshalError(in, err) | ||
} | ||
return err | ||
|
@@ -480,15 +449,13 @@ func unmarshalFull(in *jsontext.Decoder, out any, uo *jsonopts.Struct) error { | |
// See [Unmarshal] for details about the conversion of JSON into a Go value. | ||
func UnmarshalDecode(in *jsontext.Decoder, out any, opts ...Options) (err error) { | ||
xd := export.Decoder(in) | ||
uo := mayReuseOpt(&xd.Struct, opts) | ||
if uo == nil { | ||
uo = getStructOptions() | ||
defer putStructOptions(uo) | ||
*uo = xd.Struct // initialize with decoder options before joining | ||
uo.JoinWithoutCoderOptions(opts...) | ||
if len(opts) > 0 { | ||
optsOriginal := xd.Struct | ||
defer func() { xd.Struct = optsOriginal }() | ||
xd.Struct.JoinWithoutCoderOptions(opts...) | ||
} | ||
err = unmarshalDecode(in, out, uo) | ||
if err != nil && uo.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) { | ||
err = unmarshalDecode(in, out, &xd.Struct) | ||
if err != nil && xd.Flags.Get(jsonflags.ReportErrorsWithLegacySemantics) { | ||
return internal.TransformUnmarshalError(out, err) | ||
} | ||
return err | ||
|
@@ -544,6 +511,9 @@ func newAddressableValue(t reflect.Type) addressableValue { | |
return addressableValue{reflect.New(t).Elem(), true} | ||
} | ||
|
||
// TODO: Remove *jsonopts.Struct argument from [marshaler] and [unmarshaler]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this will speed up the performance of marshaling and unmarshaling since each recursive call doesn't need to keep passing the same *Struct pointer on the stack. |
||
// This can be directly accessed on the encoder or decoder. | ||
|
||
// All marshal and unmarshal behavior is implemented using these signatures. | ||
// The *jsonopts.Struct argument is guaranteed to identical to or at least | ||
// a strict super-set of the options in Encoder.Struct or Decoder.Struct. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yay! We get to delete one of the pools.