Skip to content

Instantly share code, notes, and snippets.

@rauhs
Last active June 29, 2017 13:48
Show Gist options
  • Save rauhs/355833f70391a0e82b191220b9aa80ba to your computer and use it in GitHub Desktop.
Save rauhs/355833f70391a0e82b191220b9aa80ba to your computer and use it in GitHub Desktop.

Tricky, we can NOT:

var fxy = gen_dispatcher(fxy)
var fxy.IFn ....

Since otherwise we loose (to be tested) GCC's DCE.

Thoug this should work:

(defn create-multi-arity-fn
  []
  (fn dispatcher []
    #js[(.-cljs$lang$maxFixedArity dispatcher)
        (.-cljs$lang$fixedArities dispatcher)]))

(def foo (create-multi-arity-fn))
(set! foo -cljs$lang$maxFixedArity 2)
(set! foo -cljs$lang$fixedArities #js[0 2])
(foo)

Need to check DCE...

Measuring MFA:

    (defn set-arg-len [f]
      (set! js/yyyyyyy (.. f -cljs$core$IFn$_invoke$arity$variadic -length)))
    (defn set-mfa [f]
      (set! js/xxxxxx (.. f -cljs$lang$maxFixedArity)))

    (let [sims 4000000
          fns #js[map comp some-fn str partial vary-meta not= disj! conj assoc assoc!]
          len (alength fns)]
      (dotimes [_ 3]
        (simple-benchmark []
          (set-arg-len (aget fns (rand-int len))) sims)
        (simple-benchmark []
          (set-mfa (aget fns (rand-int len))) sims)))

Slower in Chrome by like 2x :(

Optimize for inline caches - avoid hidden classes

We could maybe get the same object layout and achieve much higher performance if we set "a few" IFn to nil on the dispatcher. To be tested!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment