author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 19806 | dda89341ee2d |
permissions | -rw-r--r-- |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
1 |
/* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
4 |
* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
8 |
* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
13 |
* accompanied this code). |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
14 |
* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
18 |
* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
21 |
* questions. |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
22 |
*/ |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
23 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
24 |
import org.testng.annotations.DataProvider; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
25 |
import org.testng.annotations.Test; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
26 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
27 |
import java.util.Collection; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
28 |
import java.util.HashMap; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
29 |
import java.util.LinkedHashMap; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
30 |
import java.util.Map; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
31 |
import java.util.concurrent.ConcurrentHashMap; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
32 |
import java.util.function.BiConsumer; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
33 |
import java.util.stream.Collector; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
34 |
import java.util.stream.Collectors; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
35 |
import java.util.stream.IntStream; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
36 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
37 |
import static org.testng.Assert.assertEquals; |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
38 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
39 |
/* |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
40 |
* @test |
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
41 |
* @bug 8023463 |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
42 |
* @summary Test the case where a bin is treeified and vice verser |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
43 |
* @run testng MapBinToFromTreeTest |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
44 |
*/ |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
45 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
46 |
@Test |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
47 |
public class MapBinToFromTreeTest { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
48 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
49 |
// Initial capacity of map |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
50 |
// Should be >= the map capacity for treeifiying, see HashMap/ConcurrentMap.MIN_TREEIFY_CAPACITY |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
51 |
static final int INITIAL_CAPACITY = 64; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
52 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
53 |
// Maximum size of map |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
54 |
// Should be > the treeify threshold, see HashMap/ConcurrentMap.TREEIFY_THRESHOLD |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
55 |
// Should be > INITIAL_CAPACITY to ensure resize occurs |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
56 |
static final int SIZE = 256; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
57 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
58 |
// Load factor of map |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
59 |
// A value 1.0 will ensure that a new threshold == capacity |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
60 |
static final float LOAD_FACTOR = 1.0f; |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
61 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
62 |
@DataProvider(name = "maps") |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
63 |
static Object[][] mapProvider() { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
64 |
return new Object[][] { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
65 |
// Pass in the class name as a description for test reporting |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
66 |
// purposes |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
67 |
{ HashMap.class.getName(), new HashMap(INITIAL_CAPACITY, LOAD_FACTOR) }, |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
68 |
{ LinkedHashMap.class.getName(), new LinkedHashMap(INITIAL_CAPACITY, LOAD_FACTOR) }, |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
69 |
{ ConcurrentHashMap.class.getName(), new ConcurrentHashMap(INITIAL_CAPACITY, LOAD_FACTOR) }, |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
70 |
}; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
71 |
} |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
72 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
73 |
@Test(dataProvider = "maps") |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
74 |
public void testPutThenGet(String d, Map<HashCodeInteger, Integer> m) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
75 |
put(SIZE, m, (i, s) -> { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
76 |
for (int j = 0; j < s; j++) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
77 |
assertEquals(m.get(new HashCodeInteger(j)).intValue(), j, |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
78 |
String.format("Map.get(%d)", j)); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
79 |
} |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
80 |
}); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
81 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
82 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
83 |
@Test(dataProvider = "maps") |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
84 |
public void testPutThenTraverse(String d, Map<HashCodeInteger, Integer> m) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
85 |
Collector<Integer, ?, ? extends Collection<Integer>> c = getCollector(m); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
86 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
87 |
put(SIZE, m, (i, s) -> { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
88 |
// Note that it is OK to collect to a Set (HashSet) as long as |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
89 |
// integer values are used since these tests only check for |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
90 |
// collisions and other tests will verify more general functionality |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
91 |
Collection<Integer> actual = m.keySet().stream().map(e -> e.value).collect(c); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
92 |
Collection<Integer> expected = IntStream.range(0, s).boxed().collect(c); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
93 |
assertEquals(actual, expected, "Map.keySet()"); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
94 |
}); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
95 |
} |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
96 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
97 |
@Test(dataProvider = "maps") |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
98 |
public void testRemoveThenGet(String d, Map<HashCodeInteger, Integer> m) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
99 |
put(SIZE, m, (i, s) -> { }); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
100 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
101 |
remove(m, (i, s) -> { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
102 |
for (int j = i + 1; j < SIZE; j++) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
103 |
assertEquals(m.get(new HashCodeInteger(j)).intValue(), j, |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
104 |
String.format("Map.get(%d)", j)); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
105 |
} |
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
106 |
}); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
107 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
108 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
109 |
@Test(dataProvider = "maps") |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
110 |
public void testRemoveThenTraverse(String d, Map<HashCodeInteger, Integer> m) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
111 |
put(SIZE, m, (i, s) -> { }); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
112 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
113 |
Collector<Integer, ?, ? extends Collection<Integer>> c = getCollector(m); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
114 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
115 |
remove(m, (i, s) -> { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
116 |
Collection<Integer> actual = m.keySet().stream().map(e -> e.value).collect(c); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
117 |
Collection<Integer> expected = IntStream.range(i + 1, SIZE).boxed().collect(c); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
118 |
assertEquals(actual, expected, "Map.keySet()"); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
119 |
}); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
120 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
121 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
122 |
@Test(dataProvider = "maps") |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
123 |
public void testUntreeifyOnResizeWithGet(String d, Map<HashCodeInteger, Integer> m) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
124 |
// Fill the map with 64 entries grouped into 4 buckets |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
125 |
put(INITIAL_CAPACITY, m, (i, s) -> { }); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
126 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
127 |
for (int i = INITIAL_CAPACITY; i < SIZE; i++) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
128 |
// Add further entries in the 0'th bucket so as not to disturb |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
129 |
// other buckets, entries of which may be distributed and/or |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
130 |
// the bucket untreeified on resize |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
131 |
m.put(new HashCodeInteger(i, 0), i); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
132 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
133 |
for (int j = 0; j < INITIAL_CAPACITY; j++) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
134 |
assertEquals(m.get(new HashCodeInteger(j)).intValue(), j, |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
135 |
String.format("Map.get(%d) < INITIAL_CAPACITY", j)); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
136 |
} |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
137 |
for (int j = INITIAL_CAPACITY; j <= i; j++) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
138 |
assertEquals(m.get(new HashCodeInteger(j, 0)).intValue(), j, |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
139 |
String.format("Map.get(%d) >= INITIAL_CAPACITY", j)); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
140 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
141 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
142 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
143 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
144 |
@Test(dataProvider = "maps") |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
145 |
public void testUntreeifyOnResizeWithTraverse(String d, Map<HashCodeInteger, Integer> m) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
146 |
// Fill the map with 64 entries grouped into 4 buckets |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
147 |
put(INITIAL_CAPACITY, m, (i, s) -> { }); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
148 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
149 |
Collector<Integer, ?, ? extends Collection<Integer>> c = getCollector(m); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
150 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
151 |
for (int i = INITIAL_CAPACITY; i < SIZE; i++) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
152 |
// Add further entries in the 0'th bucket so as not to disturb |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
153 |
// other buckets, entries of which may be distributed and/or |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
154 |
// the bucket untreeified on resize |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
155 |
m.put(new HashCodeInteger(i, 0), i); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
156 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
157 |
Collection<Integer> actual = m.keySet().stream().map(e -> e.value).collect(c); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
158 |
Collection<Integer> expected = IntStream.rangeClosed(0, i).boxed().collect(c); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
159 |
assertEquals(actual, expected, "Key set"); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
160 |
} |
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
161 |
} |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
162 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
163 |
Collector<Integer, ?, ? extends Collection<Integer>> getCollector(Map<?, ?> m) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
164 |
Collector<Integer, ?, ? extends Collection<Integer>> collector = m instanceof LinkedHashMap |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
165 |
? Collectors.toList() |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
166 |
: Collectors.toSet(); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
167 |
return collector; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
168 |
} |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
169 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
170 |
void put(int size, Map<HashCodeInteger, Integer> m, BiConsumer<Integer, Integer> c) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
171 |
for (int i = 0; i < size; i++) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
172 |
m.put(new HashCodeInteger(i), i); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
173 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
174 |
c.accept(i, m.size()); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
175 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
176 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
177 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
178 |
void remove(Map<HashCodeInteger, Integer> m, BiConsumer<Integer, Integer> c) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
179 |
int size = m.size(); |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
180 |
// Remove all elements thus ensuring at some point trees will be |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
181 |
// converting back to bins |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
182 |
for (int i = 0; i < size; i++) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
183 |
m.remove(new HashCodeInteger(i)); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
184 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
185 |
c.accept(i, m.size()); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
186 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
187 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
188 |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
19806
diff
changeset
|
189 |
static final class HashCodeInteger implements Comparable<HashCodeInteger> { |
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
190 |
final int value; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
191 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
192 |
final int hashcode; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
193 |
|
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
194 |
HashCodeInteger(int value) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
195 |
this(value, hash(value)); |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
196 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
197 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
198 |
HashCodeInteger(int value, int hashcode) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
199 |
this.value = value; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
200 |
this.hashcode = hashcode; |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
201 |
} |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
202 |
|
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
203 |
static int hash(int i) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
204 |
// Assuming 64 entries with keys from 0 to 63 then a map: |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
205 |
// - of capacity 64 will have 4 buckets with 16 entries per-bucket |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
206 |
// - of capacity 128 will have 8 buckets with 8 entries per-bucket |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
207 |
// - of capacity 256 will have 16 buckets with 4 entries per-bucket |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
208 |
// |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
209 |
// Re-sizing will result in re-distribution, doubling the buckets |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
210 |
// and reducing the entries by half. This will result in |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
211 |
// untreeifying when the number of entries is less than untreeify |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
212 |
// threshold (see HashMap/ConcurrentMap.UNTREEIFY_THRESHOLD) |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
213 |
return (i % 4) + (i / 4) * INITIAL_CAPACITY; |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
214 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
215 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
216 |
@Override |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
217 |
public boolean equals(Object obj) { |
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
218 |
if (obj instanceof HashCodeInteger) { |
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
219 |
HashCodeInteger other = (HashCodeInteger) obj; |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
220 |
return other.value == value; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
221 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
222 |
return false; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
223 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
224 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
225 |
@Override |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
226 |
public int hashCode() { |
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
227 |
return hashcode; |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
228 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
229 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
230 |
@Override |
19806
dda89341ee2d
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents:
17939
diff
changeset
|
231 |
public int compareTo(HashCodeInteger o) { |
17939
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
232 |
return value - o.value; |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
233 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
234 |
|
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
235 |
@Override |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
236 |
public String toString() { |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
237 |
return Integer.toString(value); |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
238 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
239 |
} |
bd750ec19d82
8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents:
diff
changeset
|
240 |
} |