cleanup; revert string packing method

This commit is contained in:
Dan Laine 2020-06-14 18:15:44 -04:00
parent 9c4cfecf4e
commit d1796c8b0b
2 changed files with 20 additions and 36 deletions

View File

@ -284,16 +284,6 @@ func (p *Packer) PackStr(str string) {
p.PackFixedBytes([]byte(str)) 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 // UnpackStr unpacks a string from the byte array
func (p *Packer) UnpackStr() string { func (p *Packer) UnpackStr() string {
strSize := p.UnpackShort() strSize := p.UnpackShort()

View File

@ -97,7 +97,7 @@ func (c *codec) Marshal(value interface{}) ([]byte, error) {
return nil, errNil return nil, errNil
} }
funcs := make([]func(*wrappers.Packer) error, 512, 512) funcs := make([]func(*wrappers.Packer) error, 256, 256)
size, _, err := c.marshal(reflect.ValueOf(value), 0, &funcs) size, _, err := c.marshal(reflect.ValueOf(value), 0, &funcs)
if err != nil { if err != nil {
return nil, err return nil, err
@ -134,7 +134,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
switch valueKind { switch valueKind {
case reflect.Uint8: case reflect.Uint8:
size = 1 size = wrappers.ByteLen
funcsWritten = 1 funcsWritten = 1
asByte := byte(value.Uint()) asByte := byte(value.Uint())
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -143,7 +143,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.Int8: case reflect.Int8:
size = 1 size = wrappers.ByteLen
funcsWritten = 1 funcsWritten = 1
asByte := byte(value.Int()) asByte := byte(value.Int())
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -152,7 +152,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.Uint16: case reflect.Uint16:
size = 2 size = wrappers.ShortLen
funcsWritten = 1 funcsWritten = 1
asShort := uint16(value.Uint()) asShort := uint16(value.Uint())
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -161,7 +161,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.Int16: case reflect.Int16:
size = 2 size = wrappers.ShortLen
funcsWritten = 1 funcsWritten = 1
asShort := uint16(value.Int()) asShort := uint16(value.Int())
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -170,7 +170,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.Uint32: case reflect.Uint32:
size = 4 size = wrappers.IntLen
funcsWritten = 1 funcsWritten = 1
asInt := uint32(value.Uint()) asInt := uint32(value.Uint())
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -179,7 +179,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.Int32: case reflect.Int32:
size = 4 size = wrappers.IntLen
funcsWritten = 1 funcsWritten = 1
asInt := uint32(value.Int()) asInt := uint32(value.Int())
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -188,7 +188,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.Uint64: case reflect.Uint64:
size = 8 size = wrappers.LongLen
funcsWritten = 1 funcsWritten = 1
asInt := uint64(value.Uint()) asInt := uint64(value.Uint())
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -197,7 +197,7 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.Int64: case reflect.Int64:
size = 8 size = wrappers.LongLen
funcsWritten = 1 funcsWritten = 1
asInt := uint64(value.Int()) asInt := uint64(value.Int())
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -206,16 +206,17 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.String: case reflect.String:
// Note: it actually saves memory allocations to not do s := value.String()
// and use s in place of value.String(). Hence we don't do that.
funcsWritten = 1 funcsWritten = 1
asStr := value.String() size = len(value.String()) + wrappers.ShortLen
size = len(asStr) + wrappers.ShortLen
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
p.PackStrPtr(&asStr) p.PackStr(value.String())
return p.Err return p.Err
} }
return return
case reflect.Bool: case reflect.Bool:
size = 1 size = wrappers.BoolLen
funcsWritten = 1 funcsWritten = 1
asBool := value.Bool() asBool := value.Bool()
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
@ -232,13 +233,11 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
return 0, 0, fmt.Errorf("can't marshal unregistered type '%v'", reflect.TypeOf(underlyingValue).String()) return 0, 0, fmt.Errorf("can't marshal unregistered type '%v'", reflect.TypeOf(underlyingValue).String())
} }
(*funcs)[index] = nil
subsize, subFuncsWritten, subErr := c.marshal(value.Elem(), index+1, funcs) subsize, subFuncsWritten, subErr := c.marshal(value.Elem(), index+1, funcs)
if subErr != nil { if subErr != nil {
return 0, 0, subErr return 0, 0, subErr
} }
size = wrappers.IntLen + subsize
size = 4 + subsize // 4 because we pack the type ID, a uint32
(*funcs)[index] = func(p *wrappers.Packer) error { (*funcs)[index] = func(p *wrappers.Packer) error {
p.PackInt(typeID) p.PackInt(typeID)
return p.Err return p.Err
@ -287,23 +286,18 @@ func (c *codec) marshal(value reflect.Value, index int, funcs *[]func(*wrappers.
} }
return return
case reflect.Struct: case reflect.Struct:
t := value.Type() serializedFields, subErr := c.getSerializedFieldIndices(value.Type())
size = 0
fieldsMarshalled := 0
funcsWritten = 0
serializedFields, subErr := c.getSerializedFieldIndices(t)
if subErr != nil { if subErr != nil {
return 0, 0, subErr return 0, 0, subErr
} }
for _, f := range serializedFields { // Go through all fields of this struct size = 0
fieldVal := value.Field(f) // The field we're serializing funcsWritten = 0
subSize, n, err := c.marshal(fieldVal, index+funcsWritten, funcs) // Serialize the field for _, fieldIndex := range serializedFields { // Go through all fields of this struct
subSize, n, err := c.marshal(value.Field(fieldIndex), index+funcsWritten, funcs) // Serialize the field
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
fieldsMarshalled++
size += subSize size += subSize
funcsWritten += n funcsWritten += n
} }