Misleading example of niche optimization in Day 3: Morning / Smart Pointers / Box / More to Explore #1943
Replies: 2 comments 1 reply
-
I agree this in accurate! I wonder if we should simplify the explanation a little by leaving out the linked-list aspect altogether. The crux of the optimization is what you say about the list iterator: Do you want to make a PR to update and simplify this? I am leaning toward leaving it in the speaker notes, but could be convinced to make this a sub-slide of the |
Beta Was this translation helpful? Give feedback.
-
Actually this optimization is not a feature of Box, but a feature of enum. And it is already mentioned in a chapter "User-defined Types / Enums / More to Explore". So, may be like this:
If all is correct, i'll make PR |
Beta Was this translation helpful? Give feedback.
-
In the example of niche optimization
an image of memory layout
doesn't match real memory layout.
Though niche optimization occurs in this example (enum tag elimination), List::Nil is not represented by "null pointer behind a curtains", but is a full-sized object on a heap, pointed by non-null pointer in a second field of Element(2). So, we still have layout
You can see this if you change the example like this:
To reduce memory consumption to only two objects and get a real advantage of niche optimization, example must be like this:
In this example list and iterator are actually just a nullable pointer, where null pointer means None, non-null pointer means Some<Box> or Some<&ListItem>, and memory layout is
Beta Was this translation helpful? Give feedback.
All reactions