From fdeb80f970364d24c8180751937c62d5b830fd8b Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 11 Mar 2016 16:51:35 -0800 Subject: [PATCH] Speed up IndexSet::count() a bit --- src/index_set.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/index_set.cpp b/src/index_set.cpp index e79d7a14..23930f77 100644 --- a/src/index_set.cpp +++ b/src/index_set.cpp @@ -39,11 +39,20 @@ bool IndexSet::contains(size_t index) const size_t IndexSet::count(size_t start_index, size_t end_index) const { auto it = const_cast(this)->find(start_index); - size_t ret = 0; - for (; end_index > start_index && it != m_ranges.end() && it->first < end_index; ++it) { - ret += std::min(it->second, end_index) - std::max(it->first, start_index); - start_index = it->second; + const auto end = m_ranges.end(); + if (it == end || it->first >= end_index) { + return 0; } + if (it->second >= end_index) + return std::min(it->second, end_index) - std::max(it->first, start_index); + + // These checks are somewhat redundant, but this loop is hot so pulling instructions out of it helps + size_t ret = it->second - std::max(it->first, start_index); + for (++it; it != end && it->second < end_index; ++it) { + ret += it->second - it->first; + } + if (it != end && it->first < end_index) + ret += end_index - it->first; return ret; }