HikariCP源码分析之FastList
FastList是一个List接口的精简实现,只实现了接口中必要的几个方法。JDK ArrayList每次调用get()方法时都会进行rangeCheck检查索引是否越界,FastList的实现中去除了这一检查,只要保证索引合法那么rangeCheck就成为了不必要的计算开销(当然开销极小)。此外,HikariCP使用List来保存打开的Statement,当Statement关闭或Connection关闭时需要将对应的Statement从List中移除。通常情况下,同一个Connection创建了多个Statement时,后打开的Statement会先关闭。ArrayList的remove(Object)方法是从头开始遍历数组,而FastList是从数组的尾部开始遍历,因此更为高效。
简而言之就是 自定义数组类型(FastList)代替ArrayList:避免每次get()调用都要进行range check,避免调用remove()时的从头到尾的扫描。
源码分析
java.util.ArrayList
[AppleScript] 纯文本查看 复制代码
1
2
3
4
5
6
|
public class ArrayList < E > extends AbstractList < E >
implements List < E > , RandomAccess , Cloneable , java.io.Serializable < / code >
com.zaxxer.hikari.util.FastList
< code class = "" > public final class FastList < T > implements List < T > , RandomAccess , Serializable
|
我们先看一下java.util.ArrayList的get方法
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
|
/ * *
* Returns the element at the specified position in this list .
*
* @param index index of the element to return
* @ return the element at the specified position in this list
* @throws IndexOutOfBoundsException {
@inheritDoc }
* /
public E get ( int index ) {
rangeCheck ( index ) ;
return elementData ( index ) ;
}
|
我们可以看到每次get的时候都会进行rangeCheck
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
|
/ * *
* Checks if the given index is in range. If not , throws an appropriate
* runtime exception. This method does * not * check if the index is
* negative : It is always used immediately prior to an array access ,
* which throws an ArrayIndexOutOfBoundsException if index is negative.
* /
private void rangeCheck ( int index ) {
if ( index > = size )
throw new IndexOutOfBoundsException ( outOfBoundsMsg ( index ) ) ;
}
|
我们对比一下com.zaxxer.hikari.util.FastList的get方法,可以看到取消了rangeCheck,一定程度下追求了极致
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
|
/ * *
* 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
* /
@Override
public T get ( int index ) {
return elementData[ index ];
}
|
我们再来看一下ArrayList的remove(Object)方法
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/ * *
* Removes the first occurrence of the specified element from this list ,
* if it is present. If the list does not contain the element , it is
* unchanged. More formally , removes the element with the lowest index
* < tt > i < / tt > such that
* < tt > ( o = = null ? get ( i ) = = null : o. equals ( get ( i ) ) ) < / tt >
* ( if such an element exists ) . Returns < tt > true < / tt > if this list
* contained the specified element ( or equivalently , if this list
* changed as a result of the call ) .
*
* @param o element to be removed from this list , if present
* @ return < tt > true < / tt > if this list contained the specified element
* /
public boolean remove ( Object o ) {
if ( o = = null ) {
for ( int index = 0 ; index < size; index + + )
if ( elementData[ index ] = = null ) {
|