Skip to content

Commit

Permalink
make decoding faster
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-dambovaliev committed Apr 7, 2024
1 parent d66a177 commit 82e0af8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
32 changes: 16 additions & 16 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,7 +53,7 @@ func DecodeBytes(input []byte) (Value, error) {
decodedElements = append(decodedElements, decoded)
}

return &ListValue{
return ListValue{
values: decodedElements,
}, nil
}
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ 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
}

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
}

0 comments on commit 82e0af8

Please sign in to comment.