diff --git a/docs/CHANGELOG.asciidoc b/docs/CHANGELOG.asciidoc index f8df3bcb75..7acf9f98da 100644 --- a/docs/CHANGELOG.asciidoc +++ b/docs/CHANGELOG.asciidoc @@ -34,6 +34,13 @@ * Fix "stack use after scope" memory error. (See {ml-pull}2673[#2673].) +== {es} version 8.14.1 + +=== Enhancements + +* Improve memory allocation management for JSON processing to reduce memory usage. + (See {ml-pull}2679[#2679].) + == {es} version 8.14.0 === Bug Fixes diff --git a/include/api/CJsonOutputWriter.h b/include/api/CJsonOutputWriter.h index d5bae88698..39b9f444e7 100644 --- a/include/api/CJsonOutputWriter.h +++ b/include/api/CJsonOutputWriter.h @@ -273,6 +273,9 @@ class API_EXPORT CJsonOutputWriter { //! \p allocatorName A unique identifier for the allocator void pushAllocator(const std::string& allocatorName); + //! remove allocator from cache + void removeAllocator(const std::string& allocatorName); + //! revert to using the previous allocator for JSON output processing void popAllocator(); diff --git a/include/core/CBoostJsonWriterBase.h b/include/core/CBoostJsonWriterBase.h index 1cd9a76c0e..e0bee383c6 100644 --- a/include/core/CBoostJsonWriterBase.h +++ b/include/core/CBoostJsonWriterBase.h @@ -24,10 +24,7 @@ #include #include -#include -#include #include -#include #include namespace json = boost::json; @@ -134,6 +131,14 @@ class CBoostJsonWriterBase { return this->getAllocator()->get(); } + void removeAllocator(const std::string& allocatorName) { + auto allocator = m_AllocatorCache.find(allocatorName); + if (allocator != m_AllocatorCache.end()) { + allocator->second.reset(); + m_AllocatorCache.erase(allocator); + } + } + bool isComplete() const { bool ret = m_Levels.empty() || m_Levels.top() == 0; return ret; diff --git a/include/core/CScopedBoostJsonPoolAllocator.h b/include/core/CScopedBoostJsonPoolAllocator.h index 6b29f0e4f9..1febdff495 100644 --- a/include/core/CScopedBoostJsonPoolAllocator.h +++ b/include/core/CScopedBoostJsonPoolAllocator.h @@ -12,6 +12,7 @@ #define INCLUDED_ml_core_CScopedBoostJsonPoolAllocator_h #include +#include namespace ml { namespace core { @@ -31,14 +32,18 @@ class CScopedBoostJsonPoolAllocator { //! \p allocatorName Unique identifier for the allocator //! \p jsonOutputWriter JSON output writer that will make use of the allocator CScopedBoostJsonPoolAllocator(const std::string& allocatorName, T& writer) - : m_Writer(writer) { + : m_Writer(writer), m_AllocatorName(allocatorName) { m_Writer.pushAllocator(allocatorName); } - ~CScopedBoostJsonPoolAllocator() { m_Writer.popAllocator(); } + ~CScopedBoostJsonPoolAllocator() { + m_Writer.popAllocator(); + m_Writer.removeAllocator(m_AllocatorName); + } private: T& m_Writer; + std::string m_AllocatorName; }; } } diff --git a/lib/api/CJsonOutputWriter.cc b/lib/api/CJsonOutputWriter.cc index 2a7a987c23..63f8f2bf50 100644 --- a/lib/api/CJsonOutputWriter.cc +++ b/lib/api/CJsonOutputWriter.cc @@ -850,6 +850,10 @@ void CJsonOutputWriter::pushAllocator(const std::string& allocatorName) { m_Writer.pushAllocator(allocatorName); } +void CJsonOutputWriter::removeAllocator(const std::string& allocatorName) { + m_Writer.removeAllocator(allocatorName); +} + void CJsonOutputWriter::popAllocator() { m_Writer.popAllocator(); }