diff --git a/docs/CHANGELOG.asciidoc b/docs/CHANGELOG.asciidoc index 0522063150..3bf79e178c 100644 --- a/docs/CHANGELOG.asciidoc +++ b/docs/CHANGELOG.asciidoc @@ -28,6 +28,13 @@ //=== Regressions +== {es} version 7.17.23 + +=== Enhancements + +* Improve memory allocation management for JSON processing to reduce memory usage. + (See {ml-pull}#2678[2678].) + == {es} version 7.17.16 === Enhancements diff --git a/include/api/CJsonOutputWriter.h b/include/api/CJsonOutputWriter.h index eec1cd0370..ac25665d9b 100644 --- a/include/api/CJsonOutputWriter.h +++ b/include/api/CJsonOutputWriter.h @@ -266,6 +266,9 @@ class API_EXPORT CJsonOutputWriter { //! \p allocatorName A unique identifier for the allocator void pushAllocator(const std::string& allocatorName); + //! remove the allocator + void removeAllocator(const std::string& allocatorName); + //! revert to using the previous allocator for JSON output processing void popAllocator(); diff --git a/include/core/CRapidJsonWriterBase.h b/include/core/CRapidJsonWriterBase.h index 3a44e0438b..a43289cbed 100644 --- a/include/core/CRapidJsonWriterBase.h +++ b/include/core/CRapidJsonWriterBase.h @@ -162,6 +162,15 @@ class CRapidJsonWriterBase return allocator; } + //! remove an allocator from the cache + void removeAllocator(const std::string& allocatorName) { + auto allocator = m_AllocatorCache.find(allocatorName); + if (allocator != m_AllocatorCache.end()) { + allocator->second.reset(); + m_AllocatorCache.erase(allocator); + } + } + rapidjson::MemoryPoolAllocator<>& getRawAllocator() const { return this->getAllocator()->get(); } diff --git a/include/core/CScopedRapidJsonPoolAllocator.h b/include/core/CScopedRapidJsonPoolAllocator.h index 9eb671fe3b..f32634567e 100644 --- a/include/core/CScopedRapidJsonPoolAllocator.h +++ b/include/core/CScopedRapidJsonPoolAllocator.h @@ -32,14 +32,18 @@ class CScopedRapidJsonPoolAllocator { //! \p allocatorName Unique identifier for the allocator //! \p jsonOutputWriter JSON output writer that will make use of the allocator CScopedRapidJsonPoolAllocator(const std::string& allocatorName, T& writer) - : m_Writer(writer) { + : m_Writer(writer), m_AllocatorName(allocatorName) { m_Writer.pushAllocator(allocatorName); } - ~CScopedRapidJsonPoolAllocator() { m_Writer.popAllocator(); } + ~CScopedRapidJsonPoolAllocator() { + 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 79de03e44d..8bb3d59c9b 100644 --- a/lib/api/CJsonOutputWriter.cc +++ b/lib/api/CJsonOutputWriter.cc @@ -825,6 +825,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(); }