Origins and Motivation
Existing database connection pools suffered from persistent deadlocks, overly complex locking mechanisms, and violations of JDBC contracts. A common JDBC contract violation involved returning connections to the pool without resetting state variables like auto-commit settings or transaction isolation levels, leaving subsequent consumers with dirty connections. Frustrated by these reliability issues and the bloated codebases of existing solutions, Brett Wooldridge created HikariCP.
The Meaning of Hikari
Translated from Japanese, Hikari means light. This name reflects the dual focus of the project: extreme speed and a minimal, lightweight codebase.
Bytecode-Level Optimizations
JIT Inlining Facilitation
By examining compiled bytecode and JIT assembly output, HikariCP ensures that critical execution paths remain smaller than the JIT compiler inline threshold. Method inlining eliminates invocation overhead, such as parameter passing, stack frame creation, and method dispatch. Consider the following method:
int outcome = calculateSum(val1, val2);
private int calculateSum(int num1, int num2) {
return num1 + num2;
}After JIT inlining, the compiler transforms this into:
int outcome = val1 + val2;While individual micro-optimizations are nearly imperceptible, their cumulative effect across millions of invocations yields measurable millisecond-level improvements.
Invokestatic vs Invokevirtual
A significant bytecode optimization involved changing how proxy instances are obtained. Originally, proxy retrieval utilized a singleton factory pattern, requiring an invokevirtual instruction. The bytecode executed a getstatic call to fetch the factory instance, followed by an invokevirtual call to retrieve the proxy.
The implementation was refactored to use Javassist-generated static methods. This substitution resulted in three major improvements:
- The getstatic instruction was eliminated entirely.
- The invokevirtual instruction was replaced with invokestatic, which is significantly easier for the JVM to optimize.
- The operand stack size decreased from 5 to 4, as the implicit this reference no longer requires pushing and popping.
The performance advantage of invokestatic stems from how the JVM handles method dispatch. Virtual methods require the JVM to consult the Class Hierarchy Analysis (CHA) and install traps or guard conditions to handle potential polymorphic calls. Static methods, being non-virtual, are resolved during class loading and can be inlined with absolute safety, bypassing the overhead of CHA trap setup and teardown.
Code-Level Optimizations
FastList Replacing ArrayList
Standard Java ArrayList implementations perform a range check on every get(int index) call. Because HikariCP guarantees index validity, FastList removes this redundant check. Furthermore, ArrayList.remove(Object) iterates from the beginning of the array. Since JDBC statements are typically closed in the reverse order of their creation, FastList optimizes the remove method by iterating from the end of the array, drastically reducing search time.
ConcurrentBag Design
HikariCP introduces a custom ConcurrentBag designed to minimize shared resource contention. It employs a lock-free architecture, thread-local caches for connection pre-allocation, and steal queues. By utilizing ThreadLocal for pre-allocation, threads frequently avoid shared resource locks entirely during borrow and requite operations.
Design Philosophy Comparison: HikariCP vs. Druid
A historical discussion between the creators of HikariCP and Alibaba Druid highlighted differing design philosophies. Druid prioritizes extensive monitoring, SQL parsing, and data access enhancement, often utilizing fair locks to prevent starvation, which incurs a performance cost. HikariCP focuses strictly on maximizing connection pooling throughput and provides pool-level metrics rather than query-level instrumentation. While Druid dominates in specific regional ecosystems and massive domestic e-commerce events, HikariCP serves as the default connection pool in Spring Boot and handles billions of daily requests globally.