this repo has no description
0
fork

Configure Feed

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

internal/core/adt: use strings.Builder for interpolations

The vast majority of interpolations are in string literals,
not in bytes literals, so avoid an allocation in the string case
by using a strings.Builder. This moves the extra allocation to
the bytes case, but that's fine as they are not as common.

│ old │ new │
│ B/op │ B/op vs base │
VetInventory 3.560Gi ± 0% 3.548Gi ± ∞ ¹ -0.33% (p=0.222 n=8+1)

│ old │ new │
│ allocs/op │ allocs/op vs base │
VetInventory 31.69M ± 0% 31.59M ± ∞ ¹ -0.33% (p=0.222 n=8+1)

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I701f3f724151bc2491838275a2cf961476833ca4
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1234030
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>

+8 -5
+8 -5
internal/core/adt/expr.go
··· 18 18 "bytes" 19 19 "fmt" 20 20 "math/big" 21 + "strings" 21 22 22 23 "github.com/cockroachdb/apd/v3" 23 24 ··· 1116 1117 } 1117 1118 1118 1119 func (x *Interpolation) evaluate(c *OpContext, state Flags) Value { 1119 - buf := bytes.Buffer{} 1120 + var sb strings.Builder 1120 1121 for _, e := range x.Parts { 1121 1122 v := c.value(e, Flags{ 1122 1123 status: partial, ··· 1124 1125 mode: yield, 1125 1126 }) 1126 1127 if x.K == BytesKind { 1127 - buf.Write(c.ToBytes(v)) 1128 + sb.Write(c.ToBytes(v)) 1128 1129 } else { 1129 - buf.WriteString(c.ToString(v)) 1130 + sb.WriteString(c.ToString(v)) 1130 1131 } 1131 1132 } 1132 1133 if err := c.Err(); err != nil { ··· 1140 1141 return err 1141 1142 } 1142 1143 if x.K == BytesKind { 1143 - return &Bytes{x.Src, buf.Bytes()} 1144 + // Interpolations in bytes literals are not very common; 1145 + // it's okay that we allocate twice in this case. 1146 + return &Bytes{x.Src, []byte(sb.String())} 1144 1147 } 1145 - return &String{x.Src, buf.String()} 1148 + return &String{x.Src, sb.String()} 1146 1149 } 1147 1150 1148 1151 // UnaryExpr is a unary expression.