At first I thought you were right. But then I realized there's an even
deeper problem here. Imagine implementing a record mutator like this:

(define (set-foo-a! foo new-value)
  (if (is-foo? foo)
     (%unsafe-set-foo-a! foo new-value)
     (error)))

That code is incorrect, by the same logic you used: something could
change the class of foo in between the is-foo? call and the
%unsafe-set-foo-a! call. You need some atomic check-and-set operation
in order to do this safely.

Unfortunately, I don't see a correct way short of some sort of mutex.
A mutex for each record would be a lot of overhead, but what about a
mutex per class, that has to be acquired to change any instance of the
class to or from a different class? Changing a class is a rare enough
operation that we should not optimize for it, I think. Also, if we had
such a mutex, we could use it to eliminate redundant struct-vtable
checks correctly, by acquiring it at the beginning of a function and
releasing it at the end.

I'm somewhat afraid, however, that the real solution is changing how
we deal with parallelism, and that is a much bigger problem.

Noah

On Fri, Apr 13, 2012 at 10:22 AM, Mark H Weaver <[email protected]> wrote:
> Hi Andy,
>
>> commit 59c557056cff1ce6146b4d689eeee922300b6278
>> Author: Andy Wingo <[email protected]>
>> Date:   Tue Apr 10 15:56:23 2012 -0700
>>
>>     peval: elide redundant predicates; eliminate some common subexpressions
>>
>>     * module/language/tree-il/peval.scm (fold-constants): Returns #f instead
>>       of the expression, as all continuations handle #f themselves.
>>       (negate, bailout?, extract-facts, infer, infer-defined?)
>>       (infer-struct-vtable): New helpers.
>
> I haven't looked at the code, but it sounds like you are trying to
> eliminate redundant 'struct-vtable' checks.  Unfortunately, it seems to
> me that this cannot be done safely.  In practice, the checks look like:
>
>  (eq? (struct-vtable s) <foo>)
>
> Both of the values being compared here are fetched from mutable
> locations.  The 'struct-vtable' field is mutable, and <foo> is usually a
> top-level variable that is also mutable.  Furthermore, both of these
> things are mutated in practice when a GOOPS class is redefined, IIUC.
>
> Am I missing something?
>
>    Thanks,
>      Mark
>

Reply via email to