Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add computation metering to the new stdlib functions #2880

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/common/computationkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const (
ComputationKindCreateArrayValue
ComputationKindTransferArrayValue
ComputationKindDestroyArrayValue
_
ComputationKindIterateArrayValue
SupunS marked this conversation as resolved.
Show resolved Hide resolved
_
_
_
Expand Down
7 changes: 4 additions & 3 deletions runtime/common/computationkind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2996,6 +2996,9 @@ func (v *ArrayValue) Reverse(
return nil
}

// Meter computation for iterating the array.
interpreter.ReportComputation(common.ComputationKindIterateArrayValue, 1)

value := v.Get(interpreter, locationRange, index)
index--

Expand Down Expand Up @@ -3046,6 +3049,9 @@ func (v *ArrayValue) Filter(
var value Value

for {
// Meter computation for iterating the array.
interpreter.ReportComputation(common.ComputationKindIterateArrayValue, 1)

atreeValue, err := iterator.Next()
if err != nil {
panic(errors.NewExternalError(err))
Expand Down Expand Up @@ -3139,6 +3145,9 @@ func (v *ArrayValue) Map(
uint64(v.Count()),
func() Value {

// Meter computation for iterating the array.
interpreter.ReportComputation(common.ComputationKindIterateArrayValue, 1)

atreeValue, err := iterator.Next()
if err != nil {
panic(errors.NewExternalError(err))
Expand Down
4 changes: 4 additions & 0 deletions runtime/interpreter/value_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func stringFunctionJoin(invocation Invocation) Value {
first := true

stringArray.Iterate(inter, func(element Value) (resume bool) {

// Meter computation for iterating the array.
inter.ReportComputation(common.ComputationKindIterateArrayValue, 1)

// Add separator
if !first {
// Construct directly instead of using NewStringMemoryUsage to avoid
Expand Down
115 changes: 115 additions & 0 deletions runtime/tests/interpreter/metering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,118 @@ func TestInterpretFunctionInvocationHandler(t *testing.T) {
occurrences,
)
}

func TestInterpretArrayFunctionsComputationMetering(t *testing.T) {

t.Parallel()

t.Run("reverse", func(t *testing.T) {
t.Parallel()

computationMeteredValues := make(map[common.ComputationKind]uint)
inter, err := parseCheckAndInterpretWithOptions(t, `
fun main() {
let x = [1, 2, 3]
let y = x.reverse()
}`,
ParseCheckAndInterpretOptions{
Config: &interpreter.Config{
OnMeterComputation: func(compKind common.ComputationKind, intensity uint) {
computationMeteredValues[compKind] += intensity
},
},
},
)
require.NoError(t, err)

_, err = inter.Invoke("main")
require.NoError(t, err)

assert.Equal(t, uint(3), computationMeteredValues[common.ComputationKindIterateArrayValue])
})

t.Run("map", func(t *testing.T) {
t.Parallel()

computationMeteredValues := make(map[common.ComputationKind]uint)
inter, err := parseCheckAndInterpretWithOptions(t, `
fun main() {
let x = [1, 2, 3, 4]
let trueForEven = fun (_ x: Int): Bool {
return x % 2 == 0
}
let y = x.map(trueForEven)
}`,
ParseCheckAndInterpretOptions{
Config: &interpreter.Config{
OnMeterComputation: func(compKind common.ComputationKind, intensity uint) {
computationMeteredValues[compKind] += intensity
},
},
},
)
require.NoError(t, err)

_, err = inter.Invoke("main")
require.NoError(t, err)

assert.Equal(t, uint(5), computationMeteredValues[common.ComputationKindIterateArrayValue])
})

t.Run("filter", func(t *testing.T) {
t.Parallel()

computationMeteredValues := make(map[common.ComputationKind]uint)
inter, err := parseCheckAndInterpretWithOptions(t, `
fun main() {
let x = [1, 2, 3, 4, 5]
let onlyEven = fun (_ x: Int): Bool {
return x % 2 == 0
}
let y = x.filter(onlyEven)
}`,
ParseCheckAndInterpretOptions{
Config: &interpreter.Config{
OnMeterComputation: func(compKind common.ComputationKind, intensity uint) {
computationMeteredValues[compKind] += intensity
},
},
},
)
require.NoError(t, err)

_, err = inter.Invoke("main")
require.NoError(t, err)

assert.Equal(t, uint(6), computationMeteredValues[common.ComputationKindIterateArrayValue])
})
}

func TestInterpretStdlibComputationMetering(t *testing.T) {

t.Parallel()

t.Run("string join", func(t *testing.T) {
t.Parallel()

computationMeteredValues := make(map[common.ComputationKind]uint)
inter, err := parseCheckAndInterpretWithOptions(t, `
fun main() {
let s = String.join(["one", "two", "three", "four"], separator: ", ")
}`,
ParseCheckAndInterpretOptions{
Config: &interpreter.Config{
OnMeterComputation: func(compKind common.ComputationKind, intensity uint) {
computationMeteredValues[compKind] += intensity
},
},
},
)
require.NoError(t, err)

_, err = inter.Invoke("main")
require.NoError(t, err)

assert.Equal(t, uint(4), computationMeteredValues[common.ComputationKindIterateArrayValue])
})
}
Loading