forked from cerc-io/plugeth
rlp: remove allocation of bytes.Reader in DecodeBytes (#27987)
This commit is contained in:
parent
52219ced8b
commit
5c7136adb4
@ -90,7 +90,7 @@ func Decode(r io.Reader, val interface{}) error {
|
|||||||
// DecodeBytes parses RLP data from b into val. Please see package-level documentation for
|
// DecodeBytes parses RLP data from b into val. Please see package-level documentation for
|
||||||
// the decoding rules. The input must contain exactly one value and no trailing data.
|
// the decoding rules. The input must contain exactly one value and no trailing data.
|
||||||
func DecodeBytes(b []byte, val interface{}) error {
|
func DecodeBytes(b []byte, val interface{}) error {
|
||||||
r := bytes.NewReader(b)
|
r := (*sliceReader)(&b)
|
||||||
|
|
||||||
stream := streamPool.Get().(*Stream)
|
stream := streamPool.Get().(*Stream)
|
||||||
defer streamPool.Put(stream)
|
defer streamPool.Put(stream)
|
||||||
@ -99,7 +99,7 @@ func DecodeBytes(b []byte, val interface{}) error {
|
|||||||
if err := stream.Decode(val); err != nil {
|
if err := stream.Decode(val); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if r.Len() > 0 {
|
if len(b) > 0 {
|
||||||
return ErrMoreThanOneValue
|
return ErrMoreThanOneValue
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -1182,3 +1182,23 @@ func (s *Stream) listLimit() (inList bool, limit uint64) {
|
|||||||
}
|
}
|
||||||
return true, s.stack[len(s.stack)-1]
|
return true, s.stack[len(s.stack)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type sliceReader []byte
|
||||||
|
|
||||||
|
func (sr *sliceReader) Read(b []byte) (int, error) {
|
||||||
|
if len(*sr) == 0 {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
n := copy(b, *sr)
|
||||||
|
*sr = (*sr)[n:]
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sr *sliceReader) ReadByte() (byte, error) {
|
||||||
|
if len(*sr) == 0 {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
b := (*sr)[0]
|
||||||
|
*sr = (*sr)[1:]
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user