Skip to content

Commit 8a6ed2c

Browse files
committed
cue: avoid repeated work in Value.MarshalJSON with lists
Value.appendJSON already switches on the value kind and applies defaults so repeating that work in Value.List is a waste. Moreover, we already have an OpContext we can reuse. │ old │ new │ │ sec/op │ sec/op vs base │ LargeValueMarshalJSON-8 6.092m ± 1% 5.488m ± 1% -9.91% (p=0.002 n=6) │ old │ new │ │ B/op │ B/op vs base │ LargeValueMarshalJSON-8 3.445Mi ± 0% 2.360Mi ± 0% -31.49% (p=0.002 n=6) │ old │ new │ │ allocs/op │ allocs/op vs base │ LargeValueMarshalJSON-8 58.26k ± 0% 52.25k ± 0% -10.31% (p=0.002 n=6) Updates #2470. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ie180170d277c99a996a8e38a8a9e1c0b13e28154 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202101 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 8009b56 commit 8a6ed2c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

cue/types.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ func (v Value) appendJSON(ctx *adt.OpContext, b []byte) ([]byte, error) {
944944
b2, err := json.Marshal(x.(*adt.Bytes).B)
945945
return append(b, b2...), err
946946
case adt.ListKind:
947-
i, _ := v.List()
947+
i := v.mustList(ctx)
948948
return listAppendJSON(b, &i)
949949
case adt.StructKind:
950950
obj, err := v.structValData(ctx)
@@ -1327,13 +1327,19 @@ func (v Value) List() (Iterator, error) {
13271327
if err := v.checkKind(ctx, adt.ListKind); err != nil {
13281328
return Iterator{idx: v.idx, ctx: ctx}, v.toErr(err)
13291329
}
1330+
return v.mustList(ctx), nil
1331+
}
1332+
1333+
// mustList is like [Value.List], but reusing ctx and leaving it to the caller
1334+
// to apply defaults and check the kind.
1335+
func (v Value) mustList(ctx *adt.OpContext) Iterator {
13301336
arcs := []*adt.Vertex{}
13311337
for _, a := range v.v.Elems() {
13321338
if a.Label.IsInt() {
13331339
arcs = append(arcs, a)
13341340
}
13351341
}
1336-
return Iterator{idx: v.idx, ctx: ctx, val: v, arcs: arcs}, nil
1342+
return Iterator{idx: v.idx, ctx: ctx, val: v, arcs: arcs}
13371343
}
13381344

13391345
// Null reports an error if v is not null.

0 commit comments

Comments
 (0)