3 ms·
I wonder what the impact is on performance of routinely wrapping your types in Option<T>? I am assuming it is minimal, because the struct would remain on the s
by bbcbasic 12y ago
I wonder what the impact is on performance of routinely wrapping your types in Option<T>?
I am assuming it is minimal, because the struct would remain on the stack, and the object on the heap. There is just an unwrapping and wrapping cost but no more than Nullable? However this may be a naive view.
I like the syntax though. Where I work we use Code Contracts. This reduces bugs due to nulls but sometimes 25% of the code is Contract.XXX(...) which is annoying to read. And more typing too.
- louthy 12y agoAs you say the overhead should be similar to Nullable. And the struct should be allocated on the stack, but the CLR doesn't guarantee that, so it can be on the heap too. There is however an 'if' in the constructor of Option<T>, so there's a little extra potential branching overhead. For most scenarios I suspect this is all moot, but if you're looking for the absolute maximum performance then you would probably want to do some profiling before committing too heavily.
- MichaelGG 12y agoThe CLR has suboptimal codegen for structs, so there is likely an impact.