Skip to content

Commit

Permalink
Create Go maps in inlinedExtraData lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Oct 3, 2023
1 parent 3f87ec5 commit 903fc13
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 3 additions & 3 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,7 @@ func DecodeInlinedCompactMapStorable(
copy(hkeys, extraData.hkeys)

// Decode values
size := uint32(hkeyElementsPrefixSize)
elementsSize := uint32(hkeyElementsPrefixSize)
elems := make([]element, elemCount)
for i := 0; i < int(elemCount); i++ {
value, err := decodeStorable(dec, slabID, inlinedExtraData)
Expand All @@ -2517,15 +2517,15 @@ func DecodeInlinedCompactMapStorable(
elem := &singleElement{key, value, elemSize}

elems[i] = elem
size += digestSize + elem.Size()
elementsSize += digestSize + elem.Size()
}

// Create hkeyElements
elements := &hkeyElements{
hkeys: hkeys,
elems: elems,
level: 0,
size: size,
size: elementsSize,
}

header := MapSlabHeader{
Expand Down
13 changes: 9 additions & 4 deletions typeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,7 @@ type inlinedExtraData struct {
}

func newInlinedExtraData() *inlinedExtraData {
return &inlinedExtraData{
compactMapTypes: make(map[string]compactMapTypeInfo),
arrayTypes: make(map[string]int),
}
return &inlinedExtraData{}
}

// Encode encodes inlined extra data as CBOR array.
Expand Down Expand Up @@ -311,6 +308,10 @@ func newInlinedExtraDataFromData(
// Array extra data is deduplicated by array type info ID because array
// extra data only contains type info.
func (ied *inlinedExtraData) addArrayExtraData(data *ArrayExtraData) int {
if ied.arrayTypes == nil {
ied.arrayTypes = make(map[string]int)
}

id := data.TypeInfo.ID()
index, exist := ied.arrayTypes[id]
if exist {
Expand Down Expand Up @@ -339,6 +340,10 @@ func (ied *inlinedExtraData) addCompactMapExtraData(
keys []ComparableStorable,
) (int, []ComparableStorable) {

if ied.compactMapTypes == nil {
ied.compactMapTypes = make(map[string]compactMapTypeInfo)
}

id := makeCompactMapTypeID(data.TypeInfo, keys)
info, exist := ied.compactMapTypes[id]
if exist {
Expand Down

0 comments on commit 903fc13

Please sign in to comment.