Issue #11768 has been reported by Aaron Patterson.

----------------------------------------
Bug #11768: Add a polymorphic inline cache
https://bugs.ruby-lang.org/issues/11768

* Author: Aaron Patterson
* Status: Open
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: 
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
Hi,

I've attached a patch that adds a PIC to the existing Mono IC struct.

I haven't run every benchmark that's checked in, but this patch speeds up the polymorphic call benchmark by about 20%.  Here is the benchmark *before* my patch:

~~~
[aaron@TC ruby (trunk)]$ time ./ruby benchmark/bm_vm2_poly_method.rb 

real	0m3.244s
user	0m3.154s
sys	0m0.044s
[aaron@TC ruby (trunk)]$ time ./ruby benchmark/bm_vm2_poly_method.rb 

real	0m3.158s
user	0m3.090s
sys	0m0.042s
[aaron@TC ruby (trunk)]$ time ./ruby benchmark/bm_vm2_poly_method.rb 

real	0m3.162s
user	0m3.099s
sys	0m0.039s
~~~

Here it is with my patch applied:

~~~
[aaron@TC ruby (pic2)]$ time ./ruby benchmark/bm_vm2_poly_method.rb 

real	0m2.522s
user	0m2.455s
sys	0m0.044s
[aaron@TC ruby (pic2)]$ time ./ruby benchmark/bm_vm2_poly_method.rb 

real	0m2.515s
user	0m2.458s
sys	0m0.035s
[aaron@TC ruby (pic2)]$ time ./ruby benchmark/bm_vm2_poly_method.rb 

real	0m2.637s
user	0m2.545s
sys	0m0.045s
~~~

Monomorhic call sites maintain the same performance:

Before:

~~~
[aaron@TC ruby (trunk)]$ time ./ruby benchmark/bm_vm2_method.rb 

real	0m1.416s
user	0m1.371s
sys	0m0.032s
[aaron@TC ruby (trunk)]$ time ./ruby benchmark/bm_vm2_method.rb 

real	0m1.456s
user	0m1.402s
sys	0m0.032s
[aaron@TC ruby (trunk)]$ time ./ruby benchmark/bm_vm2_method.rb 

real	0m1.420s
user	0m1.372s
sys	0m0.032s
~~~

After:

~~~
[aaron@TC ruby (pic2)]$ time ./ruby benchmark/bm_vm2_method.rb 

real	0m1.451s
user	0m1.399s
sys	0m0.033s
[aaron@TC ruby (pic2)]$ time ./ruby benchmark/bm_vm2_method.rb 

real	0m1.494s
user	0m1.438s
sys	0m0.033s
[aaron@TC ruby (pic2)]$ time ./ruby benchmark/bm_vm2_method.rb 

real	0m1.466s
user	0m1.416s
sys	0m0.032s
~~~

The down side of this patch is that it increases memory usage because the size of the call cache struct gets larger, even if the call site is monomorphic.  I think we could make the code expand and contract, but I'm not sure if it's worthwhile.  The other downside is that it will probably slow down calls if the global method state changes, but I don't think that is a situation we should optimize for.

I've actually attached 2 patches, one adds the PIC, the other adds a tracepoint so that I could log cache hit / miss rates.

---Files--------------------------------
0001-add-PIC.patch (2.9 KB)
0002-add-a-tracepoint-for-PIC-hit-miss.patch (4.33 KB)


-- 
https://bugs.ruby-lang.org/