diff options
author | Tor Egge <Tor.Egge@online.no> | 2024-06-07 23:14:34 +0200 |
---|---|---|
committer | Tor Egge <Tor.Egge@online.no> | 2024-06-07 23:14:34 +0200 |
commit | 38a4da34ead02cde0ccdbb1d4fa88acce06f042f (patch) | |
tree | 6b7af6e01b6f710452741b7881ccfa9bf7e3e9b2 | |
parent | 1478e54ecaffe8fffcbfdaf7fc853fb1578a36c5 (diff) |
Move LimitedThreadBundleWrapper to vespalib.
4 files changed, 58 insertions, 17 deletions
diff --git a/searchcore/src/vespa/searchcore/proton/matching/matcher.cpp b/searchcore/src/vespa/searchcore/proton/matching/matcher.cpp index e0897e0378d..a5ace1676ef 100644 --- a/searchcore/src/vespa/searchcore/proton/matching/matcher.cpp +++ b/searchcore/src/vespa/searchcore/proton/matching/matcher.cpp @@ -18,6 +18,7 @@ #include <vespa/searchlib/fef/test/plugin/setup.h> #include <vespa/searchlib/common/allocatedbitvector.h> #include <vespa/vespalib/data/slime/inserter.h> +#include <vespa/vespalib/util/limited_thread_bundle_wrapper.h> #include <cinttypes> #include <vespa/log/log.h> @@ -77,22 +78,6 @@ numThreads(size_t hits, size_t minHits) { return static_cast<size_t>(std::ceil(double(hits) / double(minHits))); } -class LimitedThreadBundleWrapper final : public vespalib::ThreadBundle -{ -public: - LimitedThreadBundleWrapper(vespalib::ThreadBundle &threadBundle, uint32_t maxThreads) - : _threadBundle(threadBundle), - _maxThreads(std::min(maxThreads, static_cast<uint32_t>(threadBundle.size()))) - { } - size_t size() const override { return _maxThreads; } - void run(vespalib::Runnable* const* targets, size_t cnt) override { - _threadBundle.run(targets, cnt); - } -private: - vespalib::ThreadBundle &_threadBundle; - const uint32_t _maxThreads; -}; - bool willNeedRanking(const SearchRequest & request, const GroupingContext & groupingContext, std::optional<search::feature_t> first_phase_rank_score_drop_limit) @@ -302,7 +287,7 @@ Matcher::match(const SearchRequest &request, vespalib::ThreadBundle &threadBundl request.sortSpec, params.offset, params.hits); size_t numThreadsPerSearch = computeNumThreadsPerSearch(mtf->estimate(), rankProperties); - LimitedThreadBundleWrapper limitedThreadBundle(threadBundle, numThreadsPerSearch); + vespalib::LimitedThreadBundleWrapper limitedThreadBundle(threadBundle, numThreadsPerSearch); MatchMaster master; uint32_t numParts = NumSearchPartitions::lookup(rankProperties, _rankSetup->getNumSearchPartitions()); if (limitedThreadBundle.size() > 1) { diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt index 79000a89f5f..406ea68a08a 100644 --- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -49,6 +49,7 @@ vespa_add_library(vespalib_vespalib_util OBJECT jsonwriter.cpp latch.cpp left_right_heap.cpp + limited_thread_bundle_wrapper.cpp lz4compressor.cpp malloc_mmap_guard.cpp md5.c diff --git a/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.cpp b/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.cpp new file mode 100644 index 00000000000..15a5bf101f0 --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.cpp @@ -0,0 +1,31 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "limited_thread_bundle_wrapper.h" +#include "exceptions.h" + +namespace vespalib { + +LimitedThreadBundleWrapper::LimitedThreadBundleWrapper(ThreadBundle& thread_bundle, uint32_t max_threads) + : _thread_bundle(thread_bundle), + _max_threads(std::min(max_threads, static_cast<uint32_t>(thread_bundle.size()))) +{ +} + +LimitedThreadBundleWrapper::~LimitedThreadBundleWrapper() = default; + +size_t +LimitedThreadBundleWrapper::size() const +{ + return _max_threads; +} + +void +LimitedThreadBundleWrapper::run(Runnable* const* targets, size_t cnt) +{ + if (cnt > size()) { + throw IllegalArgumentException("too many targets"); + } + _thread_bundle.run(targets, cnt); +} + +} diff --git a/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.h b/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.h new file mode 100644 index 00000000000..de3c58bf74d --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/limited_thread_bundle_wrapper.h @@ -0,0 +1,24 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "thread_bundle.h" + +namespace vespalib { + +/* + * A ThreadBundle implementation that limits the number of available threads + * from the backing thread bundle. + */ +class LimitedThreadBundleWrapper : public ThreadBundle +{ + ThreadBundle& _thread_bundle; + const uint32_t _max_threads; +public: + LimitedThreadBundleWrapper(ThreadBundle& thread_bundle, uint32_t max_threadss); + ~LimitedThreadBundleWrapper() override; + size_t size() const override; + void run(Runnable* const* targets, size_t cnt) override; +}; + +} |