pack pointer to string instead of string...halves memory footprint

This commit is contained in:
Dan Laine 2020-06-14 12:23:05 -04:00
parent ee1cf620a1
commit 9c4cfecf4e
2 changed files with 12 additions and 3 deletions

View File

@ -284,6 +284,16 @@ func (p *Packer) PackStr(str string) {
p.PackFixedBytes([]byte(str))
}
// PackStrPtr appends a string to the byte array
func (p *Packer) PackStrPtr(str *string) {
strSize := len(*str)
if strSize > MaxStringLen {
p.Add(errInvalidInput)
}
p.PackShort(uint16(strSize))
p.PackFixedBytes([]byte(*str))
}
// UnpackStr unpacks a string from the byte array
func (p *Packer) UnpackStr() string {
strSize := p.UnpackShort()

View File

@ -127,12 +127,11 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
// Case: Value can't be marshalled
switch valueKind {
case reflect.Interface, reflect.Ptr, reflect.Invalid:
if value.IsNil() { // Can't marshal nil or nil pointers
if value.IsNil() { // Can't marshal nil (except nil slices)
return 0, 0, errNil
}
}
// Case: Value is of known size; return its byte repr.
switch valueKind {
case reflect.Uint8:
size = 1
@ -211,7 +210,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
asStr := value.String()
size = len(asStr) + wrappers.ShortLen
(*funcs)[index] = func(p *wrappers.Packer) error {
p.PackStr(asStr)
p.PackStrPtr(&asStr)
return p.Err
}
return