arrsingh.com

Distributed Systems & Artificial Intelligence

A Fast LinkedList in Rust: Part 2 - Performance

I've designed and implemented a Doubly LinkedList in Rust that's fast and allows adding and removing nodes in the middle of list in constant time.

The deepmesa::collections::LinkedList maintains a free list of nodes and so does not have to allocate and deallocate memory everytime an element is added or removed from the list. This also allows the list to return Handles to nodes in the list and these handles are guaranteed to remain valid even between mutations of the list.

This design makes the list twice as fast as the std::collections::LinkedList.

push_front() is 2x faster than std::collections::LinkedList

Pushing an element to the front of the list returns a NodeHandle to the element added to the list. This operation is twice as fast as the same operation on the std::collections::LinkedList.

Mean Time for push_front() in deepmesa LinkedList vs the std::collections::LinkedList
PDF push_front() deepmesa LinkedList vs the std::collections::LinkedList
Linear Regression for push_front() vs std::collections::LinkedList

push_back() is 2x faster than std::collections::LinkedList

Similarly, pushing an element to the back of the list is twice as fast and for the same reason. Allocating and deallocating memory is expensive and the deepmesa LinkedList allocates memory upfront and reuses memory from a free list.

Mean Time for push_back() in deepmesa LinkedList vs std::collections::LinkedList
PDF for push_back() in deepmesa LinkedList vs std::collections::LinkedList
Linear Regression push_back() in deepmesa LinkedList vs std::collections::LinkedList

pop_front() is more 2x as fast as std::collections::LinkedList

Popping elements from the front of the list is even faster than the standard library linked list. This is attributable to the fact that the LinkedList in the standard library has to deallocate memory everytime an element is removed from the list. The deepmesa linked list, merely has to update a few pointers.

Mean Time pop_front() in deepmesa LinkedList vs std::collections::LinkedList
PDF for pop_front() in deepmesa LinkedList vs std::collections::LinkedList
Linear Regression for pop_front() in deepmesa LinkedList vs std::collections::LinkedList

pop_back() is also more than 2x faster than std::collections::LinkedList

Removing elements from the back of the list also shows the same performance characteristics as pop_front() and for the same reasons.

Mean Time for pop_back() in deepmesa LinkedList vs std::collections::LinkedList
PDF for pop_back in deepmesa LinkedList vs std::collections::LinkedList
Linear Regression for pop_back() in deepmesa LinkedList vs std::collections::LinkedList

Pushing to the middle of the list is 35% faster

The standard library LinkedList doesn't allow pushing to the middle of the list in constant time so this benchmark compared pushing to the middle of the deepmesa list vs pushing to the front of the std::collections::LinkedList.

Mean Time for pushing elements to the middle vs push_front() in std::collections::LinkedList
PDF for pushing elements to the middle vs push_front() in std::collections::LinkedList
Linear Regression for pushing elements to the middle vs push_front in std::LinkedList

The deepmesa linkedlist is twice as fast as the std::collections::LinkedList and is a drop in replacement.

Sign up for updates

No Thanks

Great! Check your inbox and click the link to confirm your subscription.