changeset 9886:cddd5c3d5f04

fix & extend special-case optimizations for indexed assignment
author Jaroslav Hajek <highegg@gmail.com>
date Sun, 29 Nov 2009 07:51:15 +0100
parents dd3fc8ba4796
children e3bd1569a68c
files liboctave/Array.cc liboctave/ChangeLog liboctave/dim-vector.h liboctave/idx-vector.h
diffstat 4 files changed, 33 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Array.cc
+++ b/liboctave/Array.cc
@@ -1180,12 +1180,12 @@
   if (rhl == 1 || i.length (n) == rhl)
     {
       octave_idx_type nx = i.extent (n);
+      bool colon = i.is_colon_equiv (nx);
       // Try to resize first if necessary. 
       if (nx != n)
         {
           // Optimize case A = []; A(1:n) = X with A empty. 
-          if (rows () == 0 && columns () == 0 && ndims () == 2
-              && i.is_colon_equiv (nx))
+          if (dimensions.zero_by_zero () && colon)
             {
               if (rhl == 1)
                 *this = Array<T> (dim_vector (1, nx), rhs(0));
@@ -1198,7 +1198,7 @@
           n = numel ();
         }
 
-      if (i.is_colon ())
+      if (colon)
         {
           // A(:) = X makes a full fill or a shallow copy.
           if (rhl == 1)
@@ -1249,12 +1249,13 @@
 
   if (match)
     {
+      bool all_colons = (i.is_colon_equiv (rdv(0)) 
+                         && j.is_colon_equiv (rdv(1)));
       // Resize if requested.
       if (rdv != dv)
         {
           // Optimize case A = []; A(1:m, 1:n) = X
-          if (dv.all_zero () && i.is_colon_equiv (rdv(0))
-              && j.is_colon_equiv (rdv(1)))
+          if (dv.zero_by_zero () && all_colons)
             {
               if (isfill)
                 *this = Array<T> (rdv, rhs(0));
@@ -1267,7 +1268,7 @@
           dv = dimensions;
         }
 
-      if (i.is_colon () && j.is_colon ())
+      if (all_colons)
         {
           // A(:,:) = X makes a full fill or a shallow copy
           if (isfill)
@@ -1353,7 +1354,7 @@
       int j = 0, rhdvl = rhdv.length ();
       for (int i = 0; i < ial; i++)
         {
-          all_colons = all_colons && ia(i).is_colon ();
+          all_colons = all_colons && ia(i).is_colon_equiv (rdv(i));
           octave_idx_type l = ia(i).length (rdv(i));
           if (l == 1) continue;
           match = match && j < rhdvl && l == rhdv(j++);
@@ -1367,6 +1368,17 @@
           // Resize first if necessary.
           if (rdv != dv)
             {
+              // Optimize case A = []; A(1:m, 1:n) = X
+              if (dv.zero_by_zero () && all_colons)
+                {
+                  rdv.chop_trailing_singletons ();
+                  if (isfill)
+                    *this = Array<T> (rdv, rhs(0));
+                  else
+                    *this = Array<T> (rhs, rdv);
+                  return;
+                }
+
               resize_fill (rdv, rfv);
               dv = dimensions;
               chop_trailing_singletons ();
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,3 +1,11 @@
+2009-11-28  Jaroslav Hajek  <highegg@gmail.com>
+
+	* dim-vector.h (dim_vector::zero_by_zero): New method.
+	* idx-vector.h (idx_vector::idx_mask_rep::is_colon_equiv): Fix.
+	* Array.cc (Array<T>::assign): Minor tweaks. Optimize 
+	A = []; A(1:m,1:n,1:k) = X for all cases. Use a shallow copy
+	for all colon-equivalent indices.
+
 2009-11-27  Jaroslav Hajek  <highegg@gmail.com>
 
 	* idx-vector.h (idx_vector::index_class): New member: class_mask.
--- a/liboctave/dim-vector.h
+++ b/liboctave/dim-vector.h
@@ -278,6 +278,11 @@
     return retval;
   }
 
+  bool zero_by_zero (void) const
+  {
+    return length () == 2 && elem (0) == 0 && elem (1) == 0;
+  }
+
   bool any_zero (void) const
   {
     bool retval = false;
--- a/liboctave/idx-vector.h
+++ b/liboctave/idx-vector.h
@@ -367,7 +367,7 @@
       { return orig_dims; }
 
     bool is_colon_equiv (octave_idx_type n) const
-      { return count == n && ext == n; }
+      { return len == n && ext == n; }
 
     const bool *get_data (void) const { return data; }