From 4116fe873eae7b541d485215ce46d9224af49701 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Thu, 3 Sep 2015 20:38:20 +0300 Subject: QRingBuffer: improve indexOf() performance Use memchr() instead of scan cycle. Change-Id: Ic77a3e5ad4c5f6c7d2a1df12d150eac45d620744 Reviewed-by: Thiago Macieira --- src/corelib/tools/qringbuffer.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/corelib/tools/qringbuffer.cpp b/src/corelib/tools/qringbuffer.cpp index e38245ca0a..e9b655c01e 100644 --- a/src/corelib/tools/qringbuffer.cpp +++ b/src/corelib/tools/qringbuffer.cpp @@ -226,16 +226,15 @@ qint64 QRingBuffer::indexOf(char c, qint64 maxLength, qint64 pos) const index = 0; } - do { - if (*ptr++ == c) - return index + pos; - } while (++index < nextBlockIndex); + const char *findPtr = reinterpret_cast(memchr(ptr, c, + nextBlockIndex - index)); + if (findPtr) + return qint64(findPtr - ptr) + index + pos; - if (index == maxLength) + if (nextBlockIndex == maxLength) return -1; - } else { - index = nextBlockIndex; } + index = nextBlockIndex; } return -1; } -- cgit v1.2.3