Vyacheslav,
When I run the code you posted, I see a much bigger discrepancy
between test/test2 in the first pass and a slight reduction in test's
time but still a large discrepancy the second pass (indicating OSR
happened during the first loop the first time around), similar to what
I was seeing yesterday. But that's running on Node.js, and I haven't
re-built Node.js against the latest stable v8 code, but that issue is
completely gone in the current nightly Canary build.
I think I better understand the method issue now. V8 actually treats
methods set on this. differently than other properties, the assembly
generated looks aggressively inlined. If you cheat and set this.test
to a number then to the method, it effectively disables those
optimizations in V8 and you end up treating the object as a normal
object, and even though it doesn't cause deoptimizations (all objects
have the same hidden class), it's significantly slower than the
inlined method call. The real issue in my example is that test is per-
object and runTest is static, if runTest was assigned via this., it
should only ever see one hidden class, unless you do something evil
like .apply.
Though this test seems to indicate that this only occurs when building
the hidden class:
http://pastebin.com/JbuLaEUt
Even though it never deoptimizes, I'd expect each of those to have
similar performance, but only the first Foobar created is performant.
On a related note, has there been any consideration for making v8 not
de-optimize when a hidden class is ancestral to another (and therefore
compatible)? I mean if you have {a: 7, b: 7} and you have a really
hot loop that only touches a and b, then you add a c property, because
it was transitioned from the proper hidden class for that hot loop to
a superclass of it (with the same indices in its property access
table), that hot function can assume it's the {a, b} hidden class.
This is similar to how classical inheritance works (Foo extends Bar,
functions that operate on Bar can also operate on Foo), but in this
case a hidden class transition is a strict superset, which lets you
make really nice assumptions.