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
40 changes: 38 additions & 2 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1702,12 +1702,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 @@ -2463,6 +2463,42 @@
}
}

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 2468 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2468

Added line #L2468 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 2471 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2471

Added line #L2471 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 2479 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2479

Added line #L2479 was not covered by tests
}
// Push type into expr if qualifying binary expr.
if bx, ok := (*x).(*BinaryExpr); ok {
switch bx.Op {
case ADD, SUB, MUL, QUO, REM, BAND, BOR, XOR,
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
BAND_NOT, LAND, LOR:

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2485

Added line #L2485 was not covered by tests
// push t into bx.Left and bx.Right
checkWithoutConvertType(store, last, &bx.Left, t, autoNative)
checkWithoutConvertType(store, last, &bx.Right, t, autoNative)
return
case SHL, SHR:

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2487-L2490

Added lines #L2487 - L2490 were not covered by tests
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
// push t into bx.Left
checkWithoutConvertType(store, last, &bx.Left, t, autoNative)
return

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2492-L2493

Added lines #L2492 - L2493 were not covered by tests
// case EQL, LSS, GTR, NEQ, LEQ, GEQ:
// default:
}
}
}
}
}

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