[space map disk] reduce the lock counts to speed up free block searching (#84)

Avoid repetitively acquiring/releasing the bitmap while finding a free block
This commit is contained in:
Ming-Hung Tsai 2017-09-05 01:05:19 +08:00 committed by Joe Thornber
parent c70aad52e6
commit 591f725232

View File

@ -120,12 +120,7 @@ namespace {
ref_t lookup(unsigned b) const { ref_t lookup(unsigned b) const {
read_ref rr = tm_.read_lock(ie_.blocknr_, validator_); read_ref rr = tm_.read_lock(ie_.blocknr_, validator_);
void const *bits = bitmap_data(rr); return __lookup_raw(bitmap_data(rr), b);
ref_t b1 = test_bit_le(bits, b * 2);
ref_t b2 = test_bit_le(bits, b * 2 + 1);
ref_t result = b2 ? 1 : 0;
result |= b1 ? 2 : 0;
return result;
} }
void insert(unsigned b, ref_t n) { void insert(unsigned b, ref_t n) {
@ -158,8 +153,10 @@ namespace {
} }
boost::optional<unsigned> find_free(unsigned begin, unsigned end) { boost::optional<unsigned> find_free(unsigned begin, unsigned end) {
read_ref rr = tm_.read_lock(ie_.blocknr_, validator_);
void const *bits = bitmap_data(rr);
for (unsigned i = max(begin, ie_.none_free_before_); i < end; i++) for (unsigned i = max(begin, ie_.none_free_before_); i < end; i++)
if (lookup(i) == 0) if (__lookup_raw(bits, i) == 0)
return boost::optional<unsigned>(i); return boost::optional<unsigned>(i);
return boost::optional<unsigned>(); return boost::optional<unsigned>();
@ -193,6 +190,14 @@ namespace {
return h + 1; return h + 1;
} }
ref_t __lookup_raw(void const *bits, unsigned b) const {
ref_t b1 = test_bit_le(bits, b * 2);
ref_t b2 = test_bit_le(bits, b * 2 + 1);
ref_t result = b2 ? 1 : 0;
result |= b1 ? 2 : 0;
return result;
}
transaction_manager &tm_; transaction_manager &tm_;
bcache::validator::ptr validator_; bcache::validator::ptr validator_;