Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

What is PERL_COPY_ON_WRITE Compile Option About

by DeadPoet (Scribe)
on Mar 04, 2005 at 17:17 UTC ( #436654=perlquestion: print w/replies, xml ) Need Help??
DeadPoet has asked for the wisdom of the Perl Monks concerning the following question:

To All:

Please enlighten me on a curious perl compile option. The option is know as PERL_COPY_ON_WRITE. What is it and what does it do?

  • Comment on What is PERL_COPY_ON_WRITE Compile Option About

Replies are listed 'Best First'.
Re: What is PERL_COPY_ON_WRITE Compile Option About
by BrowserUk (Pope) on Mar 04, 2005 at 18:18 UTC

    PERL_COPY_ON_WRITE has nothing to do with threads or process as far as I can see.

    It is used by code within the pp_hot.c, regcomp.h and other places to avoid copying data until (and unless) it is written to.

    An example with informative comment comes from pp_hot.c:

    #ifdef PERL_COPY_ON_WRITE /* The match may make the string COW. If so, brilliant, because that's just saved us one malloc, copy and free - the regexp has donated the old buffer, and we malloc an entirely new one, rather than the regexp malloc()ing a buffer and copying our original, only for us to throw it away here during the substitution. */ if (SvIsCOW(TARG)) { sv_force_normal_flags(TARG, SV_COW_DROP_PV); } else #endif

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
      To All:

      Here is what I just found at
      "Currently in perl when you write $a = $b, the contents of $b are immediately copied into $a. In theory perl could just pretend that it did the copy, and only actually do it if one of $a or $b is modified. If $b is a large string, and $a is never modified this could potentially be a big efficiency gain. This talk consists of simple pictures - how I didn't do it, and how I did."


        It appears that currently, COW is only used for certain copy operations--some of those in the regex engine, shared hash keys (or values?), and a few other places--but not currently simple scalars.

        It looks like the proposal is to extend it's reach.

        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.
        That was an excelent talk. Highly informative and imaginatively illustrated with several people holding up cards in a carefully choreographed sequence.
Re: What is PERL_COPY_ON_WRITE Compile Option About
by hardburn (Abbot) on Mar 04, 2005 at 17:24 UTC

    CoW refers to a method of optimizing memory usage for multithreaded programs. Consider a program that fork()s after intitlizing a variable. Under normal usage, those variables are copied into the memory space for each program. Under CoW, they point to the orginal variable in the parent. As long as each thread only reads the variables, this is safe. When a modification occurs, the variable is copied into the specific thread's memory space, thus keeping the change from other threads.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      I'm confused by your answer. You talk of threads created by fork, but fork creates new processes, not new threads (except in Windows). Does CoW affect multitasking or multithreading or both?

        "Processes are threads with an attitude. Or the other way around." (I belive this is attributed to Larry, and may not be an exact quote).

        Forks and threads are conceputally similar, and for the purposes of CoW, are basically the same thing.

        "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://436654]
Approved by Limbic~Region
[Tux]: Corion++ hippo++
[ovedpo15]: it isn't homework. its a module I use at work. The process to add additional module isn't worth it. p.s never heard a university course which teach perl
[marto]: there are many, to this day.
[ovedpo15]: @Corion Yes I use the regex you showed me. its very good regex. although now i need to know that substring unit that comma
marto mad corrections to one around 18 months ago
[ovedpo15]: my code checks the value after that comma, if it isn't valid it will remove it. so I would like to remove the substring after that comma meaning getting the string before comma.
[hippo]: Are you sure that Text::CSV_XS isn't already installed at your work? It's such a useful module that it might well be there.
[marto]: pointing out the advantages of cpan modules is well worth in, as both developer and $client/$company benefit greatly
[Tux]: $src =~ m{^(.*),(.*)$/ and $2 !~ $valid and $src = $1;
[Tux]: s,/,},

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-05-27 10:47 GMT
Find Nodes?
    Voting Booth?