-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve encoding speed by caching types
Comparison to last commit db2c063: benchmark old ns/op new ns/op delta BenchmarkMarshal/Go_bool_to_CBOR_bool-2 93.4 88.5 -5.25% BenchmarkMarshal/Go_uint64_to_CBOR_positive_int-2 104 99.2 -4.62% BenchmarkMarshal/Go_int64_to_CBOR_negative_int-2 97.1 92.7 -4.53% BenchmarkMarshal/Go_float64_to_CBOR_float-2 102 97.2 -4.71% BenchmarkMarshal/Go_[]uint8_to_CBOR_bytes-2 131 121 -7.63% BenchmarkMarshal/Go_string_to_CBOR_text-2 122 121 -0.82% BenchmarkMarshal/Go_[]int_to_CBOR_array-2 529 480 -9.26% BenchmarkMarshal/Go_map[string]string_to_CBOR_map-2 2254 2194 -2.66
- Loading branch information
Showing
7 changed files
with
188 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cbor | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"sort" | ||
"sync" | ||
) | ||
|
||
type encodingStructType struct { | ||
fields fields | ||
canonicalFields fields | ||
err error | ||
} | ||
|
||
// byCanonicalRule sorts fields by field name length and field name. | ||
type byCanonicalRule struct { | ||
fields | ||
} | ||
|
||
func (s byCanonicalRule) Less(i, j int) bool { | ||
return bytes.Compare(s.fields[i].cborName, s.fields[j].cborName) <= 0 | ||
} | ||
|
||
var ( | ||
decodingStructTypeCache sync.Map // map[reflect.Type]fields | ||
encodingStructTypeCache sync.Map // map[reflect.Type]encodingStructType | ||
encodingTypeCache sync.Map // map[reflect.Type]encodeFunc | ||
) | ||
|
||
func getDecodingStructType(t reflect.Type) fields { | ||
if v, _ := decodingStructTypeCache.Load(t); v != nil { | ||
return v.(fields) | ||
} | ||
flds := getFields(t) | ||
for i := 0; i < len(flds); i++ { | ||
flds[i].isUnmarshaler = implementsUnmarshaler(flds[i].typ) | ||
} | ||
decodingStructTypeCache.Store(t, flds) | ||
return flds | ||
} | ||
|
||
func getEncodingStructType(t reflect.Type) encodingStructType { | ||
if v, _ := encodingStructTypeCache.Load(t); v != nil { | ||
return v.(encodingStructType) | ||
} | ||
|
||
flds := getFields(t) | ||
|
||
var err error | ||
es := getEncodeState() | ||
for i := 0; i < len(flds); i++ { | ||
ef := getEncodeFunc(flds[i].typ) | ||
if ef == nil { | ||
if err == nil { | ||
err = &UnsupportedTypeError{t} | ||
} | ||
} | ||
flds[i].ef = ef | ||
|
||
encodeTypeAndAdditionalValue(es, byte(cborTypeTextString), uint64(len(flds[i].name))) | ||
flds[i].cborName = make([]byte, es.Len()+len(flds[i].name)) | ||
copy(flds[i].cborName, es.Bytes()) | ||
copy(flds[i].cborName[es.Len():], flds[i].name) | ||
|
||
es.Reset() | ||
} | ||
putEncodeState(es) | ||
|
||
canonicalFields := make(fields, len(flds)) | ||
copy(canonicalFields, flds) | ||
sort.Sort(byCanonicalRule{canonicalFields}) | ||
|
||
structType := encodingStructType{fields: flds, canonicalFields: canonicalFields, err: err} | ||
encodingStructTypeCache.Store(t, structType) | ||
return structType | ||
} | ||
|
||
func getEncodeFunc(t reflect.Type) encodeFunc { | ||
if v, _ := encodingTypeCache.Load(t); v != nil { | ||
return v.(encodeFunc) | ||
} | ||
f := getEncodeFuncInternal(t) | ||
encodingTypeCache.Store(t, f) | ||
return f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.