Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Serialization code generator for QUICK struct content comparison

License

NotificationsYou must be signed in to change notification settings

CovenantSQL/HashStablePack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a code generation tool forQUICK struct content compare or hash computation.

中文介绍

For

  • 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.

How

Basically it will generate anMarshalHash method which follow theMessagePack Spec but :

  1. Without the struct key.
  2. Stable output of map.
  3. 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.

Why?

  • Use Go as your schema language
  • Performance

Why not?

  • 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

Quickstart

  1. Quick Install
go get -u github.com/CovenantSQL/HashStablePack/hsp
  1. Add tag for sourceIn a source file, include the following directive:
//go:generate hsp
  1. 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)

Features

  • Extremely fast generated code
  • Test and benchmark generation
  • Support for complex type declarations
  • Native support for Go'stime.Time,complex64, andcomplex128 types
  • Support for arbitrary type system extensions
  • File-based dependency model means fast codegen regardless of source tree size.

License

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

Stars

Watchers

Forks

Packages

No packages published

Contributors29


[8]ページ先頭

©2009-2025 Movatter.jp