NetLogo / Tortoise

Compiler and runtime engine for NetLogo models that runs in JavaScript 🐢
https://netlogoweb.org
Other
56 stars 27 forks source link

Performance of `=`, `!=`, `>=`, and `<=` #245

Open CIVITAS-John opened 1 year ago

CIVITAS-John commented 1 year ago

@Maizi reports that the way we implement the three basic operators could be further optimized.

I did some inspection and at this time, my instinction is that the optimization of operators could be a key part to improve the performance of NLW. Specifically, >= and <= should have their own implementations. It won't be hard:

lte: (a, b) ->
  if (checks.isString(a) and checks.isString(b)) or (checks.isNumber(a) and checks.isNumber(b))
    a <= b
  else if typeof(a) is typeof(b) and a.compare? and b.compare?
    result = a.compare(b) 
    result is LT or result is EQ
  else
    throw exceptions.internal("Invalid operands to `lte`")

For = and !=, my intuition is that the null check is not necessary: if someone send in a null or undefined, just let it be. Since this would only happen when a primitive or an extension primitive accidentally leaks them, it is unnecessary to put those checks in place.

Those are not enough - see the following benchmark: let i 0 let l length arr while[i < l][... set i i + 1] performs much worse than foreach either with or without calculating the index.

However, my proposal is relatively safer to integrate without compromising the symantic integrity of NLW. If we could sacrifice this as well - probably with a compiler option - then we could do even more: override the valueOf and convert everything back to Javascript operators.

CIVITAS-John commented 1 year ago

A quick implementation (not even touch the !=, only >=, <=, and =) on one of my computationally heavy models in TU shows ~8% improvement in performance.