# HG changeset patch # User mduigou # Date 1364877481 25200 # Node ID 36d389d99afa281b6450fa504f28b085ccc5c274 # Parent de228734b7427e8586e48442c51720a50b826a09 80111200: (coll) Optimize empty HashMap and ArrayList Reviewed-by: mduigou Contributed-by: Sergey Linetskiy , John Rose , Mike Duigou diff --git a/src/share/classes/java/util/ArrayList.java b/src/share/classes/java/util/ArrayList.java --- a/src/share/classes/java/util/ArrayList.java +++ b/src/share/classes/java/util/ArrayList.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -105,6 +105,11 @@ private static final long serialVersionUID = 8683452581122892189L; /** + * Shared empty array instance used for empty instances. + */ + private static final Object EMPTY_ELEMENTDATA[] = new Object[0]; + + /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. */ @@ -136,7 +141,8 @@ * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { - this(10); + super(); + this.elementData = EMPTY_ELEMENTDATA; } /** @@ -162,8 +168,7 @@ */ public void trimToSize() { modCount++; - int oldCapacity = elementData.length; - if (size < oldCapacity) { + if (size < elementData.length) { elementData = Arrays.copyOf(elementData, size); } } @@ -177,11 +182,20 @@ */ public void ensureCapacity(int minCapacity) { if (minCapacity > 0) - ensureCapacityInternal(minCapacity); + ensureExplicitCapacity(minCapacity); } private void ensureCapacityInternal(int minCapacity) { + if(elementData == EMPTY_ELEMENTDATA) { + minCapacity = Math.max(10, minCapacity); + } + + ensureExplicitCapacity(minCapacity); + } + + private void ensureExplicitCapacity(int minCapacity) { modCount++; + // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); @@ -506,8 +520,7 @@ modCount++; // Let gc do its work - for (int i = 0; i < size; i++) - elementData[i] = null; + Arrays.fill(elementData, null); size = 0; } @@ -588,8 +601,8 @@ // Let gc do its work int newSize = size - (toIndex-fromIndex); - while (size != newSize) - elementData[--size] = null; + Arrays.fill(elementData, newSize, size, null); + size = newSize; } /** @@ -677,8 +690,8 @@ w += size - r; } if (w != size) { - for (int i = w; i < size; i++) - elementData[i] = null; + // Let gc do its work + Arrays.fill(elementData, w, size, null); modCount += size - w; size = w; modified = true; @@ -702,7 +715,7 @@ s.defaultWriteObject(); // Write out array length - s.writeInt(elementData.length); + s.writeInt((elementData == EMPTY_ELEMENTDATA) ? 10 : elementData.length); // Write out all elements in the proper order. for (int i=0; i