Crates.io | appendlist |
lib.rs | appendlist |
version | 1.4.0 |
source | src |
created_at | 2019-06-23 19:58:12.688822 |
updated_at | 2019-07-06 20:21:15.955958 |
description | An append-only list that preserves references to its elements |
homepage | |
repository | https://github.com/danieldulaney/appendlist |
max_upload_size | |
id | 143058 |
size | 23,839 |
This list lets you add new things to the end, even if you're holding a reference to something already inside it. It also avoids reallocations.
When should you use it?
Vec
reallocations are significantWhen shouldn't you use it?
Vec<T>
)Vec
reallocations don't matter very much (normally the case!)What are some features?
push(&self, item: T)
method (you'd expect push(&mut self, item: T)
)Vec
, or indexing is linear-time, like LinkedList
)This example fails to compile because second_element
has a reference into
list
when list.push()
is called.
let list: Vec<u32> = (1..=10).collect();
let second_element = &list[1];
list.push(11); // Push needs &mut self, but list is already borrowed
assert_eq!(*second_element, 2); // Fails to compile
But if you just swap in AppendList
for Vec
, everything works!
use appendlist::AppendList;
let list: AppendList<u32> = (1..=10).collect();
let second_element = &list[1];
list.push(11); // Push only needs &self, so this works fine
assert_eq!(*second_element, 2); // All OK!
In general, Vec
s are pretty cool, and you should use them by default. But they
have a weakness: when you create
one, it gets created with a finite amount of space. When it runs out of space,
it needs to reallocate: grab a new hunk of memory (usually twice as big as
the current one) and copy everything over, then release the old memory.
Reallocations take O(n) time to do: you need to copy all n elements in the list.
But you don't have to do them very often: only O(log n) reallocations are needed
to do n insertions (for the current Vec
implementation). In fact, the
reallocations are rare enough that if you spread
them out across all the insertions, they just add a constant extra time.
This is why the Rust docs say that Vec
has "O(1) amortized
push" -- it generally takes constant time to push and it occasionally takes linear
time, but if you spread out those expensive pushes over all the cheap pushes,
it's still constant.
Reallocations have another issue: any reference to an element inside the Vec
is invalidated. If you have a reference to one of the elements when it gets
copied over, your reference has no way of knowing its new location and will
still point to the old location, which is now invalid memory. Using that reference
would be a use-after-free bug, so Rust forces you to have no references into a
Vec
before you push another element on, just in case that push would reallocate.
The AppendList
solves both issues by keeping a Vec
of chunks of data. When
you push a new element on, it goes to the end of the current chunk. If the chunk
is full, rather than reallocate it, AppendList
creates a whole new chunk that
starts off empty. Each
chunk is double the size of the last chunk, so only O(log n) allocations are
needed, and each one takes constant time (for most allocators) rather than linear
time. By eliminating reallocations, an AppendList
gives you a couple of benefits
compared to Vec
:
However, it also comes with some drawbacks:
Probably not.
In general, you should just use a Vec
and keep track of indices rather than
references. But if keeping references is very important, then this is your solution.