r/Compilers • u/General_Purple3060 • Aug 20 '26
AET's generic AArray is now faster than C++ std::vector
A while ago I posted about AET's approach to generics:
Delayed Specialization: A Third Way to Implement Generics?
One of the comments raised an concern: AET replaces a generic block with a function pointer call. Would that hurt optimization and performance?
I didn't have a good answer at the time.
It turned out to be a very good question.
AET has a generic container called `AArray`. In my initial implementation, operations such as `add`, `insert` and `remove` were noticeably slower than C++ `std::vector`.
The function-pointer call was one of the things I started looking at.
After about a month of compiler work, I changed how AET handles these calls during specialization. The generic code can now be optimized much more like normal concrete code.
I reran the same benchmark.
The result surprised me.
With 100 million sequential insertions:
AET AArray ~224 ms
C++ std::vector ~504 ms
```
AET is about 2.25× faster in this test.
With preallocated storage:
AET AArray ~154 ms
C++ std::vector ~223 ms
```
About 45% faster.
For middle insertion and middle erase, the two are now roughly at the same level. Tail erase is still slightly faster in `std::vector`.
There is still a trade-off I haven't fully solved.
If I want better runtime performance, I need to let AET see the concrete code so that it can inline it and run more optimization passes.
But this also means generating more specialized code, which can increase code size and compilation time.
If a generic block doesn't benefit much from inlining or further optimization, keeping the original function-pointer call may actually be the better choice.
So the real question is not simply "should AET inline generic blocks?"
It is:
When should AET inline and specialize, and when should it keep the function-pointer call?
I think this needs another mechanism to make that decision.
I don't have a good solution for that yet, so I'd be interested in how others would approach it.
5
u/eteran Aug 20 '26 edited Aug 20 '26
The preallocated storage benchmark is interesting/confusing.
When preallocated, a vector pushback is basically just a pointer deref and incrementing size.
Can you show some.of the code so we can see that it's an apples to apples comparison? And also it would be interesting to hear about how you beat such a cheap operation.
3
u/matthieum Aug 20 '26
Have you ever heard of the Constant Propagation optimization?
Confusingly, it's not about propagating a constant in a function body. Instead, it's about duplicating a function to specialize it for a specific constant argument.
So for example, a call
foo(a, 42)is rewritten tofoo_1_constprop(a)where the latter is a copy/paste offoowith the second argument hard-coded to 42.As far as I am concerned, monomorphization is just constant propagation for v-tables, so you may want to have a look at the kind of heuristics used to decide whether constant propagation seems worth it, or not.
Naively, I'd annotate a function with an opportunity "profit" for each template parameter.
So, imagining a
Vec<T, A>implementation (Rust):fn is_empty(&self) -> bool { self.len == 0 }=> 0 profit in monomorphizing.fn at(&self, index: usize) -> &T { ... }=> some profit in monomorphizing onTsize, 0 profit in monomorphizing onA.fn push(&mut self, e: T) { ... }=> some profit in monomorphizing onTsize, close-to-0 profit in monomorphizing onAif the developer hinted (likely,cold) correctly.5
u/tending Aug 20 '26
"I benchmarked this wrong"
1
u/JVApen Aug 20 '26
Which compiler do you use. Which standard library implementation? Which C++ standard?
3
u/shrimpster00 Aug 20 '26
To be completely honest, I wrote a pretty long comment that was a little inflammatory and decided not to post it after reading it over again.
It's an interesting problem: how do you know when a generic function should be generated multiple times for different concrete types vs. sharing a single implementation with a vtable lookup for each of your generic blocks. If only you knew ahead of time whether duplicating + inlining as type concretization would cause sufficient gains to make it worth your while.
But, if you ask me, that's why compilers have so many flags to enable/disable/tweak heuristic parameters on all sorts of optimizations. I'd inline by default on
-O2or above, never in-Osor-Oz, and maybe develop better heuristics with benchmarking.1
u/adityazero Aug 21 '26
> A while ago I posted about AET's approach to generics:
link?
> I reran the same benchmark.
without the link there is no way to tell what you are doing.