develooper Front page | perl.perl5.porters | Postings from November 2016

A possible new approach to COW - COW_META.

Thread Next
November 6, 2016 15:13
A possible new approach to COW - COW_META.
Message ID:
Today I pushed a branch, yves/cow_meta.

This branch is my take on reworking COW so it does not store the
refcount in the string.

The idea is to introduce a new struct cow_meta:

struct cow_meta {
    U32     cm_flags;
    U32     cm_refcnt;
    STRLEN  cm_len;

typedef struct cow_meta COW_META;

This new type is currently allocated out the HE arenas.

The xpv_len_u union in the _XPV_HEAD struct fragment gets modified to
support a new member:

    union {                                                             \
        STRLEN       xpvlenu_len;        /* allocated size */           \
        char *       xpvlenu_pv;         /* regexp string */            \
        COW_META * xpvlenu_cow_meta; /* ref to refcount struct */   \
    } xpv_len_u

We then change COW so that it works as follows.

To "en-COW" an existing SVPV we allocate a new COW_META structure and
then, after copying the length from the SVPV to the COW_META
structure, we set the xmp_len_u slot of the SVPV to point at the

To copy a COW structure we copy the PV, and copy the COW_META
structure, and increment the refcount.

The immediate advantage of this new structure is that we can COW *ANY*
buffer, without worrying about whether it is overallocated or not, and
regardless of whether it is RO or RW. The downside is far more
bookkeeping when we initially COW a string, as we have to allocate and
initialize the COW_META structure. However I consider the fact that we
lose most of the weird edge cases where a buffer could not be COW'ed
combined with the large range of the refcount to be good justification
for this change.

The code as pushed passes all tests.

Next steps...
I believe this structural change brings further benefits, as we can
switch the refcount data in PL_strtab to hold COW_META references, and
unify the logic for managing sharing strings from keys, and strings
from arbitrary buffers. I think this will greatly simplify our string
sharing logic once it is done. I am working on this now, but please
don't let that stop anyone from improving the branch as pushed.
For instance it occurs to me that the COW_META structure could be
inlined into the shared_he structure so that entries in PL_strtab dont
need independent allocation.

For now...
Take a look at the new branch and see what you think. As a patch
sequence it is a little raw, but I wanted to push it out to a wider
audience so people could take a look.

To FC for getting it working in the first place, changing it once it
works is a lot easier than doing it in the first place. To DaveM for
reviewing my original plan and suggesting the use of the HE arenas.
Sawyer and ToddR for general support and feedback.

ps: I haven't pushed it to smoke as I don't consider the branch quite
release ready yet. Various changes like renaming things, or cleaning
up the code flow a bit should probably be done before it gets merged.

perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About