Skip to content

Commit

Permalink
Rename composite to compactMap
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Oct 2, 2023
1 parent 836eb70 commit 4b3c26c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 100 deletions.
14 changes: 7 additions & 7 deletions array_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,14 @@ func (v *serializationVerifier) verifyArraySlab(slab ArraySlab) error {
}

// Extra check: encoded data size == header.size
// This check is skipped for slabs with inlined composite because
// This check is skipped for slabs with inlined compact map because
// encoded size and slab size differ for inlined composites.
// For inlined composites, digests and field keys are encoded in
// composite extra data section for reuse, and only composite field
// compact map extra data section for reuse, and only compact map field
// values are encoded in non-extra data section.
// This reduces encoding size because composite values of the same
// composite type can reuse encoded type info, seed, digests, and field names.
// TODO: maybe add size check for slabs with inlined composite by decoding entire slab.
// This reduces encoding size because compact map values of the same
// compact map type can reuse encoded type info, seed, digests, and field names.
// TODO: maybe add size check for slabs with inlined compact map by decoding entire slab.
inlinedComposite, err := hasInlinedComposite(data)
if err != nil {
// Don't need to wrap error as external error because err is already categorized by hasInlinedComposite().
Expand Down Expand Up @@ -832,7 +832,7 @@ func hasInlinedComposite(data []byte) (bool, error) {
data = data[len(b):]
}

// Parse inlined extra data to find composite extra data.
// Parse inlined extra data to find compact map extra data.
dec := cbor.NewStreamDecoder(bytes.NewBuffer(data))
count, err := dec.DecodeArrayHead()
if err != nil {
Expand All @@ -844,7 +844,7 @@ func hasInlinedComposite(data []byte) (bool, error) {
if err != nil {
return false, NewDecodingError(err)
}
if tagNum == CBORTagInlinedCompositeExtraData {
if tagNum == CBORTagInlinedCompactMapExtraData {
return true, nil
}
err = dec.Skip()
Expand Down
52 changes: 26 additions & 26 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2410,8 +2410,8 @@ func newMapDataSlabFromDataV1(
}, nil
}

// DecodeInlinedCompositeStorable decodes inlined composite data. Encoding is
// version 1 with CBOR tag having tag number CBORTagInlinedComposite, and tag contant
// DecodeInlinedCompactMapStorable decodes inlined compact map data. Encoding is
// version 1 with CBOR tag having tag number CBORTagInlinedCompactMap, and tag contant
// as 3-element array:
//
// - index of inlined extra data
Expand All @@ -2420,7 +2420,7 @@ func newMapDataSlabFromDataV1(
//
// NOTE: This function doesn't decode tag number because tag number is decoded
// in the caller and decoder only contains tag content.
func DecodeInlinedCompositeStorable(
func DecodeInlinedCompactMapStorable(
dec *cbor.StreamDecoder,
decodeStorable StorableDecoder,
parentSlabID SlabID,
Expand All @@ -2439,7 +2439,7 @@ func DecodeInlinedCompositeStorable(
if arrayCount != inlinedMapDataSlabArrayCount {
return nil, NewDecodingError(
fmt.Errorf(
"failed to decode inlined composite, expect array of %d elements, got %d elements",
"failed to decode inlined compact map data, expect array of %d elements, got %d elements",
inlinedMapDataSlabArrayCount,
arrayCount))
}
Expand All @@ -2452,16 +2452,16 @@ func DecodeInlinedCompositeStorable(
if extraDataIndex >= uint64(len(inlinedExtraData)) {
return nil, NewDecodingError(
fmt.Errorf(
"failed to decode inlined composite: inlined extra data index %d exceeds number of inlined extra data %d",
"failed to decode inlined compact map data: inlined extra data index %d exceeds number of inlined extra data %d",
extraDataIndex,
len(inlinedExtraData)))
}

extraData, ok := inlinedExtraData[extraDataIndex].(*compositeExtraData)
extraData, ok := inlinedExtraData[extraDataIndex].(*compactMapExtraData)
if !ok {
return nil, NewDecodingError(
fmt.Errorf(
"failed to decode inlined composite: expect *compositeExtraData, got %T",
"failed to decode inlined compact map data: expect *compactMapExtraData, got %T",
inlinedExtraData[extraDataIndex]))
}

Expand All @@ -2473,7 +2473,7 @@ func DecodeInlinedCompositeStorable(
if len(b) != slabIndexSize {
return nil, NewDecodingError(
fmt.Errorf(
"failed to decode inlined composite: expect %d bytes for slab index, got %d bytes",
"failed to decode inlined compact map data: expect %d bytes for slab index, got %d bytes",
slabIndexSize,
len(b)))
}
Expand All @@ -2492,12 +2492,12 @@ func DecodeInlinedCompositeStorable(
if elemCount != uint64(len(extraData.keys)) {
return nil, NewDecodingError(
fmt.Errorf(
"failed to decode composite values: got %d, expect %d",
"failed to decode compact map values: got %d, expect %d",
elemCount,
extraData.mapExtraData.Count))
}

// Make a copy of digests because extraData is shared by all inlined composite referring to the same type.
// Make a copy of digests because extraData is shared by all inlined compact map data referring to the same type.
hkeys := make([]Digest, len(extraData.hkeys))
copy(hkeys, extraData.hkeys)

Expand Down Expand Up @@ -2592,7 +2592,7 @@ func DecodeInlinedMapStorable(
if extraDataIndex >= uint64(len(inlinedExtraData)) {
return nil, NewDecodingError(
fmt.Errorf(
"failed to decode inlined composite: inlined extra data index %d exceeds number of inlined extra data %d",
"failed to decode inlined compact map data: inlined extra data index %d exceeds number of inlined extra data %d",
extraDataIndex,
len(inlinedExtraData)))
}
Expand All @@ -2612,7 +2612,7 @@ func DecodeInlinedMapStorable(
if len(b) != slabIndexSize {
return nil, NewDecodingError(
fmt.Errorf(
"failed to decode inlined composite: expect %d bytes for slab index, got %d bytes",
"failed to decode inlined compact map data: expect %d bytes for slab index, got %d bytes",
slabIndexSize,
len(b)))
}
Expand Down Expand Up @@ -2810,8 +2810,8 @@ func (m *MapDataSlab) encodeAsInlined(enc *Encoder, inlinedTypeInfo *inlinedExtr
fmt.Errorf("failed to encode standalone map data slab as inlined"))
}

if hkeys, keys, values, ok := m.canBeEncodedAsComposite(); ok {
return encodeAsInlinedComposite(enc, m.header.slabID, m.extraData, hkeys, keys, values, inlinedTypeInfo)
if hkeys, keys, values, ok := m.canBeEncodedAsCompactMap(); ok {
return encodeAsInlinedCompactMap(enc, m.header.slabID, m.extraData, hkeys, keys, values, inlinedTypeInfo)
}

return m.encodeAsInlinedMap(enc, inlinedTypeInfo)
Expand Down Expand Up @@ -2869,8 +2869,8 @@ func (m *MapDataSlab) encodeAsInlinedMap(enc *Encoder, inlinedTypeInfo *inlinedE
return nil
}

// encodeAsInlinedComposite encodes hkeys, keys, and values as inlined composite value.
func encodeAsInlinedComposite(
// encodeAsInlinedCompactMap encodes hkeys, keys, and values as inlined compact map value.
func encodeAsInlinedCompactMap(
enc *Encoder,
slabID SlabID,
extraData *MapExtraData,
Expand All @@ -2880,10 +2880,10 @@ func encodeAsInlinedComposite(
inlinedTypeInfo *inlinedExtraData,
) error {

extraDataIndex, cachedKeys := inlinedTypeInfo.addCompositeExtraData(extraData, hkeys, keys)
extraDataIndex, cachedKeys := inlinedTypeInfo.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 composite type %d", 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)))
}

if extraDataIndex > maxInlinedExtraDataIndex {
Expand All @@ -2896,7 +2896,7 @@ func encodeAsInlinedComposite(
// Encode tag number and array head of 3 elements
err = enc.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, CBORTagInlinedComposite,
0xd8, CBORTagInlinedCompactMap,
// array head of 3 elements
0x83,
})
Expand All @@ -2920,8 +2920,8 @@ func encodeAsInlinedComposite(
return NewEncodingError(err)
}

// element 2: composite values in the order of cachedKeys
err = encodeCompositeValues(enc, cachedKeys, keys, values, inlinedTypeInfo)
// element 2: compact map values in the order of cachedKeys
err = encodeCompactMapValues(enc, cachedKeys, keys, values, inlinedTypeInfo)
if err != nil {
return NewEncodingError(err)
}
Expand All @@ -2934,8 +2934,8 @@ func encodeAsInlinedComposite(
return nil
}

// encodeCompositeValues encodes composite values as an array of values ordered by cachedKeys.
func encodeCompositeValues(
// encodeCompactMapValues encodes compact values as an array of values ordered by cachedKeys.
func encodeCompactMapValues(
enc *Encoder,
cachedKeys []ComparableStorable,
keys []ComparableStorable,
Expand Down Expand Up @@ -2983,12 +2983,12 @@ func encodeCompositeValues(
return nil
}

// canBeEncodedAsComposite returns true if:
// canBeEncodedAsCompactMap returns true if:
// - map data slab is inlined
// - map is composite type
// - map type is composite type
// - no collision elements
// - keys are stored inline (not in a separate slab)
func (m *MapDataSlab) canBeEncodedAsComposite() ([]Digest, []ComparableStorable, []Storable, bool) {
func (m *MapDataSlab) canBeEncodedAsCompactMap() ([]Digest, []ComparableStorable, []Storable, bool) {
if !m.inlined {
return nil, nil, nil, false
}
Expand Down
10 changes: 5 additions & 5 deletions map_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,14 +967,14 @@ func (v *serializationVerifier) verifyMapSlab(slab MapSlab) error {
}

// Extra check: encoded data size == header.size
// This check is skipped for slabs with inlined composite because
// This check is skipped for slabs with inlined compact map because
// encoded size and slab size differ for inlined composites.
// For inlined composites, digests and field keys are encoded in
// composite extra data section for reuse, and only composite field
// compact map extra data section for reuse, and only compact map field
// values are encoded in non-extra data section.
// This reduces encoding size because composite values of the same
// composite type can reuse encoded type info, seed, digests, and field names.
// TODO: maybe add size check for slabs with inlined composite by decoding entire slab.
// This reduces encoding size because compact map values of the same
// compact map type can reuse encoded type info, seed, digests, and field names.
// TODO: maybe add size check for slabs with inlined compact map by decoding entire slab.
inlinedComposite, err := hasInlinedComposite(data)
if err != nil {
// Don't need to wrap error as external error because err is already categorized by hasInlinedComposite().
Expand Down
30 changes: 15 additions & 15 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6488,7 +6488,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 0
0xd8, 0xa4, 0x00,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -6505,7 +6505,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 2
0xd8, 0xa4, 0x01,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand Down Expand Up @@ -6658,7 +6658,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 0
0xd8, 0xa4, 0x00,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -6677,7 +6677,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 2
0xd8, 0xa4, 0x01,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand Down Expand Up @@ -6835,7 +6835,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 0
0xd8, 0xa4, 0x00,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -6854,7 +6854,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 2
0xd8, 0xa4, 0x01,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand Down Expand Up @@ -7063,7 +7063,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 0
0xd8, 0xa4, 0x00,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -7082,7 +7082,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 1
0xd8, 0xa4, 0x01,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -7101,7 +7101,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 2
0xd8, 0xa4, 0x02,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand Down Expand Up @@ -7276,7 +7276,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 0
0xd8, 0xa4, 0x00,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -7295,7 +7295,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 2
0xd8, 0xa4, 0x01,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand Down Expand Up @@ -7479,7 +7479,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 0
0xd8, 0xa4, 0x00,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -7498,7 +7498,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 1
0xd8, 0xa4, 0x01,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -7517,7 +7517,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 2
0xd8, 0xa4, 0x02,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand All @@ -7536,7 +7536,7 @@ func TestMapEncodeDecode(t *testing.T) {
0x82,
// key: 3
0xd8, 0xa4, 0x03,
// value: inlined composite (tag: CBORTagInlinedComposite)
// value: inlined composite (tag: CBORTagInlinedCompactMap)
0xd8, 0xfc,
// array of 3 elements
0x83,
Expand Down
14 changes: 7 additions & 7 deletions storable.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Storable interface {
}

// ComparableStorable is an interface that supports comparison and cloning of Storable.
// This is only used for composite keys.
// This is only used for compact keys.
type ComparableStorable interface {
Storable

Expand Down Expand Up @@ -72,13 +72,13 @@ const (
// As of Oct. 2, 2023, Cadence uses tag numbers from 128 to 224.
// See runtime/interpreter/encode.go at github.com/onflow/cadence.

CBORTagInlinedArrayExtraData = 247
CBORTagInlinedMapExtraData = 248
CBORTagInlinedCompositeExtraData = 249
CBORTagInlinedArrayExtraData = 247
CBORTagInlinedMapExtraData = 248
CBORTagInlinedCompactMapExtraData = 249

CBORTagInlinedArray = 250
CBORTagInlinedMap = 251
CBORTagInlinedComposite = 252
CBORTagInlinedArray = 250
CBORTagInlinedMap = 251
CBORTagInlinedCompactMap = 252

CBORTagInlineCollisionGroup = 253
CBORTagExternalCollisionGroup = 254
Expand Down
Loading

0 comments on commit 4b3c26c

Please sign in to comment.