this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/core/adt: place CallContext on the stack

It is a small struct, weighing as much as seven pointers.
None of the methods need a pointer receiver either,
as they only read from the receiver value.
The extra allocation per func call can really add up
when doing many thousands of calls.

│ old │ new │
│ B/op │ B/op vs base │
VetInventory 4.725Gi ± ∞ ¹ 4.703Gi ± ∞ ¹ -0.48% (p=1.000 n=1)

│ old │ new │
│ allocs/op │ allocs/op vs base │
VetInventory 51.05M ± ∞ ¹ 49.86M ± ∞ ¹ -2.34% (p=1.000 n=1)

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I2d7b696ad78be327f2cfa260091cb769969d8169
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1229426
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Matthew Sackman <matthew@cue.works>

+33 -33
+1 -1
cue/interpreter/wasm/builtin.go
··· 55 55 call := &adt.CallExpr{Fun: &adt.Builtin{ 56 56 Result: adt.TopKind, 57 57 Name: name, 58 - Func: func(call *adt.CallContext) adt.Expr { 58 + Func: func(call adt.CallContext) adt.Expr { 59 59 opctx := call.OpContext() 60 60 61 61 scope := value.Make(opctx, scope)
+9 -9
internal/core/adt/call.go
··· 28 28 isValidator bool 29 29 } 30 30 31 - func (c *CallContext) OpContext() *OpContext { 31 + func (c CallContext) OpContext() *OpContext { 32 32 return c.ctx 33 33 } 34 34 35 - func (c *CallContext) Pos() token.Pos { 35 + func (c CallContext) Pos() token.Pos { 36 36 if c.call != nil { 37 37 return Pos(c.call) 38 38 } 39 39 return Pos(c.builtin) 40 40 } 41 41 42 - func (c *CallContext) Value(i int) Value { 42 + func (c CallContext) Value(i int) Value { 43 43 return c.args[i] 44 44 } 45 45 46 46 // NumParams returns the total number of parameters to this function. 47 - func (c *CallContext) NumParams() int { 47 + func (c CallContext) NumParams() int { 48 48 return len(c.args) 49 49 } 50 50 51 - func (c *CallContext) AddPositions(err *ValueError) { 51 + func (c CallContext) AddPositions(err *ValueError) { 52 52 for _, v := range c.args { 53 53 err.AddPosition(v) 54 54 } ··· 57 57 // Args return the pre-evaluated arguments. This function is only used for 58 58 // transitioning and will be removed at some point. Use [CallContext.Value] 59 59 // instead. 60 - func (c *CallContext) Args() []Value { 60 + func (c CallContext) Args() []Value { 61 61 return c.args 62 62 } 63 63 ··· 67 67 // 68 68 // This method of getting an argument should be used when the argument is used 69 69 // as a schema and may contain cycles. 70 - func (c *CallContext) Arg(i int) Value { 70 + func (c CallContext) Arg(i int) Value { 71 71 // If the call context represents a validator call, the argument will be 72 72 // offset by 1. 73 73 if c.isValidator { ··· 84 84 } 85 85 86 86 // Expr returns the nth argument expression without evaluating it. 87 - func (c *CallContext) Expr(i int) Expr { 87 + func (c CallContext) Expr(i int) Expr { 88 88 // If the call context represents a validator call, the argument will be 89 89 // offset by 1. 90 90 if c.isValidator { ··· 99 99 return x 100 100 } 101 101 102 - func (c *CallContext) Errf(format string, args ...interface{}) *Bottom { 102 + func (c CallContext) Errf(format string, args ...interface{}) *Bottom { 103 103 return c.ctx.NewErrf(format, args...) 104 104 }
+6 -6
internal/core/adt/expr.go
··· 1396 1396 } 1397 1397 1398 1398 func (x *CallExpr) evaluate(c *OpContext, state Flags) Value { 1399 - call := &CallContext{ 1399 + call := CallContext{ 1400 1400 ctx: c, 1401 1401 call: x, 1402 1402 } ··· 1529 1529 // arguments. By default, all arguments are checked to be concrete. 1530 1530 NonConcrete bool 1531 1531 1532 - Func func(call *CallContext) Expr 1532 + Func func(call CallContext) Expr 1533 1533 1534 1534 // RawFunc gives low-level control to CUE's internals for builtins. 1535 1535 // It should be used when fine control over the evaluation process is ··· 1539 1539 // the Context. 1540 1540 // 1541 1541 // TODO: consider merging Func and RawFunc into a single field again. 1542 - RawFunc func(call *CallContext) Value 1542 + RawFunc func(call CallContext) Value 1543 1543 1544 1544 // Added indicates as of which language version this builtin can be used. 1545 1545 Added string ··· 1623 1623 return true 1624 1624 } 1625 1625 1626 - func (x *Builtin) call(call *CallContext) Expr { 1626 + func (x *Builtin) call(call CallContext) Expr { 1627 1627 c := call.ctx 1628 1628 p := call.Pos() 1629 1629 ··· 1715 1715 args[0] = v 1716 1716 copy(args[1:], x.Args) 1717 1717 1718 - call := &CallContext{ 1718 + call := CallContext{ 1719 1719 ctx: c, 1720 1720 call: x.Src, 1721 1721 builtin: x.Builtin, ··· 1726 1726 return validateWithBuiltin(call) 1727 1727 } 1728 1728 1729 - func validateWithBuiltin(call *CallContext) *Bottom { 1729 + func validateWithBuiltin(call CallContext) *Bottom { 1730 1730 var severeness ErrorCode 1731 1731 var err errors.Error 1732 1732
+12 -12
internal/core/compile/builtin.go
··· 43 43 44 44 Params: []adt.Param{stringParam}, 45 45 Result: adt.BottomKind, 46 - RawFunc: func(call *adt.CallContext) adt.Value { 46 + RawFunc: func(call adt.CallContext) adt.Value { 47 47 ctx := call.OpContext() 48 48 arg := call.Expr(0) 49 49 ··· 86 86 Name: "len", 87 87 Params: []adt.Param{{Value: &adt.BasicType{K: supportedByLen}}}, 88 88 Result: adt.IntKind, 89 - Func: func(call *adt.CallContext) adt.Expr { 89 + Func: func(call adt.CallContext) adt.Expr { 90 90 c := call.OpContext() 91 91 args := call.Args() 92 92 ··· 135 135 Name: "close", 136 136 Params: []adt.Param{structParam}, 137 137 Result: adt.StructKind, 138 - Func: func(call *adt.CallContext) adt.Expr { 138 + Func: func(call adt.CallContext) adt.Expr { 139 139 c := call.OpContext() 140 140 args := call.Args() 141 141 ··· 157 157 Name: "__closeAll", 158 158 Params: []adt.Param{topParam}, 159 159 Result: adt.TopKind, 160 - Func: func(call *adt.CallContext) adt.Expr { 160 + Func: func(call adt.CallContext) adt.Expr { 161 161 c := call.OpContext() 162 162 163 163 x := call.Expr(0) ··· 189 189 Name: "__reclose", 190 190 Params: []adt.Param{topParam}, 191 191 Result: adt.TopKind, 192 - Func: func(call *adt.CallContext) adt.Expr { 192 + Func: func(call adt.CallContext) adt.Expr { 193 193 c := call.OpContext() 194 194 195 195 x := call.Expr(0) ··· 224 224 Name: "and", 225 225 Params: []adt.Param{listParam}, 226 226 Result: adt.IntKind, 227 - Func: func(call *adt.CallContext) adt.Expr { 227 + Func: func(call adt.CallContext) adt.Expr { 228 228 c := call.OpContext() 229 229 args := call.Args() 230 230 ··· 245 245 Params: []adt.Param{listParam}, 246 246 Result: adt.IntKind, 247 247 NonConcrete: true, 248 - Func: func(call *adt.CallContext) adt.Expr { 248 + Func: func(call adt.CallContext) adt.Expr { 249 249 c := call.OpContext() 250 250 args := call.Args() 251 251 ··· 283 283 Name: "div", 284 284 Params: []adt.Param{intParam, intParam}, 285 285 Result: adt.IntKind, 286 - Func: func(call *adt.CallContext) adt.Expr { 286 + Func: func(call adt.CallContext) adt.Expr { 287 287 c := call.OpContext() 288 288 args := call.Args() 289 289 ··· 297 297 Name: "mod", 298 298 Params: []adt.Param{intParam, intParam}, 299 299 Result: adt.IntKind, 300 - Func: func(call *adt.CallContext) adt.Expr { 300 + Func: func(call adt.CallContext) adt.Expr { 301 301 c := call.OpContext() 302 302 args := call.Args() 303 303 ··· 311 311 Name: "quo", 312 312 Params: []adt.Param{intParam, intParam}, 313 313 Result: adt.IntKind, 314 - Func: func(call *adt.CallContext) adt.Expr { 314 + Func: func(call adt.CallContext) adt.Expr { 315 315 c := call.OpContext() 316 316 args := call.Args() 317 317 ··· 325 325 Name: "rem", 326 326 Params: []adt.Param{intParam, intParam}, 327 327 Result: adt.IntKind, 328 - Func: func(call *adt.CallContext) adt.Expr { 328 + Func: func(call adt.CallContext) adt.Expr { 329 329 c := call.OpContext() 330 330 args := call.Args() 331 331 ··· 352 352 Name: "testExperiment", 353 353 Params: []adt.Param{topParam}, 354 354 Result: adt.TopKind, 355 - Func: func(call *adt.CallContext) adt.Expr { 355 + Func: func(call adt.CallContext) adt.Expr { 356 356 args := call.Args() 357 357 358 358 if call.Pos().Experiment().Testing {
+2 -2
internal/core/compile/validator.go
··· 29 29 Params: []adt.Param{topParam, intParam, listParam}, // varargs 30 30 Result: adt.BoolKind, 31 31 NonConcrete: true, 32 - Func: func(call *adt.CallContext) adt.Expr { 32 + Func: func(call adt.CallContext) adt.Expr { 33 33 c := call.OpContext() 34 34 args := call.Args() 35 35 ··· 88 88 Params: []adt.Param{topParam, topParam, topParam, topParam}, 89 89 Result: adt.BoolKind, 90 90 NonConcrete: true, 91 - Func: func(call *adt.CallContext) adt.Expr { 91 + Func: func(call adt.CallContext) adt.Expr { 92 92 c := call.OpContext() 93 93 args := call.Args() 94 94
+1 -1
internal/core/runtime/extern_test.go
··· 119 119 120 120 call := &adt.CallExpr{Fun: &adt.Builtin{ 121 121 Result: adt.TopKind, 122 - Func: func(call *adt.CallContext) adt.Expr { 122 + Func: func(call adt.CallContext) adt.Expr { 123 123 opctx := call.OpContext() 124 124 125 125 cuectx := (*cue.Context)(c.runtime)
+1 -1
internal/pkg/builtin.go
··· 130 130 Package: b.Pkg, 131 131 Name: b.Name, 132 132 } 133 - x.Func = func(call *adt.CallContext) (ret adt.Expr) { 133 + x.Func = func(call adt.CallContext) (ret adt.Expr) { 134 134 ctx := call.OpContext() 135 135 args := call.Args() 136 136
+1 -1
internal/pkg/context.go
··· 28 28 29 29 // CallCtxt is passed to builtin implementations that need to use a cue.Value. This is an internal type. Its interface may change. 30 30 type CallCtxt struct { 31 - *adt.CallContext 31 + adt.CallContext 32 32 ctx *adt.OpContext 33 33 builtin *Builtin 34 34 Err any