feat: faster serialization (#157)

* refactor: improve deserialization performance

* refactor(serialization): simplify string conversion logic in Convert function

* fix(serialization): default value lookup

* refactor: add comment about concurrency in RegisterDefaultValueFactory

---------

Co-authored-by: yusing <yusing@6uo.me>
This commit is contained in:
Yuzerion
2025-10-02 20:30:31 +08:00
committed by GitHub
parent ef40793301
commit 4852efcf9c
10 changed files with 326 additions and 221 deletions

View File

@@ -6,7 +6,6 @@ import (
"strconv"
"testing"
"github.com/goccy/go-yaml"
"github.com/stretchr/testify/require"
expect "github.com/yusing/goutils/testing"
)
@@ -262,20 +261,6 @@ func TestStringToSlice(t *testing.T) {
})
}
func BenchmarkStringToSlice(b *testing.B) {
for range b.N {
dst := make([]int, 0)
_, _ = ConvertString("- 1\n- 2\n- 3", reflect.ValueOf(&dst))
}
}
func BenchmarkStringToSliceYAML(b *testing.B) {
for range b.N {
dst := make([]int, 0)
_ = yaml.Unmarshal([]byte("- 1\n- 2\n- 3"), &dst)
}
}
func TestStringToMap(t *testing.T) {
t.Run("yaml-like", func(t *testing.T) {
dst := make(map[string]string)
@@ -286,20 +271,6 @@ func TestStringToMap(t *testing.T) {
})
}
func BenchmarkStringToMap(b *testing.B) {
for range b.N {
dst := make(map[string]string)
_, _ = ConvertString(" a: b\n c: d", reflect.ValueOf(&dst))
}
}
func BenchmarkStringToMapYAML(b *testing.B) {
for range b.N {
dst := make(map[string]string)
_ = yaml.Unmarshal([]byte(" a: b\n c: d"), &dst)
}
}
func TestStringToStruct(t *testing.T) {
t.Run("yaml-like", func(t *testing.T) {
dst := struct {
@@ -335,23 +306,3 @@ autocert:
require.NoError(t, UnmarshalValidateYAML(data, &cfg))
require.Equal(t, "test", cfg.Autocert.Options.AuthToken)
}
func BenchmarkStringToStruct(b *testing.B) {
for range b.N {
dst := struct {
A string `json:"a"`
B int `json:"b"`
}{}
_, _ = ConvertString(" a: a\n b: 123", reflect.ValueOf(&dst))
}
}
func BenchmarkStringToStructYAML(b *testing.B) {
for range b.N {
dst := struct {
A string `yaml:"a"`
B int `yaml:"b"`
}{}
_ = yaml.Unmarshal([]byte(" a: a\n b: 123"), &dst)
}
}