Skip to content

Instantly share code, notes, and snippets.

@fasterthanlime
Last active August 29, 2015 14:10
Show Gist options
  • Save fasterthanlime/3fb032a06ae22df53604 to your computer and use it in GitHub Desktop.
Save fasterthanlime/3fb032a06ae22df53604 to your computer and use it in GitHub Desktop.
require 'shin/compiler'
require 'shin/js_context'
require 'shin/ast'
require 'benchmark/ips'
opts = {}
compiler = Shin::Compiler.new(opts)
core_path = "../shin/lib/shin/cljs/cljs/core.cljs"
compiler.compile(File.read(core_path))
ctx = Shin::JsContext.new
ctx.providers << compiler
ctx.load("cljs.core")
c = ctx.context
res = c.eval(%Q{
var core = $kir.modules["cljs.core"].exports;
var c = core.cons, s = core.symbol,
k = core.keyword, l = core.list,
h = core.hash$_map, v = core.vector;
(function () {
return core.take(8,
core.interleave(core.repeat(42),
core.repeat("Yay"),
core.repeat(k("some-keyword")),
core.repeat(s("some-symbol")),
core.repeat(v()),
core.repeat(l()),
core.repeat(h())
)
);
}) })
res = res.call
_to_array = c.eval("$kir.modules['cljs.core'].exports.to$_array")
arr = _to_array.call(res)
BIG_MAP = {}
def register_type(c, map, cname, type)
cons = c.eval("$kir.modules['cljs.core'].exports.#{cname}")
raise if cons.nil?
id = cons.native.GetIdentityHash
puts "Registering #{id} => #{type}"
BIG_MAP[id] = type
end
c.enter do
register_type(c, BIG_MAP, "Keyword", :keyword)
register_type(c, BIG_MAP, "Symbol", :symbol)
register_type(c, BIG_MAP, "List", :list)
register_type(c, BIG_MAP, "EmptyList", :list)
register_type(c, BIG_MAP, "PersistentVector", :vector)
register_type(c, BIG_MAP, "PersistentArrayMap", :list)
end
def core_proto_name(proto)
"cljs$dcore$v#{proto}"
end
def v8_type(val)
case true
when val[core_proto_name("IVector")]
:vector
when val[core_proto_name("ISeq")]
:list
when val[core_proto_name("ISymbol")]
:symbol
when val[core_proto_name("IKeyword")]
:keyword
when val[core_proto_name("IMap")]
:map
when val[core_proto_name("IUnquote")]
:unquote
else
:unknown
end
end
def bench_v8ty(arr)
arr.each do |elm|
case elm
when Fixnum, Float, String, true, false, nil
# muffin
when V8::Object
v8_type(elm)
end
end
end
def bench_natv(c, arr)
c.enter do
arr.each do |elm|
case elm
when Fixnum, Float, String, true, false, nil
# muffin
when V8::Object
cons = elm['constructor']
id = cons.native.GetIdentityHash
type = BIG_MAP[id]
unless type
cname = elm.native.GetConstructorName.to_ruby
raise "Unknown constructor: #{cname}, id = #{id}" unless type
end
end
end
end
end
Benchmark.ips do |x|
x.report("v8ty") { bench_v8ty(arr) }
x.report("natv") { bench_natv(c, arr) }
x.compare!
end
Registering 1866752856 => keyword
Registering 1221673719 => symbol
Registering 162188612 => list
Registering 1095229526 => list
Registering 3882202 => vector
Registering 342594901 => list
Calculating -------------------------------------
v8ty 86.000 i/100ms
natv 172.000 i/100ms
-------------------------------------------------
v8ty 899.996 (±18.6%) i/s - 4.214k
natv 1.690k (±11.5%) i/s - 8.256k
Comparison:
natv: 1689.6 i/s
v8ty: 900.0 i/s - 1.88x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment