octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

unexpected copying


From: John W. Eaton
Subject: unexpected copying
Date: Tue, 13 Jun 2006 00:57:40 -0400

On 12-Jun-2006, John W. Eaton wrote:

| On 12-Jun-2006, John W. Eaton wrote:
| 
| | I think it is assignment to "ans" that is causing trouble (though
| | "ans" is not even showing up with a value in the symbol table).  Hmm.
| | Looks like a bug, but I don't see where yet.
| | 
| | Note that with your example, adding "ans = []" after reshape prevents
| | the copy.  With this code
| | 
| |   S=T=[100:100:3000]; 
| |   for i=1:length(S), 
| |     k=S(i); A=rand(k); 
| |     tic; A=reshape(A,k*10,k/10); ans = []; A(1,1)=2; T(i) = toc; 
| |   end
| | 
| |   [P,R] = polyfit(S,T,2);
| |   plot(S,T,S,polyval(P,S))
| | 
| | I see constant time.
| 
| Hmm, well I don't think it is ans as I also see constant time if I
| replace "ans" with "x" in the above code.  OK, now I'm confused.

OK, please try the following patch.  It seems obvious now, but this
one had me stumped for quite a while.  This bug has probably been
around for a long time.  I see a few more useless assignments that can
be deleted, and maybe this function doesn't need to return anything
at all.  I'll check that out later.  For now, this patch should fix
the problem you were seeing.

Thanks,

jwe


src/ChangeLog:

2006-06-13  John W. Eaton  <address@hidden>

        * pt-stmt.cc (tree_statement_list::eval):
        Clear retval before each statement is evaluated.


Index: src/pt-stmt.cc
===================================================================
RCS file: /cvs/octave/src/pt-stmt.cc,v
retrieving revision 1.27
diff -u -r1.27 pt-stmt.cc
--- src/pt-stmt.cc      8 May 2006 20:23:06 -0000       1.27
+++ src/pt-stmt.cc      13 Jun 2006 04:49:42 -0000
@@ -165,6 +165,17 @@
        {
          OCTAVE_QUIT;
 
+         // Clear preivous values before next statement is evaluated
+         // so that we aren't holding an extra reference to a value
+         // that may be used next.  For example, in code like this:
+         //
+         //   X = rand (N);  ## refcount should be 1 after this statement.
+         //   X(idx) = val;  ## no extra copy should be needed, but
+         //                  ## we will be faked out if retval is not
+         //                  ## cleared between statements here.
+
+         retval = octave_value_list ();
+
          retval = elt->eval (silent, nargout, function_body);
 
          if (error_state)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]