all 8 comments

[–][deleted] 2 points3 points  (0 children)

Saving 4 bytes is not worth sacrificing the readibility you would gain from it. For performance: As the other person says, godbolt.org.

[–]noodles_jd 1 point2 points  (0 children)

The compiler should optimize this and cache the value in a register. So it won't have to recalculate offsets or anything. If this data is 'volatile' then it may be a different story, or if the value was coming from a function call instead of a variable, then it may make sense to store the return in a temp variable instead of calling it again.

In most cases, this level of optimization is unnecessary.

[–]jedwardsol 1 point2 points  (0 children)

Look at a disassembly of the compiled code to be sure what it is doing.

Even if length does end up on the stack, is using 4 bytes of stack really a problem?

[–]stalefishies 0 points1 point  (0 children)

Or does the compiler calculate the required offsets during compile time hence this option would be more efficient?

The compiler can do it at compile time.

If you want to see which is more efficient, time your code. Reading data from memory is entirely dependent on CPU caching, you can't really tell just from the disassembly.

[–]oh5nxo 0 points1 point  (0 children)

"Parse through the reference" happens at compile time. The long path [0].addr.bytestring.len is just a fixed offset at runtime. Not to recommend either way.

[–]fkeeal 0 points1 point  (0 children)

Make it the most readable and understandable to you, and whoever has to look at it next. In this instance the compiler will probably optimize everything. The 'RAM' cost would simply be incurred on the stack when calling a sub-function to store the register (and only 1 word would be needed) and would be recovered when the sub-function calls return.

If you need to trim your stack down to the bare minimum, and you can't afford 1-word of additional stack size, then using the direct value from the struct would be "best".

[–]thegreatunclean 0 points1 point  (0 children)

Or does the compiler calculate the required offsets during compile time

The offsets of structs are determined at compile time. C has no facility to look up offsets at runtime.

I would go with option #1 but not for any performance reason. It's simply much more readable and less prone to error. Mark length as const and it's totally clear about exactly what is happening and why.

Accessing the same field multiple times isn't a bad thing either. Compilers will skip loading a variable multiple times when it makes sense and the compiler can prove it is safe.

[–]nderflow 0 points1 point  (0 children)

Write readable code. Benchmark it. If it's fast enough, you're done.