On Thursday, 29 May 2014 at 18:12:59 UTC, Timon Gehr wrote:
On 05/29/2014 06:53 PM, Dylan Knutson wrote:
...
Is there anything so radically different in D than these other
languages, that prevents the implementation of a
run-of-the-mill VM to
eval D code?
No. (In fact, I've written a naive but mostly complete byte
code interpreter in half a week or so last year, as part of an
ongoing recreational D front end implementation effort.)
It just seems strange to me that it's such a problem when
this is basically solved by all scripting languages. And I'm
really not
trying to downplay the difficulty in implementing CTFE in D,
but rather
just figure out why it's so hard to implement in comparison.
CTFE is somewhat intertwined with semantic analysis, which
makes it a little harder to specify/implement than usual
interpreters. However, the performance problem is mostly a
structural issue of the current implementation: DMDs CTFE
interpreter gradually grew out of its constant folder in some
kind of best effort fashion as far as I understand.
It is feasible to do everything in the usual fashion and
occasionally just pause or restart interpretation at
well-defined points where it needs to interface with semantic
analysis.
Exactly. Historically, most of the work I've done on CTFE was in
fixing up the relationship between CTFE and the rest of the
compiler, ironing out all of the weird semantic interactions.
Almost *nothing* has ever been done on the CTFE implementation
itself.
The implementation is the crappiest thing you could imagine, it
leaks memory like BP leaks oil. It's been hard to fix not because
doing a JIT is hard, but because of the semantic interaction
bugs. The good news is that most of those are fixed now.
But, it's worth mentioning that at dconf, CTFE and mixins were
blamed for many things they aren't responsible for.
For example, Phobos takes forever to compile, but it's nothing to
do with CTFE.
Phobos is slow to compile because everything imports everything
else, and it instantiates nearly a million templates.
IE, an infinitely fast CTFE engine would make very little
difference to Phobos compile times.