[Analysis] HikariCP source from FastList see HikariCP Why faster?

  Why look HikariCP from FastList faster?

  HikariCP reason why fast thanks to:

  1, streamline and optimize bytecode

  2, the use of alternative ArrayList FastList

  3, ConcurrentBag: Concurrent collections classes achieve better

  Benpian analyze FastList.

  We start from the referenced, PoolEntry constructor initializes FastList, using new FastList <> (Statement.class, 16);

  PoolEntry(final Connection connection, final PoolBase pool, final boolean isReadOnly, final boolean isAutoCommit)

  {

  this.connection = connection;

  Thiskhikarifol = (ःikrifol) pool;

  this.isReadOnly = isReadOnly;

  this.isAutoCommit = isAutoCommit;

  this.lastAccessed = currentTime();

  this.openStatements = new FastList<>(Statement.class, 16);

  }

  PoolEntry initialization FastList instance, the size is specified as 16.

  We now enter from FastList (Class clazz, int capacity) constructor start.

  /**

  * Construct a FastList with a default size of 32.

  * @param clazz the Class stored in the collection

  */

  @SuppressWarnings("unchecked")

  public FastList(Class clazz)

  {

  this.elementData = (T[]) Array.newInstance(clazz, 32);

  this.clazz = clazz;

  }

  /**

  * Construct a FastList with a specified size.

  * @param clazz the Class stored in the collection

  * @param capacity the initial size of the FastList

  */

  @SuppressWarnings("unchecked")

  public FastList(Class clazz, int capacity)

  {

  this.elementData = (T[]) Array.newInstance(clazz, capacity);

  this.clazz = clazz;

  }

  FastList a linked constructor, the capacity is not specified, the default size of 32.

  We must pay attention to the class comment: Fast list without range checking.FastList no range checking.

  Package Comkscshksherkhikarikutil;

  /**

  * Fast list without range checking.

  *

  * @author Brett Wooldridge

  */

  public final class FastList implements List, RandomAccess, Serializable

  {

  private static final long serialVersionUID = -4598088075242913858L;

  private final Class clazz;

  private T[] elementData;

  private int size;

  Range check? We do not make verbal explanation.

  We analyzed by comparison with ArrayList core Api.

  1)FastList#get

  /**

  * 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];

  }

  Directly back to the specified element. If the index is invalid throws ArrayIndexOutOfBounds.

  ArrayList#get

  /**

  * 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) {

  // Range checking

  rangeCheck (index);

  return elementData(index);

  }

  Find the elements out of the specified range, throws IndexOutOfBoundsException.

  private void rangeCheck(int index) {

  if (index >= size)

  throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

  }

  Obviously ArrayList operations get the time, every time to check again the array subscript, although only just one more operation, but in the connection pool scene, frequently get, remove the operation, there will still be a significant performance cost. The FastList not rangeCheck therefore faster.

  2)FastList#remove

  @Override

  public boolean remove(Object element)

  {

  for (int index = size - 1; index >= 0; index--) {

  if (element == elementData[index]) {

  final int numMoved = size - index - 1;

  if (numMoved > 0) {

  System.arraycopy(elementData, index + 1, elementData, index, numMoved);

  } Zhengzhou gynecological hospital http://www.120zzzy.com/

  elementData[--size] = null;

  return true;

  }

  }

  return false;

  }

  FastList # remove method Similarly, only less rangeCheck, other identical.

  3)FastList#add

  @Override

  public boolean add(T element)

  {

  // container full, direct assignment

  if (size < elementData.length) {

  elementData[size++] = element;

  }

  else {

  // container is full

  // elementData initial capacity is not enough capacity left one needs expansion, equivalent to the capacity of 2 *

  final int oldCapacity = elementData.length;

  final int newCapacity = oldCapacity << 1;

  @SuppressWarnings("unchecked")

  Expansion // -> Create a new array

  final T[] newElementData = (T[]) Array.newInstance(clazz, newCapacity);

  // array copy

  System.arraycopy(elementData, 0, newElementData, 0, oldCapacity);

  // re-assignment, namely the implementation of add

  newElementData[size++] = element;

  elementData = newElementData;

  }

  return true;

  }

  ArrayList # add, upon expansion, the final call

  private void grow(int minCapacity) {

  //overflow-conscious code

  int oldCapacity = elementData.length;

  // expansion is 1.5 times the original

  int newCapacity = oldCapacity + (oldCapacity >> 1);

  if (newCapacity - minCapacity < 0)

  newCapacity = minCapacity;

  if (newCapacity - MAX_ARRAY_SIZE > 0)

  newCapacity = hugeCapacity(minCapacity);

  // minCapacity is usually close to size, so this is a win:

  elementData = Arrays.copyOf(elementData, newCapacity);

  }

  FastList # add method, compared to the original ArrayList expansion of 1.5 times, FastLis direct left one, the original 2-fold expansion. This time the operation itself is less a + operation, better adapted to the application requirements HikariCP.

  【 oldCapacity + (oldCapacity >> 1); 与 oldCapacity << 1; 】。

  Final conclusion: FastList ArrayList compared to just remove the rage inspection, and other details of the expansion, minor adjustments to the pursuit of the ultimate performance.

  ArrayList about why do rangeCheck?

  Like throws an exception because, like FastList, do check, direct manipulation of the underlying array, an exception occurs.

  I understand: different positioning, different pursuit, ArrayList as a generic container, the pursuit of a more secure, stable, operating before rangeCheck checks on the illegal request direct throw, more in line with fail-fast (fast failure) mechanism. The FastList pursuit of the ultimate performance.

Guess you like

Origin www.cnblogs.com/gnz49/p/12068676.html