|
| 1 | +--- |
| 2 | +title: Runtime Polymorphism: Back to Basics by Louis Dionne |
| 3 | +date: 2024-10-14 |
| 4 | +url: https://www.bilibili.com/video/BV1Vx411V7Rd?p=137 |
| 5 | +--- |
| 6 | + |
| 7 | +problems: |
| 8 | + |
| 9 | +- returning related types from a function |
| 10 | +- storing related types in a container |
| 11 | + |
| 12 | +variant sometimes does the trick |
| 13 | + |
| 14 | +- but it only works for closed set of types |
| 15 | +- using visitation is sometimes not convenient |
| 16 | + |
| 17 | +bottom line: manipulating an open set of related types with different representations |
| 18 | + |
| 19 | +inheritance has many problems: |
| 20 | + |
| 21 | +- bakes in reference semantics |
| 22 | +- heap allocations |
| 23 | +- bakes in nullable semantics |
| 24 | +- ownership hell |
| 25 | +- doesn't play well with algorithms |
| 26 | +- intrusive |
| 27 | + |
| 28 | +ref: [Sean Parent](https://youtu.be/QGcVXgEVMJg) |
| 29 | + |
| 30 | +Goal: independent storage and method dispatch |
| 31 | + |
| 32 | +- Storage policy |
| 33 | + - Remote Storage(Just like using dyno::remote_storage) |
| 34 | + - The Small Buffer Optimization: Add a buffer in the type |
| 35 | + - Always-Local Storage: may failed |
| 36 | + - Non-Owning storage |
| 37 | + - Shared Remote Storage |
| 38 | +- VTable policy |
| 39 | + - Remote |
| 40 | + - Inline: Pessimization |
| 41 | + - Partial Vtable Inlining |
| 42 | + |
| 43 | +Remote Storage: strengths and weaknesses |
| 44 | + |
| 45 | +- simple model, similar to classic inheritance |
| 46 | +- Always requires an allocation |
| 47 | + |
| 48 | +The Small Buffer Optimization: strengths and weakness |
| 49 | + |
| 50 | +- does not always require allocating |
| 51 | +- takes up more space |
| 52 | +- Copy/move/swap is more complicated |
| 53 | +- Dispatching may be more costly |
| 54 | + |
| 55 | +Always-Local Storage: strengths and weakness |
| 56 | + |
| 57 | +- No allocation ever |
| 58 | +- Simple dispatching |
| 59 | +- Takes up more space |
| 60 | + |
| 61 | +GuideLines |
| 62 | + |
| 63 | +- use local storage whenever you can afford it |
| 64 | +- otherwise, use SBO with the largest reasonable size |
| 65 | +- Use purely-remote storage only when object sizes are so scattered SBO wouldn't help |
| 66 | +- By default, all methods are in the remote vtable |
| 67 | +- Consider inlining some methods if |
| 68 | + - you have slack space |
| 69 | + - you know you're calling them often |
| 70 | + - you've measured it makes a difference |
| 71 | + |
| 72 | +Summary |
| 73 | + |
| 74 | +- Inheritance is only a choice of run time polymorphism |
| 75 | +- Many ways of storing polymorphic objects |
| 76 | +- Vtable can be inlined |
| 77 | +- Type erasure is tedious to do manually |
| 78 | + |
| 79 | +ref: dyno lib |
| 80 | + |
| 81 | +ref: Zack Laine's CppCon 2014 talk |
0 commit comments