Hello
I want to do some project on an arduino board. Writing some code I started thinking about whether using ref objects or not makes sense for embedded targets? What are the pros and cons of each option?
Most people will avoid refs (and dynamic heap usage in general in embedded) because of the associated runtime cost, the difficulty in predicting memory usage on these constrained systems, and the risk of heap fragmentation.
That said, use them sparingly if you have to, and you know the limits of your system. I don't mind using a seq or string here or there if I know there's an upper bound in size. Or even ref objects if you are building a recursive data structure like a graph or tree, but that's sort of rare in embedded.
Prefer plain objects in general and only use refs if you really need them.
. * a single string can kill half your ram due to fragmentation
Now aside from free memory space it's good to remember that allocations are expensive. Very expensive on embedded, and not something you'll want in any sort of loop.
My general guidance would be to limit ref objects to long lived configuration objects and containers. Allocate containers outside any loops and be careful of copies.
Usually the best performance can be found by getting in the habit of using var in proc arguments for arrays or struct. Using var openArray[T] with T being non-ref objects generally gives good performance. Ref objects can be useful if you can't use var due to code constraints.
Usually the best performance can be found by getting in the habit of using var in proc arguments for arrays or struct. Using var openArray[T] with T being non-ref objects generally gives good performance. Ref objects can be useful if you can't use var due to code constraints.
Advertising the use of var proc parameters for performance reasons is very strange. The compiler is advertised to pass value data with size larger than 3 * sizeof(float) as a hidden pointer to procs, so use of a var keyword should make only a minimal performance difference. And note that there is currently no struct data type in the Nim language :-)