Immutable Data Structures
Eric Fu
2018-06-15
Agenda
• Stack
• Tree
• Vector Trie
• HAMT
• Git
• LSM-Tree
• Practice
Immutable Stack
A Clean Example for Starters
CopyOnWriteArrayList ?
Stack
Stack
Stack
Stack
Immutable Tree
Tree
Tree
RB-Tree
RB-Tree
Vector Trie
Immutable Array List
Trie
Also called:
• Dictionary tree
• Radix tree
• Prefix tree
• Digital search tree
Trie (compressed)
• With prefix only
"A", "to", "tea", "ted"
"ten", "i", "in", "inn"
Vector Trie
Vector Trie (Mutable)
Vector Trie - Update
Vector Trie - Push
Vector Trie – Push (1/3)
Vector Trie – Push (2/3)
Vector Trie – Push (3/3)
Vector Trie – Pop (1/3)
Vector Trie – Pop (2/3)
Vector Trie – Pop (3/3)
Complexity
• Mostly....
Go Efficient
• More branches
Go Efficient
• More branches
• Align to binary bits
Go Efficient
• More branches – 32
• Align to binary bits – 5 bits
Hash Array Mapped Trie (HAMT)
Immutable Hash Table
Rethink Vector Trie
• Must the index be continuous (0, 1, 2...) ?
• Actually not!
• Furthermore: hash(anything) -> integer
HAMT - Naive
• Contains {0x00, 0xff}
HAMT – Bitmap Compressed
• Compress each node with bitmap
HAMT - Pruned
• Leave out unnecessary nodes
Some Real-world Examples
Git
How Git works?
• Git stored all versions of all directories and files
• A Git commit is a snapshot of root directory
• A Git branch/tag is a reference to some commit
That's it!
Log-Structured Merge-Tree (LSM-
Tree)
More Trees
Insights
• Modify in memory
• Read merged results
• Newly merged blocks are appended
Modern LSM-Tree Implements
• Memtable
• Immutable Memtable
• Level 0 (overlapped)
• Level 1
• Level 2
• ....
Memory
Disk
Write
Read
61
Immutable in Practice
In Java
A Familiar Example
• Java's string vs. CPP's string
Easy to understand
• Same name, same value
Mutable Request
Immutable Request:
Always thread-safe
Always thread-safe
• Immutable to programming as MVCC to database
• Lock or Snapshot
Other Pros
• Friendly for caching
• Atomic Failure
• Avoid null-pointer exception
Java Example 1
Java Example 2
• Requirement: cache the results of factorizing
• Two variable with setter?
• Two atomic variable?
Java Example 2 (cont.)
Java Example 2 (cont.)
Thanks!
Q&A

Immutable Data Structures