bugGNU Octave - Bugs: bug #65060, classdef: extremely slow writing...

 
 

bug #65060: classdef: extremely slow writing to array properties

Submitter:  A.R. Burgers <arb>
Submitted:  Wed 20 Dec 2023 12:38:50 PM UTC
   
 
Category:  Classdef Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Performance
Status:  None Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 11 Jan 2024 10:11:05 AM UTC, comment #3: 

I can confirm the issue. It is related to these lines of code from libinterp\octave-value\cdef-object.cc:650 :

            octave_value val = prop.get_value (obj, true, "subsasgn");

            std::list<octave_value_list> args (idx);

            args.erase (args.begin ());

            val = val.assign (octave_value::op_asn_eq,
                              type.substr (1), args, rhs);

            if (val.class_name () != "object"
                || ! to_cdef (val).is_handle_object ())
              prop.set_value (obj, val, true, "subsasgn");

            retval = to_ov (obj);


The member function "subsasgn" is implemented as two steps:
1 - Getting the property value that makes a copy of it
2 - Setting to the copy of the property value

It works correctly but is inefficient. In the step 1 a reference to the property value should be taken.

Another solution is implementing subsasgn as a one step function.

Anonymous
Fri 29 Dec 2023 09:10:32 PM UTC, comment #2: 

attaching a callgrind analysis, all time spent in__memset_avx2_unaligned_erms


A.R. Burgers <arb>
Tue 26 Dec 2023 09:41:48 AM UTC, comment #1: 

I made a mistake in my original example, revised and simplified file foo2.m u_foo2.m attached. The assigns to the numerical array are extremely slow, despite the array being pre-allocated.

output of u_foo2.m


Time for assign to array: 0.000345945
Time for assign to property: 5.56618


u_foo2.m:


__vm_enable__(0);
nrep =  30;
dims = [2000, 2, 20300];

data_d = zeros(dims);

f_classdef = foo2;
f_classdef.data_d = zeros(dims);

dat_set = rand(dims(1), dims(2));

tic;
for i = 1 : min(nrep, dims(3))
  data_d(:, :, i) = dat_set;
end
fprintf('Time for assign to array: %g\n', toc);

tic;
for i = 1 : min(nrep, dims(3))
  f_classdef.data_d(:, :, i) = dat_set;
end
fprintf('Time for assign to property: %g\n', toc);


A.R. Burgers <arb>
Wed 20 Dec 2023 12:38:50 PM UTC, original submission:  

Assigning to array classdef properties can be painfully slow.
Consider this class:


classdef foo
  properties
    data_s = [];
    data_d = [];
  end
end


and this script to exercise the class:


nrep = 2000;
dims = [2000, 2, 20300];

fi = foo;
fi.data_s = zeros(dims, 'single');
fi.data_d = zeros(dims, 'double');
data_s = zeros(dims, 'single');
data_d = zeros(dims, 'single');

dat_set = rand(dims(1), dims(2));

tic;
for i = 1 : min(nrep, dims(3))
  data_d(:, :, i) = dat_set;
end
fprintf('Time for assign to double precision array: %g\n', toc);

tic;
for i = 1 : min(nrep, dims(3))
  data_s(:, :, i) = dat_set;
end
fprintf('Time for assign to single precision array: %g\n', toc);

tic;
for i = 1 : min(nrep, dims(3))
  foo.data_d(:, :, i) = dat_set;
end
fprintf('Time for assign to double precision property: %g\n', toc);

tic;
for i = 1 : min(nrep, dims(3))
  foo.data_s(:, :, i) = dat_set;
end
fprintf('Time for assign to single precision property: %g\n', toc);


octave 10 timings:


Time for assign to double precision array: 0.0131512
Time for assign to single precision array: 0.0130341
Time for assign to double precision property: 52.545
Time for assign to single precision property: 29.0633


matlab R2019b timings


Time for assign to double precision array: 0.036111
Time for assign to single precision array: 0.033317
Time for assign to double precision property: 0.012477
Time for assign to single precision property: 0.042348


A.R. Burgers <arb>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #55488:  kcachegrind.png added by arb (366KiB - image/png)
file #55468:  u_foo.m added by arb (772B - text/plain)
file #55469:  foo.m added by arb (70B - text/plain)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Updated the item)
  • -email is unavailable- added by arb (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2024-01-23 mmuetzel CategoryInterpreter Classdef
    2023-12-29 arb Attached File- Added kcachegrind.png, #55488
    2023-12-20 arb Attached File- Added u_foo.m, #55468
        Attached File- Added foo.m, #55469

    Back to the top

    Powered by Savane 3.13-caa5.
    Corresponding source code