20160801 efficient immutable collections · 6 michael j. steindorfer 32 0 2 0 34 1 24 898 28 2 0...
TRANSCRIPT
![Page 1: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/1.jpg)
Immutable Collections
Data Structure Encoding VM Support
Language and API DesignMichael J. Steindorfer
mail: [email protected]: @loopingoptimism
![Page 2: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/2.jpg)
![Page 3: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/3.jpg)
“A collection represents a group of objects […]”
–java.util.Collection API
3
![Page 4: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/4.jpg)
Data Type Semanticslists, sets, maps, multi-maps, bags, …
Orderingunordered (hashed), ordered
Update Semanticsmutable, transient, immutable
Processing Semanticssequential, concurrent, parallel
Encodingarray/hashtable, linked list, tree, trie, …
Contenthomogeneous, heterogeneous
…4
![Page 5: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/5.jpg)
Data Type Semanticslists, sets, maps, multi-maps, bags, …
Orderingunordered (hashed), ordered
Update Semanticsmutable, transient, immutable
Processing Semanticssequential, concurrent, parallel
Encodingarray/hashtable, linked list, tree, trie, …
Contenthomogeneous, heterogeneous
…5
![Page 6: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/6.jpg)
Why Immutability?
• More Optimizations (Constant)
• Simple (Programming Model)
• Robust (Concurrency)
6
![Page 7: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/7.jpg)
“Classes should be immutable unless there’s a very good reason to make them mutable.”
–Effective Java, 2nd Edition (Joshua Bloch, 2008)Item 15: Minimize mutability
7
![Page 8: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/8.jpg)
“[Collections] should be immutable unless there’s a very good reason to make them mutable.”
–Effective Java, 2nd Edition (Joshua Bloch, 2008)Item 15: Minimize mutability
8
![Page 9: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/9.jpg)
“Immutable [collections] provide many advantages, and their only disadvantage is the potential for
performance problems under certain circumstances.”
–Effective Java, 2nd Edition (Joshua Bloch, 2008)Item 15: Minimize mutability
9
![Page 10: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/10.jpg)
Immutable collections are challenging to optimize, but overall they constitute a more sensible default.
–Personal Opinion
10
![Page 11: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/11.jpg)
Outline
1. Core Principles & Encodings
2. Minimize Memory Footprint
3. Efficiency & Expressivity
4. Performance Challenges
11
![Page 12: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/12.jpg)
Core Principles
12
![Page 13: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/13.jpg)
Set<Integer> updatedSet = hugeSet.newInstanceWith(34);
13
Set<Integer> __tmp = new HashSet<>(hugeSet.size + 1); __tmp.addAll(hugeSet);__tmp.add(34);
Set<Integer> updatedSet = Collections.unmodifiableSet(__tmp);
![Page 14: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/14.jpg)
• • 32
•… • •
2 4098
… …
……
• • 32
• 34
Delta
14
![Page 15: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/15.jpg)
15
Clojurehttp://clojure.org
Scalahttp://scala-lang.org
Rascalhttp://rascal-mpl.org
![Page 16: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/16.jpg)
Hash Array Mapped Trie
16
![Page 17: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/17.jpg)
0001011010
0000001110 01111
11000
… …
• • 32
•… • •
2 4098……
17
Prefix of Hash Code
tree node as array
![Page 18: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/18.jpg)
{2, 32, 34, 898, 2942, 4098, 6014}
18
![Page 19: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/19.jpg)
19
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
320 1 2 3 4 5 6
...... 31
034
1 2...
... 24...
...898
28...
... 31
20
......
40984 5 6 7 8
...... 31
029421
...... 4
60145 6 7
...... 31
Fig. 2: Examples for presentation.
![Page 20: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/20.jpg)
class Node { int bitmap; Object[] content;}
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
Fig. 2: Examples for presentation.
![Page 21: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/21.jpg)
What can you expect?
• Shallow and Wide Search Trees (Depth <= 7)
• Lookup/Insert/Delete in O(log32(n)) ≅ O(1)
• Union/Subset (Structural Operations)
![Page 22: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/22.jpg)
Minimize Memory Footprintby Specializing Class Layouts
22
![Page 23: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/23.jpg)
class Node { int bitmap; Object[] content;}
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
Fig. 2: Examples for presentation.
![Page 24: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/24.jpg)
class ArrayList32 { /* 0 <= content.size <= 32 */ Object[] content;}
24
![Page 25: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/25.jpg)
class ArrayList32 { Object[] content;}
25
class List1 implements ImmutableList { Object slot0;}
class List2 implements ImmutableList { Object slot0; Object slot1;}
class List3 implements ImmutableList { Object slot0; Object slot1; Object slot2;}
...
![Page 26: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/26.jpg)
! Small Footprints ☹Megamorphic Call Sites ☹ Code Bloat
26
![Page 27: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/27.jpg)
abstract class List2 implements ImmutableList { Object slot0; Object slot1; @Override public ImmutableList insertAtIndex( int index, Object item) { switch (index) { case 0: return new List3(item, slot0, slot1); case 1: return new List3(slot0, item, slot1); case 2: return new List3(slot0, slot1, item); default: throw new IllegalArgumentException(); } }}
27
![Page 28: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/28.jpg)
! Small Footprints !Monomorphic Call Sites ! JIT-Friendly
28
![Page 29: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/29.jpg)
Decoupling Data and Operations
29
![Page 30: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/30.jpg)
abstract class List2 implements ImmutableList { Object slot0; Object slot1;}
// viewing consecutive fields as arrays createArrayView(List2.class, "slot0", “slot1”, Object.class);
31
![Page 31: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/31.jpg)
ImmutableList insertAtIndex(int index, Object item) { ArrayView<Object> src = getArrayView(); ImmutableList newList = allocateList(src.length + 1); ArrayView<Object> dst = newList.getArrayView(); // copy 'src' and insert element at position 'index' arrayviewcopy(src, 0, dst, 0, index); dst.set(index, item); arrayviewcopy(src, index, dst, index + 1, src.length - index);
return newNode; }
![Page 32: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/32.jpg)
Efficiency & Expressivity
33
![Page 33: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/33.jpg)
class Node { int bitmap; Object[] content;}
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
Fig. 2: Examples for presentation.
![Page 34: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/34.jpg)
More Performant …
35
![Page 35: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/35.jpg)
Up to 6x faster …(speedup of iteration over Clojure / Scala)
36
Steindorfer, M. J., & Vinju, J. J. (2015). Optimizing Hash-Array Mapped Tries for Fast and Lean Immutable JVM Collections. OOPSLA’15.
![Page 36: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/36.jpg)
… up to 28x faster …(speedup equality checking over Clojure / Scala)
37
Steindorfer, M. J., & Vinju, J. J. (2015). Optimizing Hash-Array Mapped Tries for Fast and Lean Immutable JVM Collections. OOPSLA’15.
![Page 37: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/37.jpg)
… and up to 64% smaller.(memory footprint reduction over Clojure / Scala)
38
Steindorfer, M. J., & Vinju, J. J. (2015). Optimizing Hash-Array Mapped Tries for Fast and Lean Immutable JVM Collections. OOPSLA’15.
![Page 38: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/38.jpg)
39
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
Fig. 2: Examples for presentation.
![Page 39: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/39.jpg)
40
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
Fig. 2: Examples for presentation.
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
320 1 2 3 4 5 6
...... 31
034
1 2...
... 24...
...898
28...
... 31
20
......
40984 5 6 7 8
...... 31
029421
...... 4
60145 6 7
...... 31
Fig. 2: Examples for presentation.
![Page 40: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/40.jpg)
class Node { int datamap; int nodemap; Object[] content;}
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
320 1 2 3 4 5 6
...... 31
034
1 2...
... 24...
...898
28...
... 31
20
......
40984 5 6 7 8
...... 31
029421
...... 4
60145 6 7
...... 31
Fig. 2: Examples for presentation.
![Page 41: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/41.jpg)
Heterogeneous Data Structures for the Masses 5
320-Key
B0-Val 2-Key 2-Val
0-Key 0-Val
341-Key
C1-Val 24-Key 24-Val
89828-Key
D28-Val
20-Key
A0-Val
40984-Key
G4-Val
29421-Key
F1-Val
60145-Key
H5-Val
320-Key
B0-Val 2-Ref
341-Key
C1-Val
89828-Key
D28-Val 24-Ref 0-Ref
20-Key
A0-Val
40984-Key
G4-Val
29421-Key
F1-Val
60145-Key
H5-Val
B Examples for Truffle’15 Talk
C Examples for OOPSLA’15 Talk
42
![Page 42: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/42.jpg)
Heterogeneous Data Structures for the Masses 5
320-Key
B0-Val 2-Key 2-Val
0-Key 0-Val
341-Key
C1-Val 24-Key 24-Val
89828-Key
D28-Val
20-Key
A0-Val
40984-Key
G4-Val
29421-Key
F1-Val
60145-Key
H5-Val
320-Key
B0-Val 2-Ref
341-Key
C1-Val
89828-Key
D28-Val 24-Ref 0-Ref
20-Key
A0-Val
40984-Key
G4-Val
29421-Key
F1-Val
60145-Key
H5-Val
B Examples for Truffle’15 Talk
C Examples for OOPSLA’15 Talk
43
![Page 43: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/43.jpg)
More Expressive …
44
![Page 44: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/44.jpg)
Numeric Data SetsStoring and Processing with Collections
![Page 45: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/45.jpg)
data set ofint (32-bit)
SpecializedPrimitive
Collection
![Page 46: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/46.jpg)
data set ofBigInteger (Object)
Generic Collection
![Page 47: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/47.jpg)
Primitive Collection ???
data set ofint (32-bit) ✘
![Page 48: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/48.jpg)
Generic Collection ???
data set ofBigInteger (Object)
data set ofInteger (Boxed)
![Page 49: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/49.jpg)
data set ofint (32-bit)
data set ofBigInteger (Object)
Heterogeneous Collections
![Page 50: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/50.jpg)
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
BIG
320 2
341 24
89828 0
BIG 20
40984
320 2
341
89828 24 0
BIG 20
40984
Fig. 2: HAMT-based sets with values in internal nodes versus values at the leavesonly.
51
![Page 51: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/51.jpg)
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
BIG
320 2
341 24
89828 0
BIG 20
40984
320 2
341
89828 24 0
BIG 20
40984
Fig. 2: HAMT-based sets with values in internal nodes versus values at the leavesonly.
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
BIG
320 2
341 24
89828 0
BIG 20
40984
320 2
341
89828 24 0
BIG 20
40984
Fig. 2: HAMT-based sets with values in internal nodes versus values at the leavesonly.
52
Steindorfer, M. J., & Vinju, J. J. (2016). Fast and Lean Immutable Multi-Maps on the JVM based on Heterogeneous Hash-Array Mapped Tries. To Appear.
![Page 52: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/52.jpg)
00 1 00 1 … 0 0 00
01 0 00 0 … 1 1 10
class Node {
Object[] content;}
int datamap; int nodemap; 2x 1-bit
![Page 53: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/53.jpg)
00 1 … 001 0 … 1
BitVector bitmap;class Node {
Object[] content;}
1x n-bit
![Page 54: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/54.jpg)
00 1 … 001 0 … 1
BitVector bitmap;class Node {
Object[] content;}
1x n-bitOrthogonal To:• Scala’s Union Types • Valhalla’s Primitive Generics
![Page 55: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/55.jpg)
Performance Challenges
55
![Page 56: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/56.jpg)
Trees (as fast) as Arrays?Improving Locality between Nodes.
56
![Page 57: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/57.jpg)
• • 32
•… • •
2 4098
… …
……
57
![Page 58: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/58.jpg)
• • 32 •… • • 2 4098……
58
![Page 59: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/59.jpg)
• • 32
•… • •
2 4098
… …
……
• • 32
• 34
59
![Page 60: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/60.jpg)
• • 32 •… • • 2 4098……
60
• • 32 • 34
![Page 61: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/61.jpg)
Trees (as fast) as Arrays!Memory Management & Garbage Collection
61
![Page 62: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/62.jpg)
Summary
![Page 63: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/63.jpg)
15
Clojurehttp://clojure.org
Scalahttp://scala-lang.org
Rascalhttp://rascal-mpl.org
data set ofint (32-bit)
data set ofBigInteger (Object)
Heterogeneous Collections
class ArrayList32 { Object[] content;}
41
class List1 implements ImmutableList { Object slot0;}
class List2 implements ImmutableList { Object slot0; Object slot1;}
class List3 implements ImmutableList { Object slot0; Object slot1; Object slot2;}
...
40
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
Fig. 2: Examples for presentation.
6 Michael J. Steindorfer
320 2
034
1 24898
28
20
40984
29421
60145
320 2
341
89828 0 24
20
40984
29421
60145
320 2
341
89828 24 0
20
40984
29421
60145
320 1 2 3 4 5 6
...... 31
034
1 2...
... 24...
...898
28...
... 31
20
......
40984 5 6 7 8
...... 31
029421
...... 4
60145 6 7
...... 31
Fig. 2: Examples for presentation.
• • 32 •… • • 2 4098……
54
• • 32 • 34
![Page 64: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/64.jpg)
Map<String, Long> wordCount = phrases.stream()
.flatMap (toWordStream)
.filter (word -> word.length() > 0)
.map (word -> new Tuple<>(word, 1L))
.collect (groupingBy(Tuple::getKey, counting()));
![Page 65: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/65.jpg)
Map<String, Long> wordCount = phrases.stream()
.flatMap (toWordStream)
.filter (word -> word.length() > 0)
.map (word -> new Tuple<>(word, 1L))
.collect (groupingBy(Tuple::getKey, counting()));
Missing: Immutable Collections
![Page 66: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/66.jpg)
usethesource/capsuleThe Capsule Hash Trie Collections Library
![Page 67: 20160801 Efficient Immutable Collections · 6 Michael J. Steindorfer 32 0 2 0 34 1 24 898 28 2 0 4098 4 BIG 32 0 2 34 1 24 898 28 0 BIG 2 0 4098 4 32 0 2 34 1 898 28 24 0 BIG 2 0](https://reader033.vdocument.in/reader033/viewer/2022042122/5e9d104265ed335e1f59aa93/html5/thumbnails/67.jpg)
Immutable Collections
Data Structure Encoding VM Support
Language and API DesignMichael J. Steindorfer
mail: [email protected]: @loopingoptimism