Thursday 15 March 2012

javascript - Is mod required for ClojureScript array performance - or is it simply about bounds checking? -



javascript - Is mod required for ClojureScript array performance - or is it simply about bounds checking? -

lau jensen has fantastic post on getting high performance clojurescript using arrays. 1 of techniques uses array function uses mod function so:

(defn mod [x m] (js* "((x%m)+m)%m")) (defn get-cell [b x y] (js* "b[brianscript.core.mod(y,90)][brianscript.core.mod(x,90)]"))

does mod function special in javascript - or lau not doing bounds check elsewhere , including in function?

my question is: is mod required clojurescript array performance - or bounds checking?

brianscript.core.mod() compiles downwards same code cljs.core.mod(). here compiled clojurescript code:

cljs.core.mod = (function mod(n,d){return (((n % d) + d) % d);

his reimplementation of get-cell unnecessary: original version using aset compile same javascript code.

he cannot utilize js % operator (actually pronounced "remainder", not "modulus") because wants coordinate lookups in neighbor-checking functions "wrap around" board. e.g. if @ coordinate 0, , wants check cell left, index 89, not -1. -1 % 90 = -1, mod(-1, 90) = 89.

if seeing performance benefit writing own mod, not because getting more "down metal" cljs.mod. perchance javascript vm deoptimizing cljs.core.mod because beingness called arguments of inconsistent types across life of program. "cloning" mod function , calling little int arguments, vm might have been able optimize improve , more consistently. speculation, though.

there lot in article wrong. appears in many cases applying java/clojure reasoning javascript/clojurescript incorrectly (he porting application clojure after all). example, section on using multi-dimensional arrays citing java bytecode argue javascript multidimensional arrays faster, not true. (there no multidim arrays in js--perhaps js vm optimize case, way know measure.) in place says:

the real value here [of using numbers instead of keywords], allows utilize int-array, performs 10-12% faster non-typed array.

in clojurescript, int-array (and *-array , make-array functions) aliases normal javascript array. in clojure these produce java primitive arrays of different types, in clojurescript new array(). gets boost in performance because removing overhead of comparing keyword objects instead of numbers, , perchance because vm under-the-hood using more compact array representation because notices total of little integers instead of pointers.

optimizing javascript performance extremely difficult. must benchmark every change, in multiple browsers, using realistic inputs , phone call patterns. different vms optimize different approaches differently , there few rules of thumb work. thing read optimizing js javascript performance madmen, gives taste of how unpredictable js performance can be. should read david nolen's post on optimizing clojurescript jensen cites (david nolen lead clojurescript developer , maintainer).

javascript arrays clojurescript modulus

No comments:

Post a Comment