Skip to content

Commit

Permalink
refactor: reduced atomics usage in Pool
Browse files Browse the repository at this point in the history
There is no reason to `min` and `max` properties of Pool to be an atomic
type, because they can't be updated since Pool's creation. So we can use
regular `usize` instead which will be more performant.
  • Loading branch information
amv-dev committed Apr 3, 2021
1 parent 3ca713e commit 6f5e46c
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/conn/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl InnerPool {

struct ArcedPool {
inner: (Mutex<InnerPool>, Condvar),
min: AtomicUsize,
max: AtomicUsize,
min: usize,
max: usize,
count: AtomicUsize,
}

Expand Down Expand Up @@ -150,7 +150,7 @@ impl Pool {
if let Some(conn) = pool.pool.pop_front() {
drop(pool);
break conn;
} else if self.arced_pool.count.load(Ordering::Relaxed) < self.arced_pool.max.load(Ordering::Relaxed) {
} else if self.arced_pool.count.load(Ordering::Relaxed) < self.arced_pool.max {
pool.new_conn()?;
self.arced_pool.count.fetch_add(1, Ordering::SeqCst);
} else {
Expand Down Expand Up @@ -190,8 +190,8 @@ impl Pool {
Ok(Pool {
arced_pool: Arc::new(ArcedPool {
inner: (Mutex::new(pool), Condvar::new()),
min: AtomicUsize::new(min),
max: AtomicUsize::new(max),
min,
max,
count: AtomicUsize::new(min),
}),
use_cache: true,
Expand Down Expand Up @@ -249,8 +249,8 @@ impl fmt::Debug for Pool {
write!(
f,
"Pool {{ min: {}, max: {}, count: {} }}",
self.arced_pool.min.load(Ordering::Relaxed),
self.arced_pool.max.load(Ordering::Relaxed),
self.arced_pool.min,
self.arced_pool.max,
self.arced_pool.count.load(Ordering::Relaxed)
)
}
Expand Down Expand Up @@ -304,7 +304,7 @@ impl Deref for PooledConn {

impl Drop for PooledConn {
fn drop(&mut self) {
if self.pool.arced_pool.count.load(Ordering::Relaxed) > self.pool.arced_pool.max.load(Ordering::Relaxed)
if self.pool.arced_pool.count.load(Ordering::Relaxed) > self.pool.arced_pool.max
|| self.conn.is_none()
{
self.pool.arced_pool.count.fetch_sub(1, Ordering::SeqCst);
Expand Down

0 comments on commit 6f5e46c

Please sign in to comment.