When using an associative container, I’ve observed, that at least sometimes a series of:
container[key].field1 = something1;
container[key].field2 = something2;
container[key].field3 = something3;
produces a smaller binary (executable) than, say
auto& c(container[key]);
c.field1 = something1;
c.field2 = something2;
c.field3 = something3;
I am confused. AFAIK, a value would need to be looked up for every container[key] = ... statement. Does the compiler optimize these lookups away? What is the best thing to do?
“What is the best thing to do?”
The best thing to do is to not look at such details, since tomorrow (or whenever the next version of the compiler, library or coffee machine is released) everything will be different anyway.
In the end, the “best thing to do” is to write code that
So, compare your solutions and keep the one that “feels better” or “looks better”. I would probably keep the one that does just one lookup, as it emphasises the fact that everything happens on the same object.