Fully transitive const is not necessary

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 4 10:34:51 PDT 2008


"Fawzi Mohamed" wrote
> So naturally also "pure" functions would like to take advantage of it, or 
> the usefulness of the whole is very diminished.
> But then the cache has to be done *very* carefully to avoid curruptions 
> due to multiple threads accessing the object...

In fact, if pure does memoization, it must too be aware of threading 
implications, unless the memoization is per-thread (which I have no idea why 
it would be).

> It is possible but really not easy (and needs locking or atomic operations 
> that probably will not be good for performance), so very likely it will be 
> done badly and will lead to very subtle bugs.

Anytime you run 2 non-pure functions that access mutable data, be it global 
or as a logical const portion of the object, you will need synchronization. 
Adding logical const does not add to or diminish that requirement.  For pure 
to be statically verifiable by the compiler, it must not be allowed to 
access any mutable data that is at the same time accessible through another 
thread, so it should not be able to access the data at the same time, and no 
synchronization is necessary.

> I really think that the controlled and already studied ways to relax the 
> const are "suspended evaluation", and "undefined settable value blocking 
> on read".

I'm not familiar with these concepts.  Frankly, I'm not sure they are 
relevant to this discussion (although they might be pertinent to making 
multiprogramming easier).  I'm trying to take an already existing 
possibility, and make it easier to implement and perform better.

> I also think that indeed what walter wants is as guslay said
>
>> A = B = C
>
> because you never know (and for performance reasons do not want to know) 
> if another thread is executing a g_not_pure on the same data on which a 
> pure function is working.

But if A is invariant, it cannot be changed, so there are no thread 
concerns.   Think of the mutable portion of a logical-const class as being 
global data (or as I like to think, not part of the class state).  A pure 
function cannot access global data, and so likewise it cannot access the 
mutable portions of the class.  There is no need for synchronization because 
the pure function will not change it's output if the mutable portion 
changes.

-Steve 





More information about the Digitalmars-d mailing list