Skip to content

Commit

Permalink
tlv: zero alloc encoding for extended types
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Jul 15, 2019
1 parent bbfd60b commit c1e0b21
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
36 changes: 36 additions & 0 deletions tlv/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ func EUint8(w io.Writer, val interface{}, buf *[8]byte) error {
return ErrTypeForEncoding{val, "uint8"}
}

// EUint8T encodes a uint8 val to the provided io.Writer. This method is exposed
// so that encodings for custom uint8-like types can be created without
// incurring an extra heap allocation.
func EUint8T(w io.Writer, val uint8, buf *[8]byte) error {
buf[0] = val
_, err := w.Write(buf[:1])
return err
}

// EUint16 is an Encoder for uint16 values. An error is returned if val is not a
// *uint16.
func EUint16(w io.Writer, val interface{}, buf *[8]byte) error {
Expand All @@ -84,6 +93,15 @@ func EUint16(w io.Writer, val interface{}, buf *[8]byte) error {
return ErrTypeForEncoding{val, "uint16"}
}

// EUint16T encodes a uint16 val to the provided io.Writer. This method is
// exposed so that encodings for custom uint16-like types can be created without
// incurring an extra heap allocation.
func EUint16T(w io.Writer, val uint16, buf *[8]byte) error {
byteOrder.PutUint16(buf[:2], val)
_, err := w.Write(buf[:2])
return err
}

// EUint32 is an Encoder for uint32 values. An error is returned if val is not a
// *uint32.
func EUint32(w io.Writer, val interface{}, buf *[8]byte) error {
Expand All @@ -95,6 +113,15 @@ func EUint32(w io.Writer, val interface{}, buf *[8]byte) error {
return ErrTypeForEncoding{val, "uint32"}
}

// EUint32T encodes a uint32 val to the provided io.Writer. This method is
// exposed so that encodings for custom uint32-like types can be created without
// incurring an extra heap allocation.
func EUint32T(w io.Writer, val uint32, buf *[8]byte) error {
byteOrder.PutUint32(buf[:4], val)
_, err := w.Write(buf[:4])
return err
}

// EUint64 is an Encoder for uint64 values. An error is returned if val is not a
// *uint64.
func EUint64(w io.Writer, val interface{}, buf *[8]byte) error {
Expand All @@ -106,6 +133,15 @@ func EUint64(w io.Writer, val interface{}, buf *[8]byte) error {
return ErrTypeForEncoding{val, "uint64"}
}

// EUint64T encodes a uint64 val to the provided io.Writer. This method is
// exposed so that encodings for custom uint64-like types can be created without
// incurring an extra heap allocation.
func EUint64T(w io.Writer, val uint64, buf *[8]byte) error {
byteOrder.PutUint64(buf[:], val)
_, err := w.Write(buf[:])
return err
}

// DUint8 is a Decoder for uint8 values. An error is returned if val is not a
// *uint8.
func DUint8(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
Expand Down
6 changes: 2 additions & 4 deletions tlv/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ type CreateSessionTLV struct {

func EBlobType(w io.Writer, val interface{}, buf *[8]byte) error {
if t, ok := val.(*blob.Type); ok {
tt := uint16(*t)
return tlv.EUint16(w, &tt, buf)
return tlv.EUint16T(w, uint16(*t), buf)
}
return tlv.NewTypeForEncodingErr(val, "blob.Type")
}
Expand All @@ -337,8 +336,7 @@ func DBlobType(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {

func ESatPerKW(w io.Writer, val interface{}, buf *[8]byte) error {
if v, ok := val.(*lnwallet.SatPerKWeight); ok {
vv := uint64(*v)
return tlv.EUint64(w, &vv, buf)
return tlv.EUint64(w, uint64(*v), buf)
}
return tlv.NewTypeForEncodingErr(val, "lnwallet.SatPerKWeight")
}
Expand Down

0 comments on commit c1e0b21

Please sign in to comment.