Skip to content

Commit

Permalink
Replace fmt.Sprint with faster alternatives (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Dec 12, 2024
1 parent 83d3e55 commit 342293d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ linters-settings:
- tests
misspell:
locale: US
perfsprint:
int-conversion: false
err-error: false
errorf: true
sprintf1: false
strconcat: false
staticcheck:
checks: ["all", "-ST1000", "-ST1005"]

Expand All @@ -31,6 +37,7 @@ linters:
- govet
- ineffassign
- misspell
- perfsprint
- staticcheck
- typecheck
- unused
Expand Down
8 changes: 4 additions & 4 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,13 +693,13 @@ func (d *Decoder) convertValue(v reflect.Value, typ reflect.Type, src ast.Node)
// cast value to string
switch v.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return reflect.ValueOf(fmt.Sprint(v.Int())), nil
return reflect.ValueOf(strconv.FormatInt(v.Int(), 10)), nil
case reflect.Float32, reflect.Float64:
return reflect.ValueOf(fmt.Sprint(v.Float())), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return reflect.ValueOf(fmt.Sprint(v.Uint())), nil
return reflect.ValueOf(strconv.FormatUint(v.Uint(), 10)), nil
case reflect.Bool:
return reflect.ValueOf(fmt.Sprint(v.Bool())), nil
return reflect.ValueOf(strconv.FormatBool(v.Bool())), nil
}
if !v.Type().ConvertibleTo(typ) {
return reflect.Zero(typ), errors.ErrTypeMismatch(typ, v.Type(), src.GetToken())
Expand Down Expand Up @@ -953,7 +953,7 @@ func (d *Decoder) decodeByUnmarshaler(ctx context.Context, dst reflect.Value, sr
}
}

return fmt.Errorf("does not implemented Unmarshaler")
return errors.New("does not implemented Unmarshaler")
}

var (
Expand Down
6 changes: 3 additions & 3 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2098,13 +2098,13 @@ type unmarshalContext struct {
func (c *unmarshalContext) UnmarshalYAML(ctx context.Context, b []byte) error {
v, ok := ctx.Value("k").(int)
if !ok {
return fmt.Errorf("cannot get valid context")
return errors.New("cannot get valid context")
}
if v != 1 {
return fmt.Errorf("cannot get valid context")
return errors.New("cannot get valid context")
}
if string(b) != "1" {
return fmt.Errorf("cannot get valid bytes")
return errors.New("cannot get valid bytes")
}
c.v = v
return nil
Expand Down
6 changes: 3 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ func (e *Encoder) encodeNil() *ast.NullNode {
}

func (e *Encoder) encodeInt(v int64) *ast.IntegerNode {
value := fmt.Sprint(v)
value := strconv.FormatInt(v, 10)
return ast.Integer(token.New(value, value, e.pos(e.column)))
}

func (e *Encoder) encodeUint(v uint64) *ast.IntegerNode {
value := fmt.Sprint(v)
value := strconv.FormatUint(v, 10)
return ast.Integer(token.New(value, value, e.pos(e.column)))
}

Expand Down Expand Up @@ -556,7 +556,7 @@ func (e *Encoder) encodeString(v string, column int) *ast.StringNode {
}

func (e *Encoder) encodeBool(v bool) *ast.BoolNode {
value := fmt.Sprint(v)
value := strconv.FormatBool(v)
return ast.Bool(token.New(value, value, e.pos(e.column)))
}

Expand Down
5 changes: 3 additions & 2 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package yaml_test
import (
"bytes"
"context"
"errors"
"fmt"
"math"
"reflect"
Expand Down Expand Up @@ -1400,10 +1401,10 @@ type marshalContext struct{}
func (c *marshalContext) MarshalYAML(ctx context.Context) ([]byte, error) {
v, ok := ctx.Value("k").(int)
if !ok {
return nil, fmt.Errorf("cannot get valid context")
return nil, errors.New("cannot get valid context")
}
if v != 1 {
return nil, fmt.Errorf("cannot get valid context")
return nil, errors.New("cannot get valid context")
}
return []byte("1"), nil
}
Expand Down

0 comments on commit 342293d

Please sign in to comment.