OK, so, first of all let me tell you that I have spent countless hours analyzing what the Nim compiler is doing and how Nim code gets converted to C.
Let's take this naive example:
A rough idea in code:
let val = tbl[key] # COPY
if val.something == somethingElse:
# do something
if val.somethinElse == whatever:
# do something else
Another idea, would be to totally avoid the assignment and make 2 lookups instead of one (yay!).
Then - right now, as I'm writing this - I'm thinking whether using a var instead of a let would make a difference. But then, I remember more than once being totally baffled by the output, after minor changes (that, at least according to what I would expect appeared either auto-magical, or not very predictable).
I've found my own ways of doing things like that, but mostly they look like weird hacks (ok, plus I have developed some weird phobia towards assigning things; you never know you may end up with 3 trillion copies lol)
So... how do you usually handle this type of thing?
Just use more ref in your code. For tables mgetOrPut also works wonders but others already told you about it.
There is also this idiom:
let x = addr table[key]
use x[]
useAgain x[]
It's not really all that hard...
Thanks for this addr table[key]. I guess I needed some confirmation to make 100% sure I won't mess with the GC.
Awesome :)