Skip to content

Commit 60d09ef

Browse files
committed
make estimation faster by using a table
1 parent cf666b4 commit 60d09ef

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

vm/vm.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -668,23 +668,22 @@ func estimateFnArgsCount(program *Program) int {
668668
// operations, but this is just an estimation
669669
var count int
670670
for _, op := range program.Bytecode {
671-
switch op {
672-
case OpCall1:
673-
count++
674-
case OpCall2:
675-
count += 2
676-
case OpCall3:
677-
count += 3
678-
case OpCallN:
679-
// we don't know exactly but we know at least 4, so be conservative
680-
// as this is only an optimization and we also want to avoid
681-
// excessive preallocation
682-
count += 4
683-
case OpCallFast, OpCallSafe:
684-
// here we don't know either, but we can guess it could be common to
685-
// receive up to 3 arguments in a function
686-
count += 3
671+
if int(op) < len(opArgLenEstimation) {
672+
count += opArgLenEstimation[op]
687673
}
688674
}
689675
return count
690676
}
677+
678+
var opArgLenEstimation = [...]int{
679+
OpCall1: 1,
680+
OpCall2: 2,
681+
OpCall3: 3,
682+
// we don't know exactly but we know at least 4, so be conservative as this
683+
// is only an optimization and we also want to avoid excessive preallocation
684+
OpCallN: 4,
685+
// here we don't know either, but we can guess it could be common to receive
686+
// up to 3 arguments in a function
687+
OpCallFast: 3,
688+
OpCallSafe: 3,
689+
}

0 commit comments

Comments
 (0)