alan icon indicating copy to clipboard operation
alan copied to clipboard

Detect cpu-bound closures and run them accordingly

Open depombo opened this issue 4 years ago • 1 comments

Detect when the closure method for condfn and linear array methods (mapl, findl, etc.) are handed cpu bound operations

depombo avatar Dec 14 '20 18:12 depombo

I started doing this change and realized it would take a huge refactor of the AVM to be able to recursively determine if nested closures are io or cpu in order for nested arrays and conditionals to work. Furthermore, with the recent event and array parallelization improvements in tokio usage (https://github.com/alantech/alan/pull/384, https://github.com/alantech/alan/pull/385) the performance gain is no longer drastic when compared to the complexity of the change. Running a benchmark for the following code:

from @std/app import start, print, exit
from @std/http import get

on start {
  const url = "https://raw.githubusercontent.com/ledwards/advent-2020/main/data/expenses.txt";
  // const expensesTest = [1721, 979, 366, 299, 675, 1456];
  const expenses = get(url).getOr("").trim().split("\n").map(toInt64);

  expenses.eachLin(fn (a: int64) {
    expenses.eachLin(fn (b: int64) {
      const c = expenses.findLin(fn (cc: int64): bool = a + b + cc == 2020);
      if c.isOk() {
        print(a * b * c.getOr(0));
        emit exit 0;
      }
    });
  });

  emit exit 0;
}

on current main

$ time ./avm/target/release/alan run ../out1.agc 
143933922

real	0m13.177s
user	0m11.824s
sys	0m0.638s

versus https://github.com/alantech/alan/commit/63c35b4a65a1a683f15c42701ee3450cef93ce22

$ time ./avm/target/release/alan run ../out1.agc 
143933922

real	0m7.929s
user	0m7.190s
sys	0m0.593s

depombo avatar Jan 21 '21 01:01 depombo