Make LinkedList a Union instead of Abstract type #877
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello. I have a proposal for an improvement of the implementation of the linked list data structure.
The way the LinkedList is implemented right now
LinkedList
is an abstract datatype, andCons
contains a field of typeLinkedList
. This means that any operation on LinkedLists result in a ton of unavoidable runtime-dispatch.I noticed that a small redesign where
Nil
andCons
are standalone types andLinkedList
just refers to their Union provides a speedup of more than an order of magnitude, as the compiler can create very efficient code for simple Unions of a concrete and a singleton type. The only disadvantage I see in this is that users can no longer extend the LinkedList type with new types, but it may be argued that this would not be intended behavior anyway.I also made the two types immutable. They used to be mutable, but the mutability was never utilized. If there is some reason to make these mutable (like forcing the compiler to put them on the heap) let me know.