- Notifications
You must be signed in to change notification settings - Fork5
Serialization code generator for QUICK struct content comparison
License
CovenantSQL/HashStablePack
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a code generation tool forQUICK struct content compare or hash computation.
Quick compare nested struct without reflection (10~20 times faster)
BenchmarkCompare/benchmark_reflect-810000020074ns/op//reflect.DeepEqualBenchmarkCompare/benchmark_hsp-85000002322ns/opBenchmarkCompare/benchmark_hsp_1_cached-810000001101ns/opBenchmarkCompare/benchmark_hsp_both_cached-810000000011.2ns/op
bench cases seehere
Quick calculation of struct hash or signature without reflection. used inCovenantSQL for block hash.
Basically it will generate anMarshalHash method which follow theMessagePack Spec but :
- Without the struct key.
- Stable output of map.
- Can be used to compare different type with same hsp tag.
That is the following 2 structs with different member name
For more: seetest cases
//go:generate hsptypePerson1struct {NamestringAgeintAddressstringMapmap[string]intunexportedbool// this field is ignoredUnexportedstring`hsp:"-"`// this field is ignored}typePerson2struct {NamestringAddressstringAgeintMap222map[string]int`hspack:"Map"`unexportedbool// this field is ignoredUnexportedstring`hsp:"-"`// this field is ignored}
But with the same name and content of exported member,MarshalHash will produce the same bytes array:
package personimport ("bytes""testing")funcTestMarshalHashAccountStable3(t*testing.T) {p1:=Person1{Name:"Auxten",Address:"@CovenantSQL.io",Age:70,Map:map[string]int{"ss":2,"s":1,"sss":3},unexported:false,}p2:=Person2{Name:"Auxten",Address:"@CovenantSQL.io",Age:70,Map222:map[string]int{"ss":2,"s":1,"sss":3},unexported:true,}bts1,err:=p1.MarshalHash()iferr!=nil {t.Fatal(err)}bts2,err:=p2.MarshalHash()iferr!=nil {t.Fatal(err)}if!bytes.Equal(bts1,bts2) {t.Fatal("hash not stable")}}
the order of struct member is sorted by struct tag (if not, use name)
You can read more about MessagePackin the wiki, or atmsgpack.org.
- Use Go as your schema language
- Performance
- MessagePack: member name is unnecessary, different implementation may add some fields which made result undetermined. And also golang's map...
- Prorobuf: struct must defined in proto language, and other limitations discussedhere
- Quick Install
go get -u github.com/CovenantSQL/HashStablePack/hsp
- Add tag for sourceIn a source file, include the following directive:
//go:generate hsp- Run go generate
go generate ./...
Thehsp command will generate serialization methods for all exported type declarations in the file.
By default, the code generator will only generateMarshalHash andMsgsize method
func (z*Test)MarshalHash() (o []byte,errerror)func (z*Test)Msgsize() (sint)
- Extremely fast generated code
- Test and benchmark generation
- Support for complex type declarations
- Native support for Go's
time.Time,complex64, andcomplex128types - Support for arbitrary type system extensions
- File-based dependency model means fast codegen regardless of source tree size.
This lib is inspired byhttps://github.com/tinylib/msgpMost Code is diverted fromhttps://github.com/tinylib/msgp, but It's an total different lib for usage. So I created a new project instead of forking it.
About
Serialization code generator for QUICK struct content comparison
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.