diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-05-26 09:27:37 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-05-26 09:27:37 +0000 |
commit | 3cc0f5ea5d11cc6d0ad9f5b67cc00fb81d54b030 (patch) | |
tree | 9a7cc7bf2937ebacf6a56b24629b75f39f193b4f /searchlib | |
parent | dc6e85cc65b2d7e261e38326046f390c9339d928 (diff) |
The AttributeContext is a short lived cache for attributes guards. Until we use the thread bundle
we do not need to use expensive locking to fill the cache.
Most of the attributes are pulled in when building the blueprint tree, and that always happens singlethreaded.
Diffstat (limited to 'searchlib')
3 files changed, 27 insertions, 7 deletions
diff --git a/searchlib/src/vespa/searchcommon/attribute/iattributecontext.h b/searchlib/src/vespa/searchcommon/attribute/iattributecontext.h index 9c89b6a0f8b..cf7b1d2f959 100644 --- a/searchlib/src/vespa/searchcommon/attribute/iattributecontext.h +++ b/searchlib/src/vespa/searchcommon/attribute/iattributecontext.h @@ -46,6 +46,11 @@ public: virtual void releaseEnumGuards() {} /** + * Must be called before multiple threads will access the context. + */ + virtual void enableMultiThreadSafe() {} + + /** * Virtual destructor to allow safe subclassing. **/ virtual ~IAttributeContext() = default; diff --git a/searchlib/src/vespa/searchlib/attribute/attributecontext.cpp b/searchlib/src/vespa/searchlib/attribute/attributecontext.cpp index 3c85c076eb2..97a7dc8bcb1 100644 --- a/searchlib/src/vespa/searchlib/attribute/attributecontext.cpp +++ b/searchlib/src/vespa/searchlib/attribute/attributecontext.cpp @@ -30,8 +30,15 @@ AttributeContext::getAttribute(AttributeMap & map, const string & name, bool sta } } +const IAttributeVector * +AttributeContext::getAttributeMtSafe(AttributeMap &map, const string &name, bool stableEnum) const { + std::lock_guard<std::mutex> guard(_cacheLock); + return getAttribute(map, name, stableEnum); +} + AttributeContext::AttributeContext(const IAttributeManager & manager) : _manager(manager), + _mtSafe(false), _attributes(), _enumAttributes(), _cacheLock() @@ -42,20 +49,26 @@ AttributeContext::~AttributeContext() = default; const IAttributeVector * AttributeContext::getAttribute(const string & name) const { - std::lock_guard<std::mutex> guard(_cacheLock); - return getAttribute(_attributes, name, false); + return _mtSafe + ? getAttributeMtSafe(_attributes, name, false) + : getAttribute(_attributes, name, false); } const IAttributeVector * AttributeContext::getAttributeStableEnum(const string & name) const { - std::lock_guard<std::mutex> guard(_cacheLock); - return getAttribute(_enumAttributes, name, true); + return _mtSafe + ? getAttributeMtSafe(_enumAttributes, name, true) + : getAttribute(_enumAttributes, name, true); } void AttributeContext::releaseEnumGuards() { - std::lock_guard<std::mutex> guard(_cacheLock); - _enumAttributes.clear(); + if (_mtSafe) { + std::lock_guard<std::mutex> guard(_cacheLock); + _enumAttributes.clear(); + } else { + _enumAttributes.clear(); + } } void diff --git a/searchlib/src/vespa/searchlib/attribute/attributecontext.h b/searchlib/src/vespa/searchlib/attribute/attributecontext.h index 8b8cf5daead..28b05a76f65 100644 --- a/searchlib/src/vespa/searchlib/attribute/attributecontext.h +++ b/searchlib/src/vespa/searchlib/attribute/attributecontext.h @@ -21,12 +21,13 @@ private: using IAttributeFunctor = attribute::IAttributeFunctor; const IAttributeManager & _manager; + bool _mtSafe; mutable AttributeMap _attributes; mutable AttributeMap _enumAttributes; mutable std::mutex _cacheLock; const IAttributeVector *getAttribute(AttributeMap & map, const string & name, bool stableEnum) const; - + const IAttributeVector *getAttributeMtSafe(AttributeMap & map, const string & name, bool stableEnum) const; public: AttributeContext(const IAttributeManager & manager); ~AttributeContext() override; @@ -37,6 +38,7 @@ public: const attribute::IAttributeVector * getAttributeStableEnum(const string & name) const override; void getAttributeList(std::vector<const IAttributeVector *> & list) const override; void releaseEnumGuards() override; + void enableMultiThreadSafe() override { _mtSafe = true; } // Give acces to the underlying manager const IAttributeManager & getManager() const { return _manager; } |