Skip to content

Commit

Permalink
Merge pull request #2509 from onflow/bastian/2507-fix-stored-value-co…
Browse files Browse the repository at this point in the history
…nversion-metering
  • Loading branch information
turbolent authored Oct 5, 2023
2 parents 7d351b4 + 2dd271d commit 5cbc388
Show file tree
Hide file tree
Showing 4 changed files with 1,853 additions and 1,759 deletions.
75 changes: 37 additions & 38 deletions runtime/common/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,25 +314,28 @@ func atreeNodes(count uint64, elementSize uint) (leafNodeCount uint64, branchNod
return
}

func newAtreeMemoryUsage(count uint64, elementSize uint, array bool) (MemoryUsage, MemoryUsage) {
func newAtreeArrayMemoryUsage(count uint64, elementSize uint) (MemoryUsage, MemoryUsage) {
newLeafNodes, newBranchNodes := atreeNodes(count, elementSize)
if array {
return MemoryUsage{
Kind: MemoryKindAtreeArrayDataSlab,
Amount: newLeafNodes,
}, MemoryUsage{
Kind: MemoryKindAtreeArrayMetaDataSlab,
Amount: newBranchNodes,
}
} else {
return MemoryUsage{
Kind: MemoryKindAtreeMapDataSlab,
Amount: newLeafNodes,
}, MemoryUsage{
Kind: MemoryKindAtreeMapMetaDataSlab,
Amount: newBranchNodes,
}
}
return MemoryUsage{
Kind: MemoryKindAtreeArrayDataSlab,
Amount: newLeafNodes,
},
MemoryUsage{
Kind: MemoryKindAtreeArrayMetaDataSlab,
Amount: newBranchNodes,
}
}

func newAtreeMapMemoryUsage(count uint64, elementSize uint) (MemoryUsage, MemoryUsage) {
newLeafNodes, newBranchNodes := atreeNodes(count, elementSize)
return MemoryUsage{
Kind: MemoryKindAtreeMapDataSlab,
Amount: newLeafNodes,
},
MemoryUsage{
Kind: MemoryKindAtreeMapMetaDataSlab,
Amount: newBranchNodes,
}
}

func NewCadenceArrayMemoryUsages(length int) (MemoryUsage, MemoryUsage) {
Expand Down Expand Up @@ -364,28 +367,24 @@ func AdditionalAtreeMemoryUsage(originalCount uint64, elementSize uint, array bo
}
}

func NewArrayMemoryUsages(count uint64, elementSize uint) (MemoryUsage, MemoryUsage, MemoryUsage, MemoryUsage) {
leaves, branches := newAtreeMemoryUsage(count, elementSize, true)
return ArrayValueBaseMemoryUsage, MemoryUsage{
Kind: MemoryKindAtreeArrayElementOverhead,
Amount: count,
}, leaves, branches
}

func NewDictionaryMemoryUsages(count uint64, elementSize uint) (MemoryUsage, MemoryUsage, MemoryUsage, MemoryUsage) {
leaves, branches := newAtreeMemoryUsage(count, elementSize, false)
return DictionaryValueBaseMemoryUsage, MemoryUsage{
Kind: MemoryKindAtreeMapElementOverhead,
Amount: count,
}, leaves, branches
func NewAtreeArrayMemoryUsages(count uint64, elementSize uint) (MemoryUsage, MemoryUsage, MemoryUsage) {
leaves, branches := newAtreeArrayMemoryUsage(count, elementSize)
return MemoryUsage{
Kind: MemoryKindAtreeArrayElementOverhead,
Amount: count,
},
leaves,
branches
}

func NewCompositeMemoryUsages(count uint64, elementSize uint) (MemoryUsage, MemoryUsage, MemoryUsage, MemoryUsage) {
leaves, branches := newAtreeMemoryUsage(count, elementSize, false)
return CompositeValueBaseMemoryUsage, MemoryUsage{
Kind: MemoryKindAtreeMapElementOverhead,
Amount: count,
}, leaves, branches
func NewAtreeMapMemoryUsages(count uint64, elementSize uint) (MemoryUsage, MemoryUsage, MemoryUsage) {
leaves, branches := newAtreeMapMemoryUsage(count, elementSize)
return MemoryUsage{
Kind: MemoryKindAtreeMapElementOverhead,
Amount: count,
},
leaves,
branches
}

func NewAtreeMapPreAllocatedElementsMemoryUsage(count uint64, elementSize uint) MemoryUsage {
Expand Down
26 changes: 21 additions & 5 deletions runtime/interpreter/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,33 @@ func ConvertStoredValue(gauge common.MemoryGauge, value atree.Value) (Value, err
if !ok {
panic(errors.NewUnreachableError())
}
return newArrayValueFromConstructor(gauge, staticType, value.Count(), func() *atree.Array { return value }), nil
return newArrayValueFromAtreeArray(
gauge,
staticType,
ArrayElementSize(staticType),
value,
), nil

case *atree.OrderedMap:
typeInfo := value.Type()
switch typeInfo := typeInfo.(type) {
switch staticType := typeInfo.(type) {
case *DictionaryStaticType:
return newDictionaryValueFromConstructor(gauge, typeInfo, value.Count(), func() *atree.OrderedMap { return value }), nil
return newDictionaryValueFromAtreeMap(
gauge,
staticType,
DictionaryElementSize(staticType),
value,
), nil

case compositeTypeInfo:
return newCompositeValueFromConstructor(gauge, value.Count(), typeInfo, func() *atree.OrderedMap { return value }), nil
return newCompositeValueFromAtreeMap(
gauge,
staticType,
value,
), nil

default:
return nil, errors.NewUnexpectedError("invalid ordered map type info: %T", typeInfo)
return nil, errors.NewUnexpectedError("invalid ordered map type info: %T", staticType)
}

case Value:
Expand Down
Loading

0 comments on commit 5cbc388

Please sign in to comment.