Skip to content

Commit 05c663a

Browse files
committed
Merge pull request mauricio#157 from pitr/patch-1
Switch to LIFO for object pool strategy
2 parents 8692fad + 577b6a8 commit 05c663a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

db-async-common/src/main/scala/com/github/mauricio/async/db/pool/SingleThreadedAsyncObjectPool.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.github.mauricio.async.db.pool
1919
import com.github.mauricio.async.db.util.{Log, Worker}
2020
import java.util.concurrent.atomic.AtomicLong
2121
import java.util.{TimerTask, Timer}
22-
import scala.collection.mutable.ArrayBuffer
22+
import scala.collection.mutable.{ArrayBuffer, Queue, Stack}
2323
import scala.concurrent.{Promise, Future}
2424
import scala.util.{Failure, Success}
2525

@@ -49,9 +49,9 @@ class SingleThreadedAsyncObjectPool[T](
4949
import SingleThreadedAsyncObjectPool.{Counter, log}
5050

5151
private val mainPool = Worker()
52-
private val poolables = new ArrayBuffer[PoolableHolder[T]](configuration.maxObjects)
52+
private var poolables = new Stack[PoolableHolder[T]]()
5353
private val checkouts = new ArrayBuffer[T](configuration.maxObjects)
54-
private val waitQueue = new ArrayBuffer[Promise[T]](configuration.maxQueueSize)
54+
private val waitQueue = new Queue[Promise[T]]()
5555
private val timer = new Timer("async-object-pool-timer-" + Counter.incrementAndGet(), true)
5656
timer.scheduleAtFixedRate(new TimerTask {
5757
def run() {
@@ -150,10 +150,10 @@ class SingleThreadedAsyncObjectPool[T](
150150
*/
151151

152152
private def addBack(item: T, promise: Promise[AsyncObjectPool[T]]) {
153-
this.poolables += new PoolableHolder[T](item)
153+
this.poolables.push(new PoolableHolder[T](item))
154154

155-
if (!this.waitQueue.isEmpty) {
156-
this.checkout(this.waitQueue.remove(0))
155+
if (this.waitQueue.nonEmpty) {
156+
this.checkout(this.waitQueue.dequeue())
157157
}
158158

159159
promise.success(this)
@@ -205,7 +205,7 @@ class SingleThreadedAsyncObjectPool[T](
205205
case e: Exception => promise.failure(e)
206206
}
207207
} else {
208-
val item = this.poolables.remove(0).item
208+
val item = this.poolables.pop().item
209209
this.checkouts += item
210210
promise.success(item)
211211
}
@@ -241,7 +241,7 @@ class SingleThreadedAsyncObjectPool[T](
241241
}
242242
}
243243
}
244-
this.poolables --= removals
244+
this.poolables = this.poolables.diff(removals)
245245
}
246246

247247
private class PoolableHolder[T](val item: T) {

0 commit comments

Comments
 (0)