Microbenchmarks are infamously difficult to get right as often you're not
testing what you think you're testing.
Are you sure the optimizer isn't just throwing away code in some cases,
since you're not actually doing any work with the `done` property?
There's no reason that your code even has to run unless I'm reading it
wrong. And it's not like C++ where you can look at the generated
instructions to see what the optimizer is doing..
On Friday, September 22, 2017 at 9:24:00 PM UTC-7, B. Orlov wrote:
>
> Take a look at commonly used in oop inheritance pattern for extending base
> class:
>
> class Animal {
> constructor(){
> this.done = 0
> }
> doIt(){
> this.done++
> }
> }
>
> class Cat extends Animal {}
> class Dog extends Animal {}
> class Dog1 extends Animal {}
> class Dog2 extends Animal {}
> class Dog3 extends Animal {}
>
> function testAnimal(animal){
> for(var i = 0; i < 100000; i++){
> animal.doIt();
> }
> }
>
>
> function test(){
> var cat = new Cat();
> testAnimal(cat)
> var dog = new Dog();
> testAnimal(dog)
> var dog1 = new Dog1();
> testAnimal(dog1)
> var dog2 = new Dog2();
> testAnimal(dog2)
> var dog3 = new Dog3();
> testAnimal(dog3)
> }
>
> test()
>
> Running in latest node (6.0.287.53 v8 version) I get the following results:
> Invoking testAnimal function with first descendant class Cat, V8 does a
> great job by compiling to doIt() method and "this.done++" incrementation to
> REX.W asm instruction without any calls to slow-runtime c++ function for
> generic field access.Than, by invoking doIt() on second descendant class
> Dog, V8 fall down to doIt() method deoptimization (and all outer functions
> which also were inlined) and add few ams rew.x and one jz instruction to
> check hidden map for second Dog class. Than on invoking doIt() on each new
> subclass v8 again fall down to deoptimization and add new checks for new
> hidden map and finally on fourth descendant class Dog3 v8 give up and for
> "this.done++" goes to call LoadICTrampoline StoreICStrictTrampoline c++
> runtime function for generic access. I hope its only bug and v8 can
> efficient deal with inheritance and accessing field without slow runtime
> generic filed access otherwise the big question came in - is "extend"
> keyword a performance antipattern and why v8 can't implement something like
> c++ v-table mechanism ?
>
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.