Merge pull request #459 from nitincchauhan/dev

removed check (claimed != null) from hotpath
pull/460/head
Brett Wooldridge 9 years ago
commit d1f26a9346

@ -49,6 +49,7 @@ import com.zaxxer.hikari.util.ConcurrentBag.IBagStateListener;
import com.zaxxer.hikari.util.DefaultThreadFactory; import com.zaxxer.hikari.util.DefaultThreadFactory;
import com.zaxxer.hikari.util.SuspendResumeLock; import com.zaxxer.hikari.util.SuspendResumeLock;
import static com.zaxxer.hikari.pool.PoolEntry.LASTACCESS_COMPARABLE;
import static com.zaxxer.hikari.pool.PoolEntry.MAXED_POOL_MARKER; import static com.zaxxer.hikari.pool.PoolEntry.MAXED_POOL_MARKER;
import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_IN_USE; import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_IN_USE;
import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_NOT_IN_USE; import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_NOT_IN_USE;
@ -431,7 +432,7 @@ public class HikariPool extends PoolBase implements HikariPoolMXBean, IBagStateL
// Speculative increment of totalConnections with expectation of success // Speculative increment of totalConnections with expectation of success
if (totalConnections.incrementAndGet() > config.getMaximumPoolSize()) { if (totalConnections.incrementAndGet() > config.getMaximumPoolSize()) {
totalConnections.decrementAndGet(); // Pool is maxed out, so undo speculative increment of totalConnections totalConnections.decrementAndGet(); // Pool is maxed out, so undo speculative increment of totalConnections
return PoolEntry.MAXED_POOL_MARKER; return MAXED_POOL_MARKER;
} }
try { try {
@ -617,7 +618,7 @@ public class HikariPool extends PoolBase implements HikariPoolMXBean, IBagStateL
int removable = notInUseList.size() - config.getMinimumIdle(); int removable = notInUseList.size() - config.getMinimumIdle();
if (removable > 0) { if (removable > 0) {
// Sort pool entries on lastAccessed // Sort pool entries on lastAccessed
Collections.sort(notInUseList, PoolEntry.LASTACCESS_COMPARABLE); Collections.sort(notInUseList, LASTACCESS_COMPARABLE);
// Iterate the first N removable elements // Iterate the first N removable elements
final Iterator<PoolEntry> iter = notInUseList.iterator(); final Iterator<PoolEntry> iter = notInUseList.iterator();
do { do {

@ -127,9 +127,18 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
public T borrow(long timeout, final TimeUnit timeUnit) throws InterruptedException public T borrow(long timeout, final TimeUnit timeUnit) throws InterruptedException
{ {
// Try the thread-local list first // Try the thread-local list first
T claimed = claimFromThreadLocal(); List<?> list = threadList.get();
if (claimed != null) { if (weakThreadLocals && list == null) {
return claimed; list = new ArrayList<>(16);
threadList.set(list);
}
for (int i = list.size() - 1; i >= 0; i--) {
@SuppressWarnings("unchecked")
final T bagEntry = (T) (weakThreadLocals ? ((WeakReference) list.remove(i)).get() : list.remove(i));
if (bagEntry != null && bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {
return bagEntry;
}
} }
// Otherwise, scan the shared list ... for maximum of timeout // Otherwise, scan the shared list ... for maximum of timeout
@ -355,25 +364,6 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
} }
} }
private T claimFromThreadLocal()
{
List<?> list = threadList.get();
if (weakThreadLocals && list == null) {
list = new ArrayList<>(16);
threadList.set(list);
}
for (int i = list.size() - 1; i >= 0; i--) {
@SuppressWarnings("unchecked")
final T bagEntry = (T) (weakThreadLocals ? ((WeakReference) list.remove(i)).get() : list.remove(i));
if (bagEntry != null && bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {
return bagEntry;
}
}
return null;
}
/** /**
* Determine whether to use WeakReferences based on whether there is a * Determine whether to use WeakReferences based on whether there is a
* custom ClassLoader implementation sitting between this class and the * custom ClassLoader implementation sitting between this class and the

Loading…
Cancel
Save