Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Aug 3, 2023
1 parent ae0b246 commit f9d584c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 41 deletions.
51 changes: 15 additions & 36 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,7 @@ func NewArrayValueWithIterator(
interpreter *Interpreter,
arrayType ArrayStaticType,
address common.Address,
count uint64,
countOverestimate uint64,
values func() Value,
) *ArrayValue {
interpreter.ReportComputation(common.ComputationKindCreateArrayValue, 1)
Expand Down Expand Up @@ -1652,7 +1652,7 @@ func NewArrayValueWithIterator(
return array
}
// must assign to v here for tracing to work properly
v = newArrayValueFromConstructor(interpreter, arrayType, count, constructor)
v = newArrayValueFromConstructor(interpreter, arrayType, countOverestimate, constructor)
return v
}

Expand All @@ -1669,14 +1669,14 @@ func newArrayValueFromAtreeValue(
func newArrayValueFromConstructor(
gauge common.MemoryGauge,
staticType ArrayStaticType,
count uint64,
countOverestimate uint64,
constructor func() *atree.Array,
) (array *ArrayValue) {
var elementSize uint
if staticType != nil {
elementSize = staticType.ElementType().elementSize()
}
baseUsage, elementUsage, dataSlabs, metaDataSlabs := common.NewArrayMemoryUsages(count, elementSize)
baseUsage, elementUsage, dataSlabs, metaDataSlabs := common.NewArrayMemoryUsages(countOverestimate, elementSize)
common.UseMemory(gauge, baseUsage)
common.UseMemory(gauge, elementUsage)
common.UseMemory(gauge, dataSlabs)
Expand Down Expand Up @@ -2448,6 +2448,7 @@ func (v *ArrayValue) GetMember(interpreter *Interpreter, locationRange LocationR
return NewHostFunctionValue(
interpreter,
sema.ArrayFilterFunctionType(
interpreter,
v.SemaType(interpreter).ElementType(false),
),
func(invocation Invocation) Value {
Expand Down Expand Up @@ -2973,42 +2974,20 @@ func (v *ArrayValue) Filter(
locationRange LocationRange,
procedure FunctionValue,
) Value {
filteredValuesCount := 0

invocation := NewInvocation(
interpreter,
nil,
nil,
[]Value{}, // Set later during invocation.
[]sema.Type{v.semaType.ElementType(false)},
nil,
locationRange,
)
iterationInvocation := func(arrayElement Value) Invocation {
invocation.Arguments = []Value{arrayElement}
invocation := NewInvocation(
interpreter,
nil,
nil,
[]Value{arrayElement},
[]sema.Type{v.semaType.ElementType(false)},
nil,
locationRange,
)
return invocation
}

err := v.array.Iterate(
func(item atree.Value) (bool, error) {
arrayElement := MustConvertStoredValue(interpreter, item)

shouldInclude, ok := procedure.invoke(iterationInvocation(arrayElement)).(BoolValue)
if !ok {
panic(errors.NewUnreachableError())
}

if shouldInclude {
filteredValuesCount++
}

return true, nil
},
)
if err != nil {
panic(errors.NewExternalError(err))
}

iterator, err := v.array.Iterator()
if err != nil {
panic(errors.NewExternalError(err))
Expand All @@ -3018,7 +2997,7 @@ func (v *ArrayValue) Filter(
interpreter,
NewVariableSizedStaticType(interpreter, v.Type.ElementType()),
common.ZeroAddress,
uint64(filteredValuesCount),
uint64(v.Count()), // worst case estimation.
func() Value {

var value Value
Expand Down
8 changes: 3 additions & 5 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ func getArrayMembers(arrayType ArrayType) map[string]MemberResolver {
memoryGauge,
arrayType,
identifier,
ArrayFilterFunctionType(elementType),
ArrayFilterFunctionType(memoryGauge, elementType),
arrayTypeFilterFunctionDocString,
)
},
Expand Down Expand Up @@ -2264,7 +2264,7 @@ func ArrayReverseFunctionType(arrayType ArrayType) *FunctionType {
}
}

func ArrayFilterFunctionType(elementType Type) *FunctionType {
func ArrayFilterFunctionType(memoryGauge common.MemoryGauge, elementType Type) *FunctionType {
// fun filter(_ function: ((T): Bool)): [T]
// funcType: elementType -> Bool
funcType := &FunctionType{
Expand All @@ -2285,9 +2285,7 @@ func ArrayFilterFunctionType(elementType Type) *FunctionType {
TypeAnnotation: NewTypeAnnotation(funcType),
},
},
ReturnTypeAnnotation: NewTypeAnnotation(&VariableSizedType{
Type: elementType,
}),
ReturnTypeAnnotation: NewTypeAnnotation(NewVariableSizedType(memoryGauge, elementType)),
}
}

Expand Down

0 comments on commit f9d584c

Please sign in to comment.