Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace fmt.Sprint with faster alternatives #555

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -676,13 +676,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 @@ -936,7 +936,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 @@ -2076,13 +2076,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 @@ -1398,10 +1399,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
4 changes: 2 additions & 2 deletions scanner/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scanner

import (
"fmt"
"errors"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -127,7 +127,7 @@ func (c *Context) validateDocumentLineIndentColumn() error {
return nil
}
if c.docFirstLineIndentColumn > c.docLineIndentColumn {
return fmt.Errorf("invalid number of indent is specified in the document header")
return errors.New("invalid number of indent is specified in the document header")
}
return nil
}
Expand Down