diff --git a/README.md b/README.md index 50d6727..70e45fa 100644 --- a/README.md +++ b/README.md @@ -234,11 +234,10 @@ $ go test -cpuprofile cpu.prof -memprofile mem.prof -bench . -benchmem ``` ``` goos: linux -goarch: amd64 +goarch: arm64 pkg: github.com/encodingx/binary -cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz -BenchmarkMarshal 2145660 552.5 ns/op 64 B/op 6 allocs/op -BenchmarkUnmarshal 1878840 655.3 ns/op 64 B/op 8 allocs/op +BenchmarkMarshal-2 3435181 349.9 ns/op 64 B/op 6 allocs/op +BenchmarkUnmarshal-2 3004425 396.4 ns/op 64 B/op 8 allocs/op PASS -ok github.com/encodingx/binary 3.873s +ok github.com/encodingx/binary 3.288s ``` diff --git a/binary.go b/binary.go index 0a08e4c..39461b4 100644 --- a/binary.go +++ b/binary.go @@ -5,15 +5,12 @@ import ( "fmt" "io" + "github.com/encodingx/binary/internal/codecs" "github.com/encodingx/binary/internal/validation" ) -const ( - wordLengthUpperLimitBytes = 8 -) - var ( - defaultCodec = newCodec() + defaultCodec codecs.Codec = codecs.NewCodec() ) func Marshal(iface interface{}) (bytes []byte, e error) { @@ -22,7 +19,7 @@ func Marshal(iface interface{}) (bytes []byte, e error) { ) var ( - operation codecOperation + operation codecs.CodecOperation ) defer func() { @@ -39,12 +36,12 @@ func Marshal(iface interface{}) (bytes []byte, e error) { return }() - operation, e = defaultCodec.newOperation(iface) + operation, e = defaultCodec.NewOperation(iface) if e != nil { return } - bytes, e = operation.marshal() + bytes, e = operation.Marshal() if e != nil { return } @@ -58,7 +55,7 @@ func Unmarshal(bytes []byte, iface interface{}) (e error) { ) var ( - operation codecOperation + operation codecs.CodecOperation ) defer func() { @@ -75,12 +72,12 @@ func Unmarshal(bytes []byte, iface interface{}) (e error) { return }() - operation, e = defaultCodec.newOperation(iface) + operation, e = defaultCodec.NewOperation(iface) if e != nil { return } - e = operation.unmarshal(bytes) + e = operation.Unmarshal(bytes) if e != nil { return } diff --git a/codec.go b/internal/codecs/codec.go similarity index 52% rename from codec.go rename to internal/codecs/codec.go index bad36b9..8aec8ef 100644 --- a/codec.go +++ b/internal/codecs/codec.go @@ -1,25 +1,26 @@ -package binary +package codecs import ( "reflect" + "github.com/encodingx/binary/internal/codecs/metadata" "github.com/encodingx/binary/internal/validation" ) -type codec struct { - formatMetadataCache map[reflect.Type]formatMetadata +type Codec struct { + formatMetadataCache map[reflect.Type]metadata.FormatMetadata } -func newCodec() (c codec) { - c = codec{ - formatMetadataCache: make(map[reflect.Type]formatMetadata), +func NewCodec() (c Codec) { + c = Codec{ + formatMetadataCache: make(map[reflect.Type]metadata.FormatMetadata), } return } -func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) ( - format formatMetadata, e error, +func (c Codec) formatMetadataFromTypeReflection(reflection reflect.Type) ( + format metadata.FormatMetadata, e error, ) { var ( inCache bool @@ -43,7 +44,7 @@ func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) ( return } - format, e = newFormatMetadataFromTypeReflection( + format, e = metadata.NewFormatMetadataFromTypeReflection( reflection.Elem(), ) if e != nil { @@ -55,8 +56,8 @@ func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) ( return } -func (c codec) newOperation(iface interface{}) ( - operation codecOperation, e error, +func (c Codec) NewOperation(iface interface{}) ( + operation CodecOperation, e error, ) { operation.format, e = c.formatMetadataFromTypeReflection( reflect.TypeOf(iface), @@ -70,21 +71,21 @@ func (c codec) newOperation(iface interface{}) ( return } -type codecOperation struct { - format formatMetadata +type CodecOperation struct { + format metadata.FormatMetadata valueReflection reflect.Value } -func (c codecOperation) marshal() (bytes []byte, e error) { - bytes = c.format.marshal(c.valueReflection) +func (c CodecOperation) Marshal() (bytes []byte, e error) { + bytes = c.format.Marshal(c.valueReflection) return } -func (c codecOperation) unmarshal(bytes []byte) (e error) { - if len(bytes) != c.format.lengthInBytes { +func (c CodecOperation) Unmarshal(bytes []byte) (e error) { + if len(bytes) != c.format.LengthInBytes() { e = validation.NewLengthOfByteSliceNotEqualToFormatLengthError( - uint(c.format.lengthInBytes), + uint(c.format.LengthInBytes()), uint(len(bytes)), ) @@ -95,7 +96,7 @@ func (c codecOperation) unmarshal(bytes []byte) (e error) { return } - c.format.unmarshal(bytes, c.valueReflection) + c.format.Unmarshal(bytes, c.valueReflection) return } diff --git a/metadata-bit-field.go b/internal/codecs/metadata/bit-field.go similarity index 99% rename from metadata-bit-field.go rename to internal/codecs/metadata/bit-field.go index 1712b0b..6e17388 100644 --- a/metadata-bit-field.go +++ b/internal/codecs/metadata/bit-field.go @@ -1,4 +1,4 @@ -package binary +package metadata import ( "encoding/binary" diff --git a/internal/codecs/metadata/constants.go b/internal/codecs/metadata/constants.go new file mode 100644 index 0000000..eb5e073 --- /dev/null +++ b/internal/codecs/metadata/constants.go @@ -0,0 +1,5 @@ +package metadata + +const ( + wordLengthUpperLimitBytes = 8 +) diff --git a/metadata-format.go b/internal/codecs/metadata/format.go similarity index 78% rename from metadata-format.go rename to internal/codecs/metadata/format.go index 0bd48e6..6e959b0 100644 --- a/metadata-format.go +++ b/internal/codecs/metadata/format.go @@ -1,4 +1,4 @@ -package binary +package metadata import ( "reflect" @@ -6,13 +6,13 @@ import ( "github.com/encodingx/binary/internal/validation" ) -type formatMetadata struct { +type FormatMetadata struct { words []wordMetadata lengthInBytes int } -func newFormatMetadataFromTypeReflection(reflection reflect.Type) ( - format formatMetadata, e error, +func NewFormatMetadataFromTypeReflection(reflection reflect.Type) ( + format FormatMetadata, e error, ) { var ( i int @@ -32,7 +32,7 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) ( return } - format = formatMetadata{ + format = FormatMetadata{ words: make([]wordMetadata, reflection.NumField(), ), @@ -52,7 +52,7 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) ( return } -func (m formatMetadata) marshal(reflection reflect.Value) (bytes []byte) { +func (m FormatMetadata) Marshal(reflection reflect.Value) (bytes []byte) { // Merge byte slices marshalled from words, // in the order they appear in the format. @@ -78,7 +78,7 @@ func (m formatMetadata) marshal(reflection reflect.Value) (bytes []byte) { return } -func (m formatMetadata) unmarshal(bytes []byte, reflection reflect.Value) { +func (m FormatMetadata) Unmarshal(bytes []byte, reflection reflect.Value) { var ( i int j int @@ -105,3 +105,7 @@ func (m formatMetadata) unmarshal(bytes []byte, reflection reflect.Value) { return } + +func (m FormatMetadata) LengthInBytes() int { + return m.lengthInBytes +} diff --git a/metadata-word.go b/internal/codecs/metadata/word.go similarity index 99% rename from metadata-word.go rename to internal/codecs/metadata/word.go index 1baff95..fab8eb3 100644 --- a/metadata-word.go +++ b/internal/codecs/metadata/word.go @@ -1,4 +1,4 @@ -package binary +package metadata import ( "encoding/binary"