Manishearth / elsa

Append-only collections for Rust where borrows to entries can outlive insertions
Apache License 2.0
228 stars 33 forks source link

Questions about `FrozenVec` #64

Closed bzm3r closed 1 year ago

bzm3r commented 1 year ago

(This is a discussion, rather than an issue. Can it be converted into one?)

Two questions, about FrozenVec:

0) Am I right in understanding that FrozenVec will never be re-allocated on resize?

1) is FrozenVec more prone to fragmentation issues as it grows, since it will never be re-allocated on resize?

2) why does FrozenVec not expose a with_capacity-like for creating new FrozenVecs?

(Thank you! 🙏)

Manishearth commented 1 year ago
  • Am I right in understanding that FrozenVec will never be re-allocated on resize?

No, it will.

1. why does FrozenVec not expose a with_capacity-like for creating new FrozenVecs?

It could, it wasn't needed so far. Happy to merge a PR for it.

bzm3r commented 1 year ago

I think I can make an attempt to answer my questions, based on some thinking + reading of the source.

  1. No, it is not true that the underlying Vec backing a FrozenVec will never be re-allocated on resize. (At least, I could not find how this would be prevented, in the source.) **This does not break FrozenVec's underlying StableDeref's requirements, because any reference to something held within Vec is still valid across re-allocation, after all Vec implement's StableDeref. (how?)

  2. is moot given 0.

  3. Not sure.

bzm3r commented 1 year ago

@Manishearth Ah, you beat me to it! 🤣

Now the puzzle in my mind is around how Vec's refs stay stable across re-allocation.

bzm3r commented 1 year ago

Okay, I missed this: https://docs.rs/elsa/latest/src/elsa/vec.rs.html#40

So, FrozenVec works because the elements of the underlying Vec are StableDeref. That Vec is StableDeref is irrelevant, because that only seeks to confirm: "a reference to the Vec is stable upon its re-allocation" and says nothing about references to the entries of the Vec. So, this answers all my questions, and makes them moot.

Re: with_capacity it makes sense that it hasn't come up yet, and I don't think it has come up yet for me either, so its not important. My primary motivation for it was based on no-re-alloc concern I had when I was misunderstanding FrozenVec.