changeset 12949:d1db86336a49

cellfun.cc (cellfun): Small optimisation for function handles to built-ins
author Jordi Gutiérrez Hermoso <jordigh@gmail.com>
date Thu, 11 Aug 2011 02:41:36 -0500
parents 61fba96cb482
children 5a5cb2a4b71d
files src/DLD-FUNCTIONS/cellfun.cc
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/DLD-FUNCTIONS/cellfun.cc
+++ b/src/DLD-FUNCTIONS/cellfun.cc
@@ -222,6 +222,7 @@
     }
 
   octave_value func = args(0);
+  bool symbol_table_lookup = false;
 
   if (! args(1).is_cell ())
     {
@@ -339,6 +340,8 @@
               func = symbol_table::find_function (name);
               if (func.is_undefined ())
                 error ("cellfun: invalid function NAME: %s", name.c_str ());
+
+              symbol_table_lookup = true;
             }
         }
     }
@@ -349,6 +352,19 @@
   if (func.is_function_handle () || func.is_inline_function ()
       || func.is_function ())
     {
+
+      // The following is an optimisation because the symbol table can
+      // give a more specific function class, so this can result in
+      // fewer polymorphic function calls as the function gets called
+      // for each value of the array.
+      if (! symbol_table_lookup )
+        {
+          octave_value f = symbol_table::find_function ( func.function_value ()
+                                                         -> name ());
+          if (f.is_defined ())
+            func = f;
+        }
+
       unwind_protect frame;
       frame.protect_var (buffer_error_messages);