Skip to content

Commit 9c3e156

Browse files
committed
cue: reuse adt.OpContext in MarshalJSON recursion
Create the context once in MarshalJSON and reuse it in appendJSON calls. │ old │ new │ │ sec/op │ sec/op vs base │ LargeValueMarshalJSON-8 7.664m ± 1% 6.484m ± 1% -15.39% (p=0.002 n=6) │ old │ new │ │ B/op │ B/op vs base │ LargeValueMarshalJSON-8 7.158Mi ± 0% 4.224Mi ± 0% -40.98% (p=0.002 n=6) │ old │ new │ │ allocs/op │ allocs/op vs base │ LargeValueMarshalJSON-8 70.28k ± 0% 62.27k ± 0% -11.40% (p=0.002 n=6) Updates #2470. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Id24eb441c19e5d6028cc7fa8e1eaf0d1551623e9 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201824 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 32b169a commit 9c3e156

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

cue/decode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (d *decoder) decode(x reflect.Value, v Value, isPtr bool) {
118118
ij, it, x := indirect(x, v.IsNull())
119119

120120
if ij != nil {
121-
b, err := v.appendJSON(nil)
121+
b, err := v.MarshalJSON()
122122
d.addErr(err)
123123
d.addErr(ij.UnmarshalJSON(b))
124124
return

cue/types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (o *structValue) appendJSON(b []byte) ([]byte, error) {
155155
}
156156
b = append(b, s...)
157157
b = append(b, ':')
158-
b, err = v.appendJSON(b)
158+
b, err = v.appendJSON(o.ctx, b)
159159
if err != nil {
160160
return nil, err
161161
}
@@ -299,7 +299,7 @@ func listAppendJSON(b []byte, l *Iterator) ([]byte, error) {
299299
if l.Next() {
300300
for i := 0; ; i++ {
301301
var err error
302-
b, err = l.Value().appendJSON(b)
302+
b, err = l.Value().appendJSON(l.ctx, b)
303303
if err != nil {
304304
return nil, err
305305
}
@@ -901,19 +901,19 @@ func (v Value) IncompleteKind() Kind {
901901

902902
// MarshalJSON marshalls this value into valid JSON.
903903
func (v Value) MarshalJSON() (b []byte, err error) {
904-
b, err = v.appendJSON(nil)
904+
ctx := newContext(v.idx)
905+
b, err = v.appendJSON(ctx, nil)
905906
if err != nil {
906907
return nil, unwrapJSONError(err)
907908
}
908909
return b, nil
909910
}
910911

911-
func (v Value) appendJSON(b []byte) ([]byte, error) {
912+
func (v Value) appendJSON(ctx *adt.OpContext, b []byte) ([]byte, error) {
912913
v, _ = v.Default()
913914
if v.v == nil {
914915
return append(b, "null"...), nil
915916
}
916-
ctx := newContext(v.idx)
917917
x := v.eval(ctx)
918918

919919
if _, ok := x.(adt.Resolver); ok {

0 commit comments

Comments
 (0)