this repo has no description
0
fork

Configure Feed

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

internal/core/convert: use strings.ToValidUTF8 over x/text/encoding/unicode

The former is in the standard library since Go 1.13, it's simpler,
and most importantly, it doesn't allocate in the common case
where the input string is already valid UTF-8.

This is not unexpected; x/text/encoding/unicode has way more features,
so it's much harder for it to do nothing in the happy path.

│ old │ new │
│ B/op │ B/op vs base │
VetInventory 4.687Gi ± ∞ ¹ 4.627Gi ± ∞ ¹ -1.28% (p=1.000 n=1)

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

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

+4 -7
+4 -7
internal/core/convert/go.go
··· 24 24 "slices" 25 25 "strconv" 26 26 "strings" 27 + "unicode/utf8" 27 28 28 29 "github.com/cockroachdb/apd/v3" 29 - "golang.org/x/text/encoding/unicode" 30 30 31 31 "cuelang.org/go/cue/ast" 32 32 "cuelang.org/go/cue/ast/astutil" ··· 197 197 return false 198 198 } 199 199 200 - var utf8Enc = unicode.UTF8.NewEncoder() 201 - 202 200 func fromGoValue(ctx *adt.OpContext, nilIsTop bool, val reflect.Value) (result adt.Value) { 203 201 src := ctx.Source() 204 202 if !val.IsValid() { // untyped nil, or dereferencing a nil pointer/interface ··· 299 297 if err != nil { 300 298 return ctx.AddErr(errors.Promote(err, "encoding.TextMarshaler")) 301 299 } 302 - s, _ := utf8Enc.String(string(b)) 303 - return &adt.String{Src: src, Str: s} 300 + str := strings.ToValidUTF8(string(b), string(utf8.RuneError)) 301 + return &adt.String{Src: src, Str: str} 304 302 } 305 303 if _, ok := implements(typ, goError); ok { 306 304 v, _ := val.Interface().(error) // TODO(go1.25): use reflect.TypeAssert ··· 316 314 return ctx.NewBool(val.Bool()) 317 315 318 316 case reflect.String: 319 - str := val.String() 320 - str, _ = utf8Enc.String(str) 317 + str := strings.ToValidUTF8(val.String(), string(utf8.RuneError)) 321 318 // TODO: here and above: allow to fail on invalid strings. 322 319 // if !utf8.ValidString(str) { 323 320 // return ctx.AddErrf("cannot convert result to string: invalid UTF-8")