changeset 29708:fbcc3a680826

Improve performance (> 100X) of jsondecode (bug #60625). * jsondecode.cc (decode_array_of_arrays): Create new temporary variable sub_array_value within outermost for loop, rather than calling array_value() every time within innermost for loop.
author Rik <rik@octave.org>
date Wed, 26 May 2021 07:51:34 -0700
parents 81be7e4ddb0f
children 3f61913fdee4
files libinterp/corefcn/jsondecode.cc
diffstat 1 files changed, 7 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/jsondecode.cc	Wed May 26 12:23:24 2021 +0200
+++ b/libinterp/corefcn/jsondecode.cc	Wed May 26 07:51:34 2021 -0700
@@ -311,10 +311,13 @@
   NDArray array (array_dims);
 
   // Populate the array with specific order to generate MATLAB-identical output
-  octave_idx_type array_index = 0;
-  for (octave_idx_type i = 0; i < array.numel () / cell_numel; ++i)
-    for (octave_idx_type k = 0; k < cell_numel; ++k)
-      array(array_index++) = cell(k).array_value ()(i);
+  octave_idx_type sub_array_numel = array.numel () / cell_numel;
+  for (octave_idx_type k = 0; k < cell_numel; ++k)
+    {
+      NDArray sub_array_value = cell(k).array_value ();
+      for (octave_idx_type i = 0; i < sub_array_numel; ++i)
+        array(k + i*cell_numel) = sub_array_value(i);
+    }
 
   if (is_bool)
     return boolNDArray (array);