in reply to Re^2: Tracking down an Lvalue bug?
in thread Tracking down an Lvalue bug?

Back to the drawing board!

What are you trying to do?

Replies are listed 'Best First'.
Re^4: Tracking down an Lvalue bug?
by BrowserUk (Patriarch) on Mar 20, 2012 at 02:27 UTC

    Demonstrate that taking an lvalue substr ref causes the referenced substring to be copied, And that this is a bug.

        Related to ref to read-only alias ... why??

        I don't think so.

        This demonstrates the problem (bug):

        perl -E"$x=chr(0); $x x= 100e6; <>; $r = \substr $x, 50e6; <>"

        Watch the process memory with ProcessManager:

        1. When it pauses the first time memory usage is ~98MB. Hit enter
        2. Now the memory usage shoots up to ~145MB.

          All that's changes is that it has taken a substr ref to the second half of the string.

        I suspect it is a result of this "fix".

        But I believe that fix is wrong, and that the original bug is spurious. (At least in part.)

        If you do this:

        my $ref; { my $string = "123"; $ref = \$string; } ## Here $string persists.

        No one is surprised that (the memory allocated to) $string persists beyond the block.

        Why should this be any different:

        my $ref; { my $string = "123"; $ref = \substr $string, 1, 1; } ## This was the (IMO false) "bug" scenario.

        In this case -- when the string goes out of scope -- I could make arguments for 3 possible outcomes:

        1. The memory allocated to $string persists:

          $ref continues to refer (directly) to the memory allocate to $string.

          Those parts of $string outside of $ref are inaccessible and irrecoverable until $ref is GC's.

        2. The memory allocated to $string is GC'd; $ref becomes undef.

          Probably difficult to orchestrate.

        3. The memory allocated to $string persists until $ref is used the next time:

          At which point the extraneous (pre and/or post fix) bits of $string are GC'd leaving $ref pointing at just that which it references.

          This would require retaining a pointer back to 'parent' string with the lvalue ref, and when the lvalue magic fires it checks the refcount of its parent.

          If that refcount is 1 -- meaning it holds the only reference to it, it frees off the extraneous parts of the parent.

        Of the three, I'd prefer C, but would find A completely in keeping with Perl's philosophy.

        Duplicating the referenced memory of every lvalue ref and the having to copy it back each time it is modified -- just to cover off a really obscure scenario that is at worst, only mildly anomalous -- is crazy.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?