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

perf: for loop and if #2140

Merged
merged 12 commits into from
Jun 19, 2024
32 changes: 32 additions & 0 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,38 @@ func main() {
m.RunMain()
}

func BenchmarkForLoop(b *testing.B) {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
m := NewMachine("test", nil)
c := `package test
func main() {
for i:=0; i<10000; i++ {}
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)

for i := 0; i < b.N; i++ {
m.RunMain()
}
}

func BenchmarkIfStatement(b *testing.B) {
m := NewMachine("test", nil)
c := `package test
func main() {
for i:=0; i<10000; i++ {
if i > 10 {

}
}
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)

for i := 0; i < b.N; i++ {
m.RunMain()
}
}

func TestDoOpEvalBaseConversion(t *testing.T) {
m := NewMachine("test", nil)

Expand Down
4 changes: 4 additions & 0 deletions gnovm/pkg/gnolang/op_inc_dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (m *Machine) doOpInc() {
panic("expected lv.V to be nil for primitive type for OpInc")
}
}

// here we can't just switch on the value type
// because it could be a type alias
// type num int
switch baseOf(lv.T) {
case IntType:
lv.SetInt(lv.GetInt() + 1)
Expand Down
23 changes: 21 additions & 2 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1729,12 +1729,12 @@
// TRANS_LEAVE -----------------------
case *ForStmt:
// Cond consts become bool *ConstExprs.
checkOrConvertType(store, last, &n.Cond, BoolType, false)
checkWithoutConvertType(store, last, &n.Cond, BoolType, false)

// TRANS_LEAVE -----------------------
case *IfStmt:
// Cond consts become bool *ConstExprs.
checkOrConvertType(store, last, &n.Cond, BoolType, false)
checkWithoutConvertType(store, last, &n.Cond, BoolType, false)

// TRANS_LEAVE -----------------------
case *RangeStmt:
Expand Down Expand Up @@ -2490,6 +2490,25 @@
}
}

func checkWithoutConvertType(store Store, last BlockNode, x *Expr, t Type, autoNative bool) {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
if cx, ok := (*x).(*ConstExpr); ok {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
convertConst(store, last, cx, t)

Check warning on line 2495 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2495

Added line #L2495 was not covered by tests
} else if bx, ok := (*x).(*BinaryExpr); ok && (bx.Op == SHL || bx.Op == SHR) {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
// "push" expected type into shift binary's left operand.
checkWithoutConvertType(store, last, &bx.Left, t, autoNative)

Check warning on line 2498 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2498

Added line #L2498 was not covered by tests
} else if *x != nil { // XXX if x != nil && t != nil {
xt := evalStaticTypeOf(store, last, *x)
if t != nil {
checkType(xt, t, autoNative)
}
if isUntyped(xt) {
if t == nil {
t = defaultTypeOf(xt)

Check warning on line 2506 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2506

Added line #L2506 was not covered by tests
}
}
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
}
}

// like checkOrConvertType(last, x, nil)
func convertIfConst(store Store, last BlockNode, x Expr) {
if cx, ok := x.(*ConstExpr); ok {
Expand Down
Loading