[RFC] clone semantics

Andrew Whitworth wknight8111 at gmail.com
Thu Mar 11 17:37:05 UTC 2010


On Thu, Mar 11, 2010 at 11:28 AM, Allison Randal <allison at parrot.org> wrote:
> One important thing to keep in mind is that there is no global uniform
> implementation of 'clone': the clone op just calls the clone vtable
> function, and each PMC is free to implement it however it chooses. (This is
> a good thing, different languages have different semantics for cloning.)

This almost makes the case that we should provide both deep_clone and
shallow_clone vtables, since the two operations really are wildly
different and different people need to make use of them differently.
I'm not necessarily suggesting this, but they really are two different
operations and the one name for them is quite confusing in this
regard.

> As chromatic pointed out in the ticket, there's a substantial advantage to
> (eventually) solving the problem of unique traversals once for all potential
> uses, including deep cloning, freezing, marking RO or shared variables, some
> kinds of iterators, and potentially GC marking.
>
> A simple registry is a good idea, but it should be more general than a
> "clone registry". A better approach would be a "seen registry" marking nodes
> that have been visited in a particular traversal (whatever is being done by
> that traversal). Storing the registry in interp is unsafe, because you may
> have multiple traversals happening simultaneously. (I'm not even talking
> about concurrency, where global state is the plague. Simple sequential code
> may traverse part-way through a data structure, then call some other code
> that traverses through some other data structure.) Storing it the PMC struct
> of the item being traversed is better, but still unsafe because you may have
> multiple processes iterating over the same PMC at the same time. What you
> really want is data storage for the registry that's unique to each
> traversal.

Peter was using a new registry type in his fixes for freeze/thaw
recursive traversals. He is trying to use that same type to fix the
recursive cloning issue. So this requirement is already being
accounted for.

A good registry PMC (and I am not sure about the specifics of the one
Peter is using) would be able to return a boolean "Have I seen this
one already?", and also be able to return some sort of tag "I have
seen this one, and here is the associated data". In a freeze you
should be able to get access to the already-frozen STRING buffer, in
thaw you want to get access to the already-thawed PMC, and in clone
you want to get a pointer to the cloned PMC/STRING to faithfully
recreate cyclic structures. Assuming his PMC type does this, I think
we have our general solution.

Where we store the traversal, and how we keep track of it is a
different issue. We could keep track of it by passing references, but
that would requir signature changes to vtables that used it (clone,
freeze, thaw, maybe mark, etc).I'm in favor of this, but requires a
deprecation cycle.

--Andrew Whitworth


More information about the parrot-dev mailing list