Skip to content

fix: add gas usage for composite literals #4020

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

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 10 additions & 9 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,15 +1088,16 @@ const (
OpCPUTypeAssert2 = 25
// TODO: OpCPUStaticTypeOf is an arbitrary number.
// A good way to benchmark this is yet to be determined.
OpCPUStaticTypeOf = 100
OpCPUCompositeLit = 50
OpCPUArrayLit = 137
OpCPUSliceLit = 183
OpCPUSliceLit2 = 467
OpCPUMapLit = 475
OpCPUStructLit = 179
OpCPUFuncLit = 61
OpCPUConvert = 16
OpCPUStaticTypeOf = 100
OpCPUCompositeLit = 50
OpCPUArrayLit = 137
OpCPUSliceLit = 183
OpCPUSliceLit2 = 467
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant list here represents an average value derived from the benchmark. As such, it won’t be precise for a composite literal with a dynamic size. To make it accurate—specifically for a slice literal—we would need to first measure the doOpSliceLit2 shallowly, and then dynamically assess the per-element consumption for the dynamic part. This would complicate the benchmark. This isn’t just about this PR but also relates to #3921.

OpCPUCompositeLitEl = 40
OpCPUMapLit = 475
OpCPUStructLit = 179
OpCPUFuncLit = 61
OpCPUConvert = 16

/* Type operators */
OpCPUFieldType = 59
Expand Down
20 changes: 20 additions & 0 deletions gnovm/pkg/gnolang/op_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ func (m *Machine) doOpArrayLit() {
// assess performance TODO
x := m.PopExpr().(*CompositeLitExpr)
ne := len(x.Elts)

defer func() {
m.incrCPU(int64(OpCPUCompositeLitEl * ne))
}()

// peek array type.
at := m.PeekValue(1 + ne).V.(TypeValue).Type
bt := baseOf(at).(*ArrayType)
Expand Down Expand Up @@ -493,6 +498,11 @@ func (m *Machine) doOpSliceLit2() {
// assess performance TODO
x := m.PopExpr().(*CompositeLitExpr)
el := len(x.Elts)

defer func() {
m.incrCPU(int64(OpCPUCompositeLitEl * el))
}()

tvs := m.PopValues(el * 2)
// peek slice type.
st := m.PeekValue(1).V.(TypeValue).Type
Expand Down Expand Up @@ -542,6 +552,11 @@ func (m *Machine) doOpSliceLit2() {
func (m *Machine) doOpMapLit() {
x := m.PopExpr().(*CompositeLitExpr)
ne := len(x.Elts)

defer func() {
m.incrCPU(int64(OpCPUCompositeLitEl * ne))
}()

// peek map type.
mt := m.PeekValue(1 + ne*2).V.(TypeValue).Type
// bt := baseOf(at).(*MapType)
Expand Down Expand Up @@ -581,6 +596,11 @@ func (m *Machine) doOpStructLit() {
// assess performance TODO
x := m.PopExpr().(*CompositeLitExpr)
el := len(x.Elts) // may be incomplete

defer func() {
m.incrCPU(int64(OpCPUCompositeLitEl * el))
}()

// peek struct type.
xt := m.PeekValue(1 + el).V.(TypeValue).Type
st := baseOf(xt).(*StructType)
Expand Down
Loading