diff --git a/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java b/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java index c4e14a24..bbd98cb4 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java @@ -21,13 +21,13 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import java.util.Timer; import java.util.TimerTask; import com.zaxxer.hikari.HikariPool; +import com.zaxxer.hikari.util.FastList; /** * This is the proxy class for java.sql.Connection. @@ -42,7 +42,7 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy protected final Connection delegate; - private final ArrayList openStatements; + private final FastList openStatements; private final HikariPool parentPool; private boolean isClosed; @@ -73,7 +73,7 @@ public abstract class ConnectionProxy implements IHikariConnectionProxy this.delegate = connection; creationTime = lastAccess = System.currentTimeMillis(); - openStatements = new ArrayList(); + openStatements = new FastList(); } public final void unregisterStatement(Object statement) diff --git a/src/main/java/com/zaxxer/hikari/util/FastList.java b/src/main/java/com/zaxxer/hikari/util/FastList.java new file mode 100644 index 00000000..d89f2d4c --- /dev/null +++ b/src/main/java/com/zaxxer/hikari/util/FastList.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2013 Brett Wooldridge + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.zaxxer.hikari.util; + + +/** + * Fast list without range checking. + * + * @author Brett Wooldridge + */ +public class FastList +{ + private transient Object[] elementData; + + private int size; + + /** + * Construct a FastList with a default size of 16. + */ + public FastList() + { + this.elementData = new Object[16]; + } + + /** + * Construct a FastList with a specfied size. + * + * @param size the initial size of the FastList + */ + public FastList(int size) + { + this.elementData = new Object[size]; + } + + /** + * Add an element to the tail of the FastList. + * + * @param element the element to add + */ + public void add(E element) + { + if (size < elementData.length) + { + elementData[size++] = element; + } + else + { + // overflow-conscious code + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + Object[] newElementData = new Object[newCapacity]; + System.arraycopy(element, 0, newElementData, 0, oldCapacity); + newElementData[size++] = element; + elementData = newElementData; + } + } + + /** + * Get the element at the specified index. + * + * @param index the index of the element to get + * @return the element, or ArrayIndexOutOfBounds is thrown if the index is invalid + */ + @SuppressWarnings("unchecked") + public E get(int index) + { + return (E) elementData[index]; + } + + /** + * This remove method is most efficient when the element being removed + * is the last element. Equality is identity based, not equals() based. + * + * @param element the element to remove + */ + public void remove(Object element) + { + for (int index = size - 1; index >= 0; index++) + { + if (element == elementData[index]) + { + int numMoved = size - index - 1; + if (numMoved > 0) + { + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[--size] = null; + break; + } + } + } + + /** + * Clear the FastList. + */ + public void clear() + { + for (int i = 0; i < size; i++) + { + elementData[i] = null; + } + + size = 0; + } + + + /** + * Get the current number of elements in the FastList. + * + * @return the number of current elements + */ + public int size() + { + return size; + } +}