RFR JDK-8011200 (was 7143928) : (coll) Optimize for Empty ArrayList and HashMap
Martin Buchholz
martinrb at google.com
Mon Apr 8 23:03:24 UTC 2013
Style: spaces around "="
+ private static final int DEFAULT_CAPACITY= 10;
---
It's a matter of taste whether to remove the temp var oldCapacity. I
believe it was coded intentionally.
public void trimToSize() {
modCount++;
- int oldCapacity = elementData.length;
- if (size < oldCapacity) {
+ if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
---
754 throws java.io.IOException, ClassNotFoundException { 755
elementData = EMPTY_ELEMENTDATA;
Your indentation is off.
---
Because "threshold" is a serialized field, its javadoc is part of the
public interface of this class, and hence cannot refer to implementation
details like EMPTY_TABLE.
161 /** 162 * The next size value at which to resize
(capacity * load factor). If 163 * table == EMPTY_TABLE then this
is the initial capacity at which the 164 * table will be created
when inflated.
165 * @serial
166 */
167 int threshold;**
*
http://docs.oracle.com/javase/7/docs/api/serialized-form.html#java.util.HashMap
*
*
*
---
Consider making roundUpToPowerOf2 public.
Best Implementation is not obvious. I would probably write a loop with core
x = x & (x - 1)
until get to zero.
274 private static int roundUpToPowerOf2(int number) { 275
int rounded = number >= MAXIMUM_CAPACITY 276 ?
MAXIMUM_CAPACITY 277 : (rounded =
Integer.highestOneBit(number)) != 0 278 ?
(Integer.bitCount(number) > 1) ? rounded << 1 : rounded 279
: 1; 280 281 return rounded; 282 }
---
I think it's a mistake to "optimize" for the empty case in code like this:
694 public boolean containsValue(Object value) { 695 if
(isEmpty()) { 696 return false; 697 } 698
More information about the core-libs-dev
mailing list