motoko icon indicating copy to clipboard operation
motoko copied to clipboard

perf: optimize `Array.tabulateVar`

Open crusso opened this issue 2 years ago • 0 comments

using or re-using dedicated prim instread of this code in base Array.mo

   public func tabulateVar<X>(size : Nat, generator : Nat -> X) : [var X] {
    // FIXME add this as a primitive in the RTS
    if (size == 0) { return [var] };
    let array = Prim.Array_init<X>(size, generator 0);
    var i = 0;
    while (i < size) {
      array[i] := generator i;
      i += 1
    };
    array
  };

Right now tabulateVar is an order of magnitude more expensive than tabulate (or so)

https://dfinity.slack.com/archives/CPL67E7MX/p1678194612815229?thread_ts=1678192911.487829&cid=CPL67E7MX

crusso avatar Mar 07 '23 13:03 crusso