Skip to content

Commit

Permalink
Create Encoder.inlinedExtraData lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Oct 24, 2023
1 parent d718306 commit d88bd12
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
8 changes: 4 additions & 4 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ func (a *ArrayDataSlab) encodeAsInlined(enc *Encoder) error {
fmt.Errorf("failed to encode standalone array data slab as inlined"))
}

extraDataIndex := enc.inlinedExtraData.addArrayExtraData(a.extraData)
extraDataIndex := enc.inlinedExtraData().addArrayExtraData(a.extraData)

if extraDataIndex > maxInlinedExtraDataIndex {
return NewEncodingError(
Expand Down Expand Up @@ -828,7 +828,7 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error {
h.setRoot()
}

if !elementEnc.inlinedExtraData.empty() {
if !elementEnc.inlinedExtraData().empty() {
h.setHasInlinedSlabs()
}

Expand All @@ -848,8 +848,8 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error {
}

// Encode inlined extra data
if !elementEnc.inlinedExtraData.empty() {
err = elementEnc.inlinedExtraData.Encode(enc)
if !elementEnc.inlinedExtraData().empty() {
err = elementEnc.inlinedExtraData().Encode(enc)
if err != nil {
// err is already categorized by inlinedExtraData.Encode().
return err
Expand Down
22 changes: 14 additions & 8 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ import (
// Encoder writes atree slabs to io.Writer.
type Encoder struct {
io.Writer
CBOR *cbor.StreamEncoder
Scratch [64]byte
encMode cbor.EncMode
inlinedExtraData *InlinedExtraData
CBOR *cbor.StreamEncoder
Scratch [64]byte
encMode cbor.EncMode
_inlinedExtraData *InlinedExtraData
}

func NewEncoder(w io.Writer, encMode cbor.EncMode) *Encoder {
streamEncoder := encMode.NewStreamEncoder(w)
return &Encoder{
Writer: w,
CBOR: streamEncoder,
encMode: encMode,
inlinedExtraData: newInlinedExtraData(),
Writer: w,
CBOR: streamEncoder,
encMode: encMode,
}
}

func (enc *Encoder) inlinedExtraData() *InlinedExtraData {
if enc._inlinedExtraData == nil {
enc._inlinedExtraData = newInlinedExtraData()
}
return enc._inlinedExtraData
}

type StorableDecoder func(
decoder *cbor.StreamDecoder,
storableSlabID SlabID,
Expand Down
10 changes: 5 additions & 5 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2718,7 +2718,7 @@ func (m *MapDataSlab) Encode(enc *Encoder) error {
h.setRoot()
}

if !elemEnc.inlinedExtraData.empty() {
if !elemEnc.inlinedExtraData().empty() {
h.setHasInlinedSlabs()
}

Expand All @@ -2738,8 +2738,8 @@ func (m *MapDataSlab) Encode(enc *Encoder) error {
}

// Encode inlined types
if !elemEnc.inlinedExtraData.empty() {
err = elemEnc.inlinedExtraData.Encode(enc)
if !elemEnc.inlinedExtraData().empty() {
err = elemEnc.inlinedExtraData().Encode(enc)
if err != nil {
return NewEncodingError(err)
}
Expand Down Expand Up @@ -2816,7 +2816,7 @@ func (m *MapDataSlab) encodeAsInlined(enc *Encoder) error {

func (m *MapDataSlab) encodeAsInlinedMap(enc *Encoder) error {

extraDataIndex := enc.inlinedExtraData.addMapExtraData(m.extraData)
extraDataIndex := enc.inlinedExtraData().addMapExtraData(m.extraData)

if extraDataIndex > maxInlinedExtraDataIndex {
return NewEncodingError(fmt.Errorf("extra data index %d exceeds limit %d", extraDataIndex, maxInlinedExtraDataIndex))
Expand Down Expand Up @@ -2876,7 +2876,7 @@ func encodeAsInlinedCompactMap(
values []Storable,
) error {

extraDataIndex, cachedKeys := enc.inlinedExtraData.addCompactMapExtraData(extraData, hkeys, keys)
extraDataIndex, cachedKeys := enc.inlinedExtraData().addCompactMapExtraData(extraData, hkeys, keys)

if len(keys) != len(cachedKeys) {
return NewEncodingError(fmt.Errorf("number of elements %d is different from number of elements in cached compact map type %d", len(keys), len(cachedKeys)))
Expand Down

0 comments on commit d88bd12

Please sign in to comment.