I'm studying salsa -- in this line https://github.com/salsa-rs/salsa/blob/master/src/interned.rs#L272 how do we guarantee that
values.len() won't be 0 ? It's initialized as an empty vector, so it seems to me that it's possible to get a
0 there, but that would break
InternId (which can't be 0), so I'm wondering what I'm missing.
tables.values.len() is a
I was expecting that we'd call this line https://github.com/salsa-rs/salsa/blob/fecec6bab2ea218b1fad45aa910cddb85284fc29/src/intern_id.rs#L115
Yes, we do, and
new_uncecked does +1
Ahh, I see. Thanks!
Another related question, why is the max value of InterId defined as
0xFFFF_FF00 instead of
For future compatability
Ideally, we want to use not the
NonZero, but "highest two bits are zero"
This is what rustc is using, and it is measurably faster
but to do that, you need unstable API to specify valid patterns precisely
Do you mean least significant bits? Why does that matter for performance?
no, I mean highest
with no-zero, you need to do
- 1 for every index access, which is not free
with "highlest two bits are always zero", you don't need to do anything special for indexing, but you still have space for niche-filling
Why does highest bits matter for
- 1 performance? What do you mean by "special"?
idx is a NonZeroU32 index. To access vector element, we need to do
xs[idx as usize - 1] (we need -1, b/c we added one when creating the index)
idx is TwoHighestBitsAreZero index. To access vector element, we need to do
xs[idx as usize]
Ahh, so same optimizations are applied to TwoHighestBitsAreZero (e.g. Option<TwoHighestBitsAreZero> has the same size as TwoHighestBitsAreZero) ?
OK, but I still can't relate that to the max value chosen for InterId. Max value for TwoHighestBitsAreZero would be
0x3FFFFFFF, I think
riiight, I think this actually might be a bug?
Or it might also be me not understanding things...
OK, thanks for the answers. This has been educational to me. Another question: why does InternId implement From<u32> and From<usize>? That means users of salsa can invent InternIds out of thin air, which shouldn't be possible, I think. I'd expect to obtain InternIds only by interning.