Coding techniques to conserve memory
-
I'm working with a Duet2 WiFi, so I have a shortage of memory, so I'm trying to develop some programming techniques to minimize memory use. So I have an idea and I'd like to know if it can be made to work.
For a simple example, let's say I have a variable A which is only used to calculate another variable B. Assume the code for the value of A and B is too complex to simply replace "var.A" in a single expression for B with the expression used to calculate A.
My idea:
var B = 0 if true var A = 0 ; Complex code to get the final value for A ; Complex code involving A, to get the final value of B ; A is never needed again var C = 0 var D = 0 ; Code which uses B
Would I be correct that the end of the conditional block would see the deallocation of A? At that point I'm hoping that the memory in use would be only enough for the 3 variables B, C and D (assuming no other variables are defined in the macro). That is, that this would use 75% of the memory that an equivalent macro without the conditional would. If this works, I suspect I could do something similar with a while loop, and maybe other constructs (could I just indent, without any flow control statements, like a {compound statement} in C++?)
I should mention, that for this to be useful, it would either have to be done more than once in a macro, or it would have to be done earlier than some other variables get defined.
If this isn't a solution, I know I can offload the code of some macros into multiple nested calls, but that suffers from 3 limitations I can think of: 1. At this time, I can't return a value; 2. There's a limit to the nesting depth, and I'm already often up against it; 3. I assume there's some performance hit for a nested call. Nonetheless, I'm already doing this sometimes, but sometimes it's not ideal.
If my idea doesn't work, is there something else I could do?
Thanks for your help.
-
@DonStauffer See https://docs.duet3d.com/en/User_manual/Reference/Gcode_meta_commands#local-variable-declaration
The scope of a local name is the remainder of the block in which it is declared.
I think that's what you mean? You could test by echo-ing var.A outside the loop.
Ian
-
@droftarts I assumed the inability to use the variable outside its scope, so I wouldn't try to use it. I only intended to use this if I didn't need to. Hence the comment "A is never needed again".
My question is whether it would, in fact, make the memory of A available, say, to be used for C for example, thus reducing the maximum memory requirements of the macro.
Assuming my 4 variables are all 4 byte integers, and disregarding any overhead, the variables in my example should at most use 12 bytes, where the same code without the conditional would reach 16 bytes usage, if I'm correct in my assumptions. My main assumption is that when A goes out of scope, its memory becomes available even though the macro is still running.
I do recognize that memory allocation schemes probably make it a little more complicated, like if memory gets allocated a page at a time or something. But that doesn't necessarily mean that if I use this technique a fair amount, I wouldn't be able to get a program to work that would run out of memory otherwise.
So my question is whether that assumption about memory availability is correct. at least generally.
-
@DonStauffer I see, but I don't know! One for @dc42.
Ian
-
@DonStauffer yes the memory allocated for var.A will be made available for use by other variables when it goes out of scope.