速度打败Druid的王者---HikariCP连接池

  • HikariCP是什么

    HikariCP是数据库连接池,而且是号称史上最快的,而且目前来看确实是这样的,SpringBoot2.0也已经采用HikariCP作为默认连接池配置.

    官网性能柱状图

    HikariCP GitHub网址

  • HikariCP为什么这么快

    1.1 代码量非常小:
    要知道对于连接池来讲,代码越少,占用cpu和内存越少,Bug出现几率也是越小,那么代表他的执行率高,这就是为什么HikariCP受欢迎的原因之一
    1.2 稳定性,可靠性强:
    HikariCP是经受了市场的考验,走过太上老君的丹炉,现如今拿到SpringBoot的入场证,走上了人生巅峰.
    1.3 速度奇快:
    光有健壮性可不行,坦克很健壮,却跑不过飞机,但是HikariCP却依靠自己的代码少,重写数据结构等特点,成功晋级速度最快连接池冠军宝座.

  • HikariCP为什么这么快呢

HikariCP快得益于:
1.1 优化并精简字节码
1.2 使用FastList替代ArrayList
1.3 ConcurrentBag:更好的并发集合类实现

下面,我带大家看一下这么牛的连接池的FastList源码

/**
* 没有列表检查的 FastList。
*
* @author Brett Wooldridge
*/
public final class FastList<T> implements List<T>, RandomAccess, Serializable
{
  private static final long serialVersionUID = -4598088075242913858L;

  private final Class<?> clazz;
  private T[] elementData;
  private int size;

  /**
   * 构建一个默认大小为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;
  }

  /**
   * 构造具有指定大小的列表。
   * @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的构造,可以看到它初始化了32长度的数组

 @Override
   public boolean add(T element)
   {
      //给 list添加属性
      //如果 size值小于 初始化的值
      if (size < elementData.length) {
         elementData[size++] = element;
      }
      else {
         // 溢出的代码
         //elementData 原始32不够用 需要扩容
         final int oldCapacity = elementData.length;
         final int newCapacity = oldCapacity << 1;
         @SuppressWarnings("unchecked")
         //扩容集合
         final T[] newElementData = (T[]) Array.newInstance(clazz, newCapacity);
         //数组复制
         System.arraycopy(elementData, 0, newElementData, 0, oldCapacity);
         //属性赋值
         newElementData[size++] = element;
         elementData = newElementData;
      }

      return true;
   }

这一段是进行添加元素,没有什么特别的,但是请看下面的get和remove方法

 /*
    * 我复制ArrayList的get代码 来看看为什么 FastList 更快
    *  public E get(int index) {
    *   rangeCheck(index);
    *    return elementData(index);
    *   }
    *   多出了rangeCheck 检查角标范围 节省时间
    */
   @Override
   public T get(int index)
   {
      return elementData[index];
   }

很明显ArrayList操作get的时候,每次都要去检查一遍数组角标,而FastList却不需要去检查,进而速度更快.

 /*
    * 这个是ArrayList的 remove()代码 FastList 少了检查范围 和 从头到尾的 检查元素动作 性能更快
    *   rangeCheck(index);
    *   modCount++;
    *    E oldValue = elementData(index);
    */
   @Override
   public boolean remove(Object element)
   {
      for (int index = size - 1; index >= 0; index--) {
         if (element == elementData[index]) {
            final int numMoved = size - index - 1;
            //如果角标不是最后一个 copy一个新的数组结构
            if (numMoved > 0) {
               System.arraycopy(elementData, index + 1, elementData, index, numMoved);
            }
            //如果角标是最后面的 直接初始化为null
            elementData[--size] = null;
            return true;
         }
      }

      return false;
   }

这个是remove方法,一样的ArrayList不但先进行检查角标范围,而且还从头到脚进行扫描元素,FastList摒弃了这个做法去追求性能上的极致.

HikariCP在优化并精简字节码上也下了功夫,使用第三方的Java字节码修改类库Javassist来生成委托实现动态代理.动态代理的实现在ProxyFactory类,速度更快,相比于JDK Proxy生成的字节码更少,精简了很多不必要的字节码.

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot是一个用于简化Spring应用开发的框架,而Druid是一个高效的数据库连接池。在Spring Boot项目中使用Druid连接池可以提供高性能的数据库连接管理和监控功能。 要在Spring Boot中使用Druid连接池,需要以下步骤: 1. 在项目的pom.xml文件中添加Druid依赖: ```xml <dependencies> <!-- Spring Boot Starter JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Druid依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> </dependencies> ``` 2. 在application.properties或application.yml文件中配置Druid连接池相关属性,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # Druid连接池配置 spring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-active=20 spring.datasource.druid.max-wait=60000 spring.datasource.druid.time-between-eviction-runs-millis=60000 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 1 spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.filters=stat spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.use-global-data-source-stat=true ``` 3. 在启动类上添加`@EnableTransactionManagement`和`@MapperScan`注解,例如: ```java @SpringBootApplication @EnableTransactionManagement @MapperScan("com.example.mapper") public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 以上是在Spring Boot项目中使用Druid连接池的基本配置步骤,你可以根据自己的需求修改配置参数来满足具体业务场景。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值