Infer purity of functions too?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jul 18 14:01:24 PDT 2011


On Mon, 18 Jul 2011 16:52:41 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On 2011-07-18 13:32, Steven Schveighoffer wrote:
>> On Mon, 18 Jul 2011 16:01:08 -0400, bearophile  
>> <bearophileHUGS at lycos.com>
>>
>> wrote:
>> > Maybe someone has already answered this question, but I don't remember
>> > it.
>> >
>> > DMD 2.054 is able to infer if a template function is pure. Isn't it a
>> > good idea to use the same machinery to infer the unmarked functions  
>> too?
>> > A template may call a second function that's not annotated with 'pure'
>> > despite being pure. If the compiler is able to infer the purity of the
>> > second function, then both the template and the second function can be
>> > pure. This may offer optimization opportunities. Is doing this too  
>> much
>> > slow?
>>
>> It might not be possible. For example, if the target function has no
>> public implementation. This is not the case for templates -- the
>> implementation must be available.
>>
>> In theory, it's possible for the compiler to mark a function whose  
>> source
>> is available as pure, and indeed, most could be. It would be a nice
>> solution to the issue we have now where so much is not pure. At some
>> point though, optional may not be what you want. In fact, you may want
>> the compiler to complain that a function you marked as pure isn't  
>> actually
>> pure. Relying on the compiler to determine purity has drawbacks...
>
> We pretty much _have_ to rely on purity inference for templates, because  
> the
> only other way is to have multiple versions of the template (one pure  
> and one
> not), and not only is that highly undesirable, it becomes completely  
> untenable
> once you add nothrow and @safe into the mix. Normal functions have none  
> of
> these problems. And if pure were inferred for a function and then it  
> became
> impure, that could break a _lot_ of code. And even if it didn't, and all  
> of
> the functions in the chain just silently became impure, it could have  
> negative
> effects on performance, and you wouldn't have a clue why.
>
> Ideally, we wouldn't need purity inference at all. With templates, we  
> don't
> have much choice, but it's not needed for normal functions.

Well, with the relaxed purity rules, we are looking at a very large phobos  
codebase which could be vastly more pure than it is.  Not only does this  
mean pure attributes littered everywhere, but someone has to go through  
and mark 'em all.  And because pure functions cannot call impure  
functions, you can't just mark ones you *think* should be pure, you have  
to start at the bottom and move up from there.  It's not trivial work.

It's almost like shared, except we are forced to annotate for "unshared".

I'm not saying pure should be inferred everywhere, I'm just saying, it  
would be an attractive solution for the current situation.

-Steve


More information about the Digitalmars-d-learn mailing list