News for hackers. Stuff that matters.
Re:Is it time to stop using sentinel values for null / “NA” ... by morelisp on Tuesday June 06, 2023 @07:05AM (#36210889)
> converting to a sentinel when possible is the best we can do.

Did you, uh, do the thing I can't ask if you did on HN? Because it's entirely about how to get better performance than sentinel values by specially structuring your discriminant.

Re:Is it time to stop using sentinel values for null / “NA” ... by weinzierl on Tuesday June 06, 2023 @11:56AM (#36214770)
EDIT: Ah, I think I understand where this went wrong. I was answering from my comment thread and did not see your other comments, especially the one where you explained how Julia handles this case. I did not know about that. I'm sorry, I didn't mean to berate you I just was under the wrong impression you said what you said because you were not aware of Rusts discriminant elision. My apologies.

ORIGINAL:

Not sure what you mean, so I'll try again.

An Option<T> in Rust should occupy the size of T plus the size of a discriminant. On a 64-bit machine for example an Option<i32> will take up 8 bytes. Four bytes for the i32 and four bytes (including alignment) for the discriminant. A 100% increase in space is clearly a performance hit.

If, however, T is a non-nullable type, the Rust compiler is smart enough to use this null value as a sentinel and you end up with an Option<T> that only takes up the size of T.

So, for example, both a &i32 and an Option<i32> will take 8 bytes, no difference, zero waste.