in reply to How to swap scalar values without copies

I dug for an hour or so into the concept of using a set of tied scalars whos "STORE" methods only store references to $_[1], so that copies are never made. My goal was to encapsulate all use of references within the module that handles the tied scalars, so that the scalars themselves just look like normal plain-vanilla scalars, and yet they never actually create a copy of the original data (they should only internally store references to the original data).

Unfortunately, my attempts, while producing functional code, seem to still make copies. diotalevi suggests that this is because the ( $x, $y ) = ( $y, $x ) construct has its own optimizations that create the copies internally no matter what you do. I also suspect that my FETCH method's ${$self->{VRef}} construct is creating a copy.

So my attempt at a pure Perl solution to swapping the values of two lexically scoped scalars without creating (explicitly or internally) copies, and without explicitly using references in the main code, is a failure.

Nevertheless, I thought that the attempt was worth demonstrating to see if anyone else could do anything with it. I really thought I was on to something, but it seems that there isn't a pure-Perl solution to the OP's quandry. Before I paste it here, I just wanted to quickly thank blokhead for helping me test it. Here it is:

package Tie::Scalar::NoCopies; # This package compiles, runs, and implements a tied # scalar. But it doesn't do as it advertises... ie., # it doesn't suppress copies. use strict; use warnings; sub TIESCALAR { my ( $class ) = @_; my $self = {}; $self->{VRef} = undef; bless $self, $class; } sub STORE { my $self = shift; $self->{VRef} = \$_[0]; return ${$self->{VRef}}; } sub FETCH { my $self = shift; return ${$self->{VRef}}; } sub DESTROY { my $self = shift; } 1; package main; use strict; use warnings; my ($var1, $var2); tie $var1, "Tie::Scalar::NoCopies"; tie $var2, "Tie::Scalar::NoCopies"; $var1 = 'a' x 10; $var2 = 'b' x 10; print "$var1\t$var2\n"; ( $var1, $var2 ) = ( $var2, $var1 ); print "$var1\t$var2\n";

Enjoy!!!


Dave

Replies are listed 'Best First'.
Re: Re: How to swap scalar values without copies
by Anonymous Monk on Feb 20, 2004 at 23:34 UTC
    >> I also suspect that my FETCH method's ${$self->{VRef}} construct is creating a copy.

    I definitely think that ${....} creates copies. I set up a shell with virtual memory restricted to a value chosen by me (with ulimit -v ..... This value is such that a small perl program can allocate a large buffer, but not two copies of the buffer. Now, the following code:

    $this->{data} = \$large_buffer; print length ${$this->{data}} . "\n";
    cannot be executed (it dies of "Out of memory"). So it seems that the reference way (and the function returning the lvalue) are not viable, because the act of dereferencing seems to allocate a copy of the object.

    Another indication that the dereferentiation is nasty, is that running a program where I repeatedly dereferentiate a reference to a large buffer is way slower than a program which accesses the buffer directly (orders of magnitude slower than what the dereferentiation overhead per se could justify).

    Can you tell me if this is true? or who is eating memory then?