From 507774a9020f0a1e9ecc47569e5f84cae46bf600 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Piotr=20Da=C5=82ek?= Date: Mon, 18 Jan 2016 13:27:35 +0100 Subject: [PATCH] common/buffer: replace RWLock with spinlocks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This decreases buffer::raw size by about 100 bytes (since RWLock occupies 104 bytes and simple_spinlock_t just 4) and also significantly decreases time wasted by locking and unlocking, possibly reducing context switches too. Particularly visible when using small blocksizes. Signed-off-by: Piotr Dałek --- src/common/buffer.cc | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/common/buffer.cc b/src/common/buffer.cc index e2de936d8c4..864c7eefb9e 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -162,16 +162,16 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; unsigned len; atomic_t nref; - mutable RWLock crc_lock; + mutable simple_spinlock_t crc_spinlock; map, pair > crc_map; raw(unsigned l) : data(NULL), len(l), nref(0), - crc_lock("buffer::raw::crc_lock", false) + crc_spinlock(SIMPLE_SPINLOCK_INITIALIZER) { } raw(char *c, unsigned l) : data(c), len(l), nref(0), - crc_lock("buffer::raw::crc_lock", false) + crc_spinlock(SIMPLE_SPINLOCK_INITIALIZER) { } virtual ~raw() {} @@ -208,32 +208,29 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; } bool get_crc(const pair &fromto, pair *crc) const { - crc_lock.get_read(); + simple_spin_lock(&crc_spinlock); map, pair >::const_iterator i = crc_map.find(fromto); if (i == crc_map.end()) { - crc_lock.unlock(); + simple_spin_unlock(&crc_spinlock); return false; } *crc = i->second; - crc_lock.unlock(); + simple_spin_unlock(&crc_spinlock); return true; } void set_crc(const pair &fromto, const pair &crc) { - crc_lock.get_write(); + simple_spin_lock(&crc_spinlock); crc_map[fromto] = crc; - crc_lock.unlock(); + simple_spin_unlock(&crc_spinlock); } void invalidate_crc() { - // don't own the write lock when map is empty - crc_lock.get_read(); + simple_spin_lock(&crc_spinlock); if (crc_map.size() != 0) { - crc_lock.unlock(); - crc_lock.get_write(); crc_map.clear(); } - crc_lock.unlock(); + simple_spin_unlock(&crc_spinlock); } }; -- 2.47.3