From 9b5a1a4a326c243ff12571ad0537778bf7193d6a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Piotr=20Da=C5=82ek?= Date: Fri, 26 May 2017 11:39:58 +0200 Subject: [PATCH] inline_memory: optimized mem_is_zero for non-X64 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit mem_is_zero is fast for X64 where 128-bit registers are available, but it's very easy to optimze it for 32-bit Intel and ARM CPUs as well, the speed won't be anywhere near the fastest one but still almost 7x faster than regular byte-by-byte check. Now with extra test to check for corner cases that may pop with such implementations. Signed-off-by: Piotr Dałek --- src/include/inline_memory.h | 9 +++++++++ src/test/bufferlist.cc | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/include/inline_memory.h b/src/include/inline_memory.h index f2166826b76d..f2433f03edf7 100644 --- a/src/include/inline_memory.h +++ b/src/include/inline_memory.h @@ -124,6 +124,15 @@ bool mem_is_zero(const char *data, size_t len) static inline bool mem_is_zero(const char *data, size_t len) { const char *end = data + len; + const char* end64 = data + (len / sizeof(uint64_t))*sizeof(uint64_t); + + while (data < end64) { + if (*(uint64_t*)data != 0) { + return false; + } + data += sizeof(uint64_t); + } + while (data < end) { if (*data != 0) { return false; diff --git a/src/test/bufferlist.cc b/src/test/bufferlist.cc index 907a194229e4..b2d408e26450 100644 --- a/src/test/bufferlist.cc +++ b/src/test/bufferlist.cc @@ -1764,6 +1764,17 @@ TEST(BufferList, is_zero) { bl.append_zero(1); EXPECT_TRUE(bl.is_zero()); } + + for (size_t i = 1; i <= 256; ++i) { + bufferlist bl; + bl.append_zero(i); + EXPECT_TRUE(bl.is_zero()); + bl.append('A'); + // ensure buffer is a single, contiguous before testing + bl.rebuild(); + EXPECT_FALSE(bl.is_zero()); + } + } TEST(BufferList, clear) { -- 2.47.3