Skip to content

Commit

Permalink
corecpp slides
Browse files Browse the repository at this point in the history
  • Loading branch information
jll63 committed Nov 28, 2024
1 parent d3a180a commit dadf3fe
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 118 deletions.
Binary file added docs/slides/YOMM2-corecpp.pdf
Binary file not shown.
15 changes: 4 additions & 11 deletions docs/slides/deck/01-title.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<!-- .slide: class="title" -->
<!-- .slide: class="center" -->

# Open Is Good


YOMM2: Fast, Orthogonal Open (Multi) Methods

<div class="title-author">
<!--VERSION-->
<small>
Jean-Louis Leroy - jl@leroy.nyc<br/>
</small>
</div>
<section
data-background-image="/resources/corecpp2024.png"
data-background-size="100%">
</section>
8 changes: 5 additions & 3 deletions docs/slides/deck/04-open-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ define_method(int, value, (Plus& expr)) {
mov rdx, qword ptr [rip+fast_perfect_hash<release>::mult]
imul rdx, qword ptr [rax-8]
mov cl, byte ptr [rip+fast_perfect_hash<release>::shift]
mov cl, byte ptr [rip+fast_perfect_hash<release>::shift]
shr rdx, cl
mov rax, qword ptr [rip+vptr_vector<release>::vptrs]
Expand All @@ -118,8 +118,10 @@ define_method(int, value, (Plus& expr)) {
jmp qword ptr [rax+8*rcx]
```

* 15-30% slower than equivalent native virtual function call (using perfect
integer hash; but see `virtual_ptr`)
* llvm-mca: 4.2 cycles

* benchmarks (google, rdtsc): 15-65% slower than equivalent native virtual
function call (using perfect integer hash; but see `virtual_ptr`)

* [Optimizing Away C++ Virtual Functions May Be
Pointless](https://www.youtube.com/watch?v=i5MAXAxp_Tw) - Shachar Shemesh -
Expand Down
122 changes: 40 additions & 82 deletions docs/slides/deck/05-inside.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Inside yomm2
# Inside YOMM2

<!-- .slide: class="title" -->
<!-- .slide: class="center" -->
Expand All @@ -8,9 +8,9 @@

## Inside YOMM2

* purely in C++17 (no extra tooling)
* pure C++17 (no extra tooling)

* constant time dispatch
* "constant" time dispatch (proportional to #vargs)

* uses tables of function pointers

Expand Down Expand Up @@ -57,23 +57,7 @@ declare_method(double, pay, (virtual_<Employee&>));

```C++
struct YoMm2_S_pay;
yomm2::method<
YoMm2_S_pay, double(virtual_<const Employee&>),
yomm2::default_policy>
pay_yOMM2_selector_(
yomm2::detail::remove_virtual<virtual_<const Employee&>> a0);
```

## declare_method
```C++
declare_method(double, pay, (virtual_<Employee&>));
```


```C++
inline double
pay(yomm2::detail::remove_virtual<virtual_<const Employee&>> a0) {
return yomm2::method<
Expand All @@ -83,6 +67,12 @@ pay(yomm2::detail::remove_virtual<virtual_<const Employee&>> a0) {
yomm2::detail::remove_virtual<virtual_<const Employee&>>>(
a0));
};

yomm2::method<
YoMm2_S_pay, double(virtual_<const Employee&>),
yomm2::default_policy>
pay_yOMM2_selector_(
yomm2::detail::remove_virtual<virtual_<const Employee&>> a0);
```
Expand All @@ -108,17 +98,6 @@ struct _yOMM2_spec {
};
_yOMM2_method::add_function<_yOMM2_spec::yOMM2_body>
YoMm2_gS_11(&next, typeid(_yOMM2_spec).name()); } }
```
## define_method
```C++
define_method(double, pay, (Employee&)) { return 3000; }
```

```C++
YoMm2_gS_10::_yOMM2_method::return_type
YoMm2_gS_10::_yOMM2_spec::yOMM2_body(const Employee&) {
return 3000;
Expand All @@ -129,13 +108,13 @@ YoMm2_gS_10::_yOMM2_spec::yOMM2_body(const Employee&) {
## update
uses the class and method info registered by static ctors
* uses class and method info registered by static ctors
* build representation of class hierarchies
* builds a representation of class hierarchies
* calculate the hash and dispatch tables
* builds dispatch tables
* find a perfect (not minimal) hash function for the `type_info`s
* finds a perfect (not minimal) hash function for the `type_info`s
* H(x) = (M * x) >> S
Expand All @@ -155,7 +134,7 @@ uses the class and method info registered by static ctors
during `update`
```C++
method<pay>::slots_strides.i = 1;
method<pay>::slots_strides[] = { 1 };
// method table for Employee
mtbls[ H(&typeid(Employee)) ] = {
Expand All @@ -179,9 +158,9 @@ pay(bill)
```
=>
```C++
mtbls[ H(&typeid(bill)) ] // mtable for type
[ method<pay>::slots_strides.i ] // pointer to fun
(bill) // call
mtbls[ H(&typeid(bill)) ] // mtable for type
[ method<pay>::slots_strides[0] ] // pointer to fun
(bill) // call
```


Expand All @@ -193,15 +172,15 @@ double call_pay(Employee& e) { return pay(e); }
```
```asm
mov r8, qword ptr [rip + context+24] ; hash table
mov rdx, qword ptr [rip + context+32] ; M
mov cl, byte ptr [rip + context+40] ; S
movsxd rsi, dword ptr [rip + method<pay>::fn+96] ; slot
mov rax, qword ptr [rdi] ; vptr
imul rdx, qword ptr [rax - 8] ; M * &typeid(e)
shr rdx, cl ; >> S
mov rax, qword ptr [r8 + 8*rdx] ; method table
jmp qword ptr [rax + 8*rsi] ; call wrapper
mov rax, qword ptr [rdi] ; vptr
mov rdx, qword ptr [rip + hash_mult] ; M
imul rdx, qword ptr [rax - 8] ; M * &typeid(e)
movzx ecx, byte ptr [rip + hash_shift] ; S
shr rdx, cl ; >> S
mov rax, qword ptr [rip + vptrs] ; vptrs
mov rax, qword ptr [rax + 8*rdx] ; vptr
mov rcx, qword ptr [rip + slots_strides] ; slot
jmp qword ptr [rax + 8*rcx]
```


Expand Down Expand Up @@ -235,15 +214,15 @@ define_method(bool, approve, (Founder& r, Expense& e, double amount)) {
* it's a little more complicated
* use a multi-dimensional dispatch table
* uses a multi-dimensional dispatch table
* size can grow very quickly
* the table must be "compressed", devoid of redundancies
* table must be "compressed", devoid of redundancies
* in fact the "uncompressed" table never exists
* work in terms of class _groups_, not classes
* works in terms of class _groups_, not classes
Expand Down Expand Up @@ -271,7 +250,7 @@ define_method(bool, approve, (Founder& r, Expense& e, double amount)) {
## Dispatching a Multi-Method
```C++
method<approve>::.slots_strides.pw = { 0, 4, 0 };
method<approve>::.slots_strides = { 0, 4, 0 };
mtbls[ H(&typeid(Employee)) ] = {
// & of (Employee,Expense+Jet) cell
Expand All @@ -292,59 +271,38 @@ mtbls[ H(&typeid(Cab)) ] = { 2 };
## Dispatching a Multi-Method

```C++
approve(bill, ticket, 5000)
approve(bill, ticket, 6)
```
=>
```C++
word* slots_strides = method<approve>::.slots_strides.pw;
std::uintptr_t* slots_strides = method<approve>::.slots_strides;
mtbls[ H(&typeid(bill)) ] // method table for bill
[ slots_strides[0].i ] // ptr to cell in 1st column
[ slots_strides[0] ] // ptr to cell in 1st column
[ mtbls [ H(&typeid(ticket)) ] // method table for ticket
[ slots_strides[2].i ] // column
* slots_strides[1].i // stride
[ slots_strides[2] ] // column
* slots_strides[1] // stride
] // pointer to function
(bill, ticket, 5000) // call
(bill, ticket, 6) // call
```



## Benchmarks

| | | gcc6 | clang6 |
|---------------------|----------|------|--------|
| normal inheritance | | | |
| virtual function | 1-method | 16% | 17% |
| double dispatch | 2-method | 25% | 35% |
| virtual inheritance | | | |
| virtual function | 1-method | 19% | 17% |
| double dispatch | 2-method | 40% | 33% |



## yomm2 vs other systems
## YOMM2 vs other systems

* Pirkelbauer - Solodkyi - Stroustrup (PSS)
* yomm11
* Cmm
* Loki / Modern C++



## yomm2 vs PSS
## YOMM2 vs PSS

* Solodkyi's papers on open methods etc.:
* [Open Multi-Methods for C++](http://www.stroustrup.com/multimethods.pdf)
* [Design and Evaluation of C++ Open Multi-Methods](https://parasol.tamu.edu/~yuriys/papers/OMM10.pdf)
* [Simplifying the Analysis of C++ Programs](http://oaktrust.library.tamu.edu/bitstream/handle/1969.1/151376/SOLODKYY-DISSERTATION-2013.pdf)
* PSS attempts harder to resolve ambiguities
* yomm2 overrides not visible as overloads, cannot specialize multiple methods
* yomm2 supports smart pointers, `next`



## yomm2 vs yomm11

* no need to instrument classes

* methods are ordinary functions
* YOMM2 overrides not visible as overloads, cannot specialize multiple methods
* YOMM2 supports smart pointers, `next`
58 changes: 36 additions & 22 deletions docs/slides/deck/06-evolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@



## Past

<br/>


## Past

<br/>


## Past

<br/>
Expand Down Expand Up @@ -38,8 +48,8 @@

<ul>
<li class="fragment">2020: give up on adoption in the standard</li>
<li class="fragment">new developments
</li>
<li class="fragment">new features</li>
<li class="fragment">virtual_ptr</li>
</ul>


Expand All @@ -49,6 +59,10 @@

<br/>

```c++
declare_method(int, value, (virtual_ptr<Node>));
```
```c++
int call_via_vptr(virtual_ptr<const Node> node) {
return value(node);
Expand All @@ -61,12 +75,6 @@ mov rax, qword ptr [rsi + 8*rax]
jmp rax
```

<br/>

```c++
declare_method(int, value, (virtual_ptr<Node>));
```



Expand Down Expand Up @@ -121,6 +129,17 @@ ret



## New Features

<br/>

<ul>
<li>virtual_ptr</li>
<li class="fragment">core API</li>
</ul>



## Core API

```c++
Expand Down Expand Up @@ -153,23 +172,25 @@ YOMM2_STATIC(use_classes<Node, Number, Plus, Times>);
## Present
## New Features
<br/>
<ul>
<li>virtual_ptr</li>
<li>core API</li>
<li class="fragment">template interop toolkit</li>
<li class="fragment">header only (Compiler Explorer)</li>
<li class="fragment">header only</li>
<li class="fragment">friendship</li>
<li class="fragment">member methods</li>
<li class="fragment">policies and facets (latest release)
<li class="fragment">policies and facets
<ul>
<li class="fragment">custom RTTI</li>
<li class="fragment">custom error handling, trace, vptr placement...</li>
</ul>
</li>
<li class="fragment">match (beat?) virtual function speed</li>
<li class="fragment">pre-calculate dispatch tables</li>
<li class="fragment">vcpkg, Conan, Compiler Explorer</li>
</ul>
Expand All @@ -179,14 +200,7 @@ YOMM2_STATIC(use_classes<Node, Number, Plus, Times>);
<br/>
<ul>
<li class="fragment">goals:
<ul>
<li class="fragment">match (beat?) virtual function speed</li>
<li class="fragment">pre-calculate dispatch tables</li>
<li class="fragment">malloc-free operation</li>
<li class="fragment">dispatch on std::any</li>
<li class="fragment">(feature complete)</li>
<li class="fragment">C++20</li>
</ul>
</li>
<li class="fragment">Boost.OpenMethod ?</li>
<li class="fragment">C++20</li>
<li class="fragment">C++26 (reflection)</li>
</ul>
Binary file added docs/slides/resources/corecpp2024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit dadf3fe

Please sign in to comment.