pure-ifying my code

Dmitry Olshansky dmitry.olsh at gmail.com
Mon Nov 18 00:57:18 PST 2013


18-Nov-2013 11:37, Jonathan M Davis пишет:
> On Monday, November 18, 2013 08:22:37 qznc wrote:
>> On Sunday, 17 November 2013 at 21:00:16 UTC, Jonathan M Davis
>>
>> wrote:
>>> will definitely result in multiple calls to pure_func. It's not
>>> that it's
>>> impossible for the compiler to do it - it's perfectly possible.
>>> But doing so
>>> would require at least basic code flow analysis, and dmd almost
>>> never does any
>>> kind of code flow analysis.
>>
>> I remember Walter being against flow analysis in the frontend.
>> This is middleend optimization stuff, though. Does dmd really use
>> no data flow analysis anywhere?
>

The backend, of course:

https://github.com/D-Programming-Language/dmd/blob/master/src/backend/gflow.c

> It has very, very limited flow analysis that it uses in constructors to avoid
> multiple calls to super as well as guaranteeing that nothing gets initialized
> multiple times in a const or immutable constructor. It may also does some very
> minimal flow analysis when dealing with value range propagation (
> http://www.drdobbs.com/tools/value-range-propagation/229300211 ). It may also
> do some minimal checks for missing return statements. But it general, no, it
> doesn't do flow analysis, and when it does, it's very minimal. Walter does not
> consider it to be worth the complexity that it requires.

I think it was about doing it in the frontend.

> And while in this particular case, it would certainly be much nicer for
> repeated calls to a pure function to be optimized within an entire function
> instead of just within an expression, I don't think that it actually ends up
> mattering much in practice, simply because it's generally rare to make the
> exact same call multiple times within a function. And if you do, it's trivial
> to save the result in a variable to be reused.


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list