Skip to content

Instantly share code, notes, and snippets.

@rauhs
Last active June 30, 2017 09:04
Show Gist options
  • Save rauhs/becde4ada04378b390dc7f735608b3e1 to your computer and use it in GitHub Desktop.
Save rauhs/becde4ada04378b390dc7f735608b3e1 to your computer and use it in GitHub Desktop.
(ns v8.core
"
Start Chrome with:
google-chrome-beta --js-flags=\"--allow-natives-syntax\"
")
(defn v8
"Runs a v8 natives syntax command with eval."
([c]
(try
(js/eval (str "%" c "()"))
(catch :default _ :eval-fail)))
([c a]
(try
(js/eval (str "%" c "(a)"))
(catch :default _ :eval-fail)))
([c a b]
(try
(js/eval (str "%" c "(a,b)"))
(catch :default _ :eval-fail))))
#_(v8 "GetV8Version")
(defn ifn [f n]
(aget f (str "cljs$core$IFn$_invoke$arity$" n)))
(defn same-map?
"If two object have the same hidden class. Very desirable."
[a b]
(v8 "HaveSameMap" a b))
(defn v8-version []
(v8 "GetV8Version"))
#_(v8-version)
(defn fast-props?
[x]
{:fast-props (v8 "HasFastProperties" x)
:fast-SMI-el (v8 "HasFastSmiElements" x)
;; If obejcts are in the array:
:fast-obj-el (v8 "HasFastObjectElements" x)
;; For double arrays:
;:fast-double (v8* "HasFastDoubleElements" x)
:fast-dict-el (v8 "HasDictionaryElements" x)
;; For arrays/objects that have holes?
:fast-holey-el (v8 "HasFastHoleyElements" x)})
(defn sloppy-args?
"If the fn does something silly and leaks arguments. We don't do that."
[x]
(v8 "HasSloppyArgumentsElements" x))
(defn opt-fn!
"
optimizedFunctionOnNextCall(func) needs the function ran before it can tag it for optimization.
So the procedure is:
- Run your function
- Run your function (Have to do this TWICE)
- Tag your function for optimization
- Run your Function
Verify that the v8 Engine optimized it. If it did not optimized it; then that means you have code
that can't be optimized in it."
[f]
(v8 "OptimizeFunctionOnNextCall" f))
#_(opt-status apply)
#_(opt-fn! apply)
#_(.call apply nil + 1 2 3 [1 2])
(defn never-opt-fn?
[f]
(v8 "NeverOptimizeFunction" f))
(defn opt-status
"Note: This is vor v8 v5.9.211. It looks like the bits were different for older versions."
[f]
;; SEE:
;; https://github.com/v8/v8/blob/master/src/runtime/runtime.h
;; https://github.com/v8/v8/blob/master/test/mjsunit/mjsunit.js
(let [c (v8 "GetOptimizationStatus" f)]
(sorted-map
:_status c
:fn? (pos? (bit-and c 0x01))
:never-opt (pos? (bit-and c 0x02))
:always-opt (pos? (bit-and c 0x04))
:maybe-deopted (pos? (bit-and c 0x08))
:optimized (pos? (bit-and c 0x10))
:turbo-fanned (pos? (bit-and c 0x20))
:crank-shafted (zero? (bit-and c 0x20))
:interpreted? (pos? (bit-and c 0x40)))))
#_(opt-status (ifn map 3))
;; At some point it'll get turbo-fanned
#_(dotimes [_ 10000]
(map vector (range) (range 10)))
(defn smi?
"Is it a fast signed integer?"
[x]
(v8 "_IsSmi" x))
#_(smi? 2)
(comment
(fast-props? map)
(fast-props? (map #()))
(js/Object.keys {})
(fast-props? {})
(js/Object.keys (lazy-seq [0]))
(fast-props? (lazy-seq [0]))
(js/Object.keys (comp inc inc))
(js/Object.keys (comp inc inc +))
;; Cool: We get the same hidden class even as long as we have the same
;; object layout, EVEN when generated with completely different code!
(same-map? (comp inc inc) (comp + + -)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment