-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify getInputPortSignal and getOutputPortSignal regarding dynamic memory allocations? #27
Comments
get***PortSignal
w.r.t. to memory allocations?
For solving 2), a good solution may be to wait for when we will adopt C++17, and switch to use |
Some time ago, the Today this is the default behavior for both input and output signals, and in Simulink we had to configure input signals as follows: blockfactory/sources/Simulink/src/BlockFactory.cpp Lines 175 to 176 in 39b3d44
otherwise they are treated as Said this, here are my comments:
In the case of the Simulink engine, this is correct. The Simulink Coder implementation instead does not have this problem since the
Why do you think that there is a copy in this case? I recently forced in #33 to use the move semantic to return input signals, and this was necessary only due to a defect in ISO C++11:
|
I probably did not expressed myself clearly, but I am not sure about what you are referring to. I do not think I ever said there is a copy in this case. I just think that if Signal was a relatively standard class with no pimpl, a simple return will avoid the need of calling
I strongly suspect that eventually removing the pimpl from |
Sorry, I misunderstood your point if it was on the |
For several reasons, typically involving performance (due to use of system calls and violation of cache locality) but also determinism on some hard real-time OS, a good practice is to avoid, sometimes it is considered good practice to avoid any kind of dynamic memory allocation in code that will be called in "tight loop", for example the code that is called at every step of a control, simulation or optimization algorithm.
Related to that, I notice that the current BlockFactory API induces the users to perform two dynamic memory allocations whenever, from an
output
method, and input or output signal is accessed. The situation could get worse if eventually tackle #8, as some of the same signal would need to be accessed also from theupdateDiscreteState
and the BlockFactory equivalent ofmdlDerivatives
. Note that this two memory allocation are avoided in the "CoderBlockInformation" implementation only because in that case the IO signals are stored internally already asstd::shared_ptr<Signal>
, but for example are necessary in theSimulinkBlockInformation
implementation (and similarly they will probably be present in<something>BlockInformation
implementations for which the addresses of IO signals could change at each simulation step).These two dynamic memory allocations are caused by:
blockfactory::core::Signal
(added in diegoferigo/WB-Toolbox@75c1d36#diff-e32b9f2c904cb22f7a9187e2a76861a3). I guess this change can be relatively safely reverted, if we think it is the case.std::shared_ptr<Signal>
to return a signal ingetInputPortSignal
andgetOutputPortSignal
, in place of standard copy on the stack, that in most cases will be optimized away due to Return Value Optimization (RVO). This it is a more complicated change, as it was explicitly introduced in Signal const correctness wb-toolbox#126 to ensure that input data is only read and not wrote without introducing a new typeConstSignal
orSignal<isConst>
.The change 2) is definitely more complicated (if we want to continue to enfore the const correctnes of input signals), while 1) should be straightforward.
The text was updated successfully, but these errors were encountered: