Use zero-copy approach to convert types between string and byte… (#2206)

* Use zero-copy approach to convert types between string and byte slice

* Rename argument to a eligible one

Benchmark:

BenchmarkBytesConvBytesToStrRaw-4   	21003800	        70.9 ns/op	      96 B/op	       1 allocs/op
BenchmarkBytesConvBytesToStr-4      	1000000000	         0.333 ns/op	       0 B/op	       0 allocs/op
BenchmarkBytesConvStrToBytesRaw-4   	18478059	        59.3 ns/op	      96 B/op	       1 allocs/op
BenchmarkBytesConvStrToBytes-4      	1000000000	         0.373 ns/op	       0 B/op	       0 allocs/op


Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Andy Pan
2020-01-18 00:32:50 +08:00
committed by Bo-Yi Wu
parent ace6e4c2ea
commit 982daeb1ec
6 changed files with 130 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import (
"strings"
"time"
"github.com/gin-gonic/gin/internal/bytesconv"
"github.com/gin-gonic/gin/internal/json"
)
@ -208,9 +209,9 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
case time.Time:
return setTimeField(val, field, value)
}
return json.Unmarshal([]byte(val), value.Addr().Interface())
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
case reflect.Map:
return json.Unmarshal([]byte(val), value.Addr().Interface())
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
default:
return errUnknownType
}