Download presentation
Presentation is loading. Please wait.
Published byGeorgina Cook Modified over 8 years ago
1
Eclipse Collections An Inside Look March 2016 Copyright © 2016 Goldman Sachs. All rights reserved.
2
A GENDA Agenda What is Eclipse Collections? Eclipse Collections vs others Eclipse Collections by examples UnifiedMap : The memory saver UnifiedSet : It is a Set not a Map 2
3
What is Eclipse Collections? Copyright © 2016 Goldman Sachs. All rights reserved.
4
E CLIPSE C OLLECTIONS What is Eclipse Collections Eclipse Collections −Feature rich Java Collections framework −Project @ Eclipse Foundation http://www.eclipse.org/collections/ −Eclipse Collections released under EDL and EPLEDLEPL Open for contributionscontributions Eclipse Collections Kata −Tutorial developed to learn the framework https://github.com/eclipse/eclipse-collections-kata 4
5
Eclipse Collections vs Others Copyright © 2016 Goldman Sachs. All rights reserved.
6
E CLIPSE C OLLECTIONS VS OTHERS Eclipse Collections vs Others 6 FeaturesEC 7.0.0Java 8GuavaTroveScala Rich API InterfacesReadable, Mutable, Immutable, FixedSize, Lazy Mutable, StreamMutable, FluentMutableReadable, Mutable, Immutable, Lazy Optimized Set & Map (+Bag) Immutable Collections Primitive Collections (+Bag, +Immutable) Multimaps (+Bag, +SortedBag) (+Linked)(Multimap trait) Bags (Multisets) BiMaps Iteration StylesEager/Lazy, Serial/Parallel Lazy, Serial/Parallel Lazy, Serial Eager, Serial Eager/Lazy, Serial/Parallel (Lazy Only)
7
E CLIPSE C OLLECTIONS VS J AVA 8 S TREAMS Eclipse Collections vs Java 8 Streams 7 Eclipse Collections 7.0.0 Eager & Lazy, Serial & Parallel Memory efficient containers Primitive containers (all 8) Immutable containers More container types More iteration patterns “With” method patterns “target” method patterns Covariant return types Java 5+ compatible Eclipse Collections 7.0.0 Eager & Lazy, Serial & Parallel Memory efficient containers Primitive containers (all 8) Immutable containers More container types More iteration patterns “With” method patterns “target” method patterns Covariant return types Java 5+ compatible Java 8 Streams -Functional APIs -Lazy only -Single use -Serial & Parallel -Primitive streams (3 types) -Extensible Collectors Java 8 Streams -Functional APIs -Lazy only -Single use -Serial & Parallel -Primitive streams (3 types) -Extensible Collectors
8
Eclipse Collections by Example Copyright © 2016 Goldman Sachs. All rights reserved.
9
UnifiedMap: The memory saver Copyright © 2016 Goldman Sachs. All rights reserved.
10
H ASH M AP HashMap 10 JDK HashMap is backed by an Entry Entry holds key, value, next and hash transient Node [] table Node implements Map.Entry
11
U NIFIED M AP UnifiedMap EC UnifiedMap is backed by an array protected transient Object[] table Key, value in consecutive slots improves cache locality 11
12
U NIFIED M AP The Unification /** * Entry objects are not stored in the table * like in java.util.HashMap. Instead of trying * to deal with collisions in the main array * using Entry objects, we put a special object * in the key slot and put a regular Object[] * in the value slot. The array contains the * key value pairs in consecutive slots, just * like the main array, but it's a linear list * with no hashing. * The final result is a Map implementation * that's leaner than java.util.HashMap and * faster than Trove's THashMap. The best of * both approaches unified together, and thus * the name UnifiedMap. */ 12
13
U NIFIED M AP UnifiedMap public V get(Object key) { int index = this.index(key); Object cur = this.table[index]; if (cur != null) { Object val = this.table[index + 1]; if (cur == CHAINED_KEY) { return this.getFromChain((Object[])val,(K)key); } if (this.nonNullTableObjectEquals(cur, (K) key)) { return (V) val; } } return null; } 13
14
M EMORY C OMPARISON Comparing Maps Save half the memory 14
15
P ERFORMANCE C OMPARISON Comparing Maps Map.get() JMH Tests ElementOps/sec – higher the better 15
16
P ERFORMANCE C OMPARISON Comparing Maps Map.put() JMH Tests (non-presized) ElementOps/sec – higher the better 16
17
P ERFORMANCE C OMPARISON Comparing Maps Map.put() JMH Tests (presized) ElementOps/sec – higher the better 17
18
UnifiedSet: It is a Set not a Map Copyright © 2016 Goldman Sachs. All rights reserved.
19
H ASH S ET HashSet 19 JDK HashSet is backed by HashMap private transient HashMap map Values in each (key, value) pair are a waste of space Key, value pairs in HashMap are wrapped in an Entry leading to additional memory wastage
20
U NIFIED S ET UnifiedSet 20 EC UnifiedSet is backed by an array protected transient Object[] table Not backed by Map
21
M EMORY C OMPARISON Comparing Sets Save 4x the memory 21
22
Learn more at GS.com/Engineering © 2016 Goldman Sachs. This presentation should not be relied upon or considered investment advice. Goldman Sachs does not warrant or guarantee to anyone the accuracy, completeness or efficacy of this presentation, and recipients should not rely on it except at their own risk. This presentation may not be forwarded or disclosed except with this disclaimer intact.
23
Appendix – More Features Copyright © 2016 Goldman Sachs. All rights reserved.
24
M ORE F EATURES The “as” methods (O(1) cost) 24
25
M ORE F EATURES The “to” methods (O(n) cost) 25
26
M ORE F EATURES Handling Exceptions 26 Using Java Collections Using Eclipse Collections
27
Appendix – Primitive Specialization Copyright © 2016 Goldman Sachs. All rights reserved.
28
P RIMITIVE S PECIALIZATION Primitive Object Boxing is expensive −Reference + Header + alignment −Wrapper classes are immutable Unboxing is expensive 28
29
P RIMITIVE S PECIALIZATION Primitive Object From Oracle Java guide “You can’t put an int (or other primitive value) into a collection. Collection s can only hold object references, so you have to box primitive values into the appropriate wrapper class. An Integer is not a substitute for an int ; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.” Source: http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html 29
30
P RIMITIVE S PECIALIZATION List Java has object and primitive arrays Java 8 has primitive Stream s −They cannot be stored in a field −They are not reusable Java does not have primitive List s, Set s, Map s 30
31
P RIMITIVE S PECIALIZATION Primitive Lists 31 intList longList shortList floatList doubleList charList byteList booleanList Primitive Sets are similar Primitive Maps have combination of primitives
32
P RIMITIVE S PECIALIZATION Primitive Lists – Memory Comparison 32
33
P RIMITIVE S PECIALIZATION Primitive Sets – Memory Comparison 33
34
P RIMITIVE S PECIALIZATION Primitive Maps – Performance Comparison 34
35
P RIMITIVE S PECIALIZATION Primitive Maps – Memory Comparison 35
36
Appendix – Resources Copyright © 2016 Goldman Sachs. All rights reserved.
37
R ESOURCES Resources 37 Eclipse Collections Home Page http://www.eclipse.org/collections/ Eclipse Collections on GitHub https://github.com/eclipse/eclipse-collections https://github.com/eclipse/eclipse-collections/wiki https://github.com/eclipse/eclipse-collections-kata GS Collections Memory Benchmark http://www.goldmansachs.com/gs- collections/presentations/GSC_Memory_Tests.pdf Conference Talks and Meetups https://github.com/eclipse/eclipse- collections/wiki/Conference-talks-and-meetups
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.