On 3/1/2014 1:00 PM, Steve Teale wrote:
I have already dealt
with the yada-yada cases by old-fashioned OOP.


As I see it, a big part of the benefit of templates is that they can help you avoid a lot of the downsides of OOP:

- OO Boilerplate.
- Multiple dispatch is ridiculously messy, having to resort to contortions like the visitor pattern.
- Upcasting looses compile-time type info.
- Decreased opportunity for compiler optimizations, because a single function handles multiple data types at runtime. So the optimizer can't generate optimal code for all the data types used.
- Extra indirections at runtime.
- Downcasting has a runtime cost.
- Poor cache behavior.
- Tendency for increased heap activity, which is not cheap at all.
- Lumping all data/functionality for a single "object" instance into the same physical chunk of memory causes problems for parallelization. And that's increasingly problematic on modern processors which work best when operating as "streaming-data processors". (See "Entity/Component Systems"[1] - There are good reasons videogame development has mostly switched from OOP to entity/component systems.)

Basically, while OOP certainly has benefits, it also has notable flaws that inherently slow down both the programmer and the computer. JVM goes to an enormous amount of trouble to mitigate that natural tendency, but it still has limits. And most languages (like D) can't expect to approach JVM's work on de-slow-ifying OO.

The runtime performance issues of OO aren't *always* a problem, but they can easily kill inner-loop/performance-sensitive sections of code. So with OO, you have to give up the generic/polymorphic benefits for any critical sections. Metaprogramming lets you still be generic even in the critical sections. This is demonstrated in sections #1-4 of an article I wrote a little while back[2]. It's a very contrived example, but even I was still surprised just *how* badly the OO version performed.

There's also this template primer[3] which might help, but I'm guessing it may be below your ability level.

[1] Entity/Component Systems: http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/

[2] http://www.semitwist.com/articles/EfficientAndFlexible/MultiplePages/Page1/

[3] https://semitwist.com/articles/article/view/template-primer-in-d

Reply via email to