From 82e0af841c39c5d95aba41db81bdcd50dddc2e9e Mon Sep 17 00:00:00 2001 From: Petar Dambovaliev Date: Sun, 7 Apr 2024 12:59:52 +0200 Subject: [PATCH] make decoding faster --- decode.go | 32 ++++++++++++++++---------------- types.go | 8 ++++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/decode.go b/decode.go index 340b729..ca5b6c4 100755 --- a/decode.go +++ b/decode.go @@ -26,14 +26,14 @@ func DecodeBytes(input []byte) (Value, error) { } if !isListType { - return &BytesValue{ + return BytesValue{ value: data, }, nil } var ( arrayLength = metadata.dataLength - metadata.dataOffset - decodedElements = make([]Value, 0) + decodedElements = make([]Value, 0, 4) ) // Parse each element of the list @@ -53,7 +53,7 @@ func DecodeBytes(input []byte) (Value, error) { decodedElements = append(decodedElements, decoded) } - return &ListValue{ + return ListValue{ values: decodedElements, }, nil } @@ -74,9 +74,9 @@ type metadata struct { } // getMetadata returns the metadata about the top-level RLP type -func getMetadata(data []byte) (*metadata, error) { +func getMetadata(data []byte) (metadata, error) { if len(data) == 0 { - return &metadata{ + return metadata{ dataType: emptyType, }, nil } @@ -86,7 +86,7 @@ func getMetadata(data []byte) (*metadata, error) { switch { case firstByte <= 0x7f: // Single byte value - return &metadata{ + return metadata{ dataType: byteType, dataOffset: 0, dataLength: 1, @@ -96,10 +96,10 @@ func getMetadata(data []byte) (*metadata, error) { length := int(firstByte - 0x80) if length > len(data)-1 { - return nil, constructLengthError(length, len(data)-1) + return metadata{}, constructLengthError(length, len(data)-1) } - return &metadata{ + return metadata{ dataType: shortBytesType, dataOffset: 0, dataLength: length, @@ -108,16 +108,16 @@ func getMetadata(data []byte) (*metadata, error) { // Long bytes lengthBytes := int(firstByte - 0xb7) if lengthBytes > len(data)-1 { - return nil, constructLengthError(lengthBytes, len(data)-1) + return metadata{}, constructLengthError(lengthBytes, len(data)-1) } length := convertHexArrayToInt(data[1 : lengthBytes+1]) if length > len(data)-1-lengthBytes { - return nil, constructLengthError(length, len(data)-1-lengthBytes) + return metadata{}, constructLengthError(length, len(data)-1-lengthBytes) } - return &metadata{ + return metadata{ dataType: longBytesType, dataOffset: lengthBytes, dataLength: lengthBytes + length, @@ -126,10 +126,10 @@ func getMetadata(data []byte) (*metadata, error) { // Short array length := int(firstByte - 0xc0) if length > len(data)-1 { - return nil, constructLengthError(length, len(data)-1) + return metadata{}, constructLengthError(length, len(data)-1) } - return &metadata{ + return metadata{ dataType: shortArrayType, dataOffset: 0, dataLength: length, @@ -138,16 +138,16 @@ func getMetadata(data []byte) (*metadata, error) { // Long array lengthBytes := int(firstByte - 0xf7) if lengthBytes > len(data)-1 { - return nil, constructLengthError(lengthBytes, len(data)-1) + return metadata{}, constructLengthError(lengthBytes, len(data)-1) } length := convertHexArrayToInt(data[1 : lengthBytes+1]) if length > len(data)-1-lengthBytes { - return nil, constructLengthError(length, len(data)-1-lengthBytes) + return metadata{}, constructLengthError(length, len(data)-1-lengthBytes) } - return &metadata{ + return metadata{ dataType: longArrayType, dataOffset: lengthBytes, dataLength: lengthBytes + length, diff --git a/types.go b/types.go index d02441e..ae68000 100755 --- a/types.go +++ b/types.go @@ -29,11 +29,11 @@ type BytesValue struct { value []byte } -func (b *BytesValue) GetType() Type { +func (b BytesValue) GetType() Type { return Bytes } -func (b *BytesValue) GetValue() any { +func (b BytesValue) GetValue() any { return b.value } @@ -41,10 +41,10 @@ type ListValue struct { values []Value } -func (a *ListValue) GetType() Type { +func (a ListValue) GetType() Type { return List } -func (a *ListValue) GetValue() any { +func (a ListValue) GetValue() any { return a.values }