effekt
effekt copied to clipboard
Performance drop with handlers and mutable variables in loops
There is some (to me) strange interaction between effect handlers and mutable variables which leads to a slowdown in runtime performance. The following example works just fine and finishes pretty much immediately
effect Effect(): Unit
def main() = {
val num = 15000;
var count = 0;
try {
while (count < num) {
do Effect();
do Effect();
count = count + 1
}
} with Effect { () =>
resume(())
}
}
If, however, a mutable variable is used inside the loop, as is done here,
effect Effect(): Unit
def main() = {
val num = 15000;
var count = 0;
try {
while (count < num) {
var unused = ();
do Effect();
do Effect();
count = count + 1
}
} with Effect { () =>
resume(())
}
}
execution slows down considerably, it takes several seconds to finish and each additional call to the Effect
operation increases execution time by additional seconds.
On the other hand, having the handler inside the loop, while still using the mutable variable, does not lead to a performance drop:
effect Effect(): Unit
def main() = {
val num = 15000;
var count = 0;
while (count < num) {
try {
var unused = ();
do Effect();
do Effect();
count = count + 1
} with Effect { () =>
resume(())
}
}
}