Closed Bug 1001664 Opened 10 years ago Closed 10 years ago

slow performance on "instanceof" microbenchmark

Categories

(Core :: JavaScript Engine: JIT, defect)

x86_64
Linux
defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: mbrubeck, Unassigned)

References

Details

(Keywords: perf)

On this microbenchmark which simply runs "subObj instanceof Obj" in a loop (where Obj is in the protype chain of subObj), Spidermonkey is much slower than other JavaScript engines:
http://jsperf.com/instanceof-vs-method

The benchmark result for Firefox 30 is about 30 million ops/sec on my 64-bit Linux laptop.  This is about 6x slower than Chrome on the same hardware, and more than 10x slower than Safari on similar hardware.  It's also about 5x slower than calling an Obj.prototype method on subObj.

I don't have any evidence about whether this issue affects any real-world code.
So I can reproduce this, but only in a somewhat-jsperf-like setup where the "var Obj = function () {};" bit happens inside another function.  Without that we're actually faster than Chrome, afaict.

When Obj is defined at global scope, the instanceof gets compiled to a MInstanceOf and hence LInstanceOfO, which inlines the proto chain walk.

When Obj is defined inside another function, the instanceof gets compiled to a MCallInstanceOf and then LCallInstanceOf, which does a VM call.

This is in fact a lot slower, but the scale matters.  On my hardware, the fast version is about 3-4ns while the slow one is 25-30ns.  So unless instanceof is somehow part of the critical path of some code the difference ought not to matter.

The reason we get MCallInstanceOf instead of MInstanceOf is presumably because in jsop_instanceof the proto of the RHS is not a singleton, since it will be different on every function invocation...
Component: JavaScript Engine → JavaScript Engine: JIT
Depends on: 1113643
This is completely fixed by bug 1113643. Well, at least the benchmark isn't applicable anymore, I think: the scores for both instanceof and isObject look suspiciously close to empty loop performance to me.
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.