include/tools/json_writer.hxx | 6 +++--- tools/source/misc/json_writer.cxx | 21 ++++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-)
New commits: commit fc7f7226d0f623a087642a915abf1ee8acbde7c9 Author: Noel Grandin <[email protected]> AuthorDate: Sat Mar 2 16:47:10 2024 +0200 Commit: Noel Grandin <[email protected]> CommitDate: Sun Mar 3 13:54:22 2024 +0100 avoid a memcpy when constructing output of tools::JsonWriter we can store the data in an rtl::OString object and then return that directly Change-Id: I65af6820bfd3135b38d437cf9736fffff8924e88 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164291 Tested-by: Jenkins Reviewed-by: Noel Grandin <[email protected]> (cherry picked from commit b1c081196979188bd5502778617a2fa694830bff) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164271 Reviewed-by: Caolán McNamara <[email protected]> Tested-by: Jenkins CollaboraOffice <[email protected]> diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx index c5faf542f1d8..8bb8d192d2ff 100644 --- a/include/tools/json_writer.hxx +++ b/include/tools/json_writer.hxx @@ -34,7 +34,7 @@ class TOOLS_DLLPUBLIC JsonWriter friend class ScopedJsonWriterArray; friend class ScopedJsonWriterStruct; - char* mpBuffer; + rtl_String* mpBuffer; char* mPos; int mSpaceAllocated; int mStartNodeCount; @@ -99,14 +99,14 @@ private: inline void addValidationMark() { #ifndef NDEBUG - *(mpBuffer + mSpaceAllocated - 1) = JSON_WRITER_DEBUG_MARKER; + *(mpBuffer->buffer + mSpaceAllocated - 1) = JSON_WRITER_DEBUG_MARKER; #endif } inline void validate() { #ifndef NDEBUG - unsigned char c = *(mpBuffer + mSpaceAllocated - 1); + unsigned char c = *(mpBuffer->buffer + mSpaceAllocated - 1); assert(c == JSON_WRITER_DEBUG_MARKER); #endif } diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx index e3e27bf756b4..e091ed8b8b76 100644 --- a/tools/source/misc/json_writer.cxx +++ b/tools/source/misc/json_writer.cxx @@ -19,8 +19,8 @@ namespace tools constexpr int DEFAULT_BUFFER_SIZE = 2048; JsonWriter::JsonWriter() - : mpBuffer(static_cast<char*>(malloc(DEFAULT_BUFFER_SIZE))) - , mPos(mpBuffer) + : mpBuffer(rtl_string_alloc(DEFAULT_BUFFER_SIZE)) + , mPos(mpBuffer->buffer) , mSpaceAllocated(DEFAULT_BUFFER_SIZE) , mStartNodeCount(0) , mbFirstFieldInNode(true) @@ -37,7 +37,7 @@ JsonWriter::JsonWriter() JsonWriter::~JsonWriter() { assert(mbClosed && "forgot to extract data?"); - free(mpBuffer); + rtl_string_release(mpBuffer); } ScopedJsonWriterNode JsonWriter::startNode(std::string_view pNodeName) @@ -344,12 +344,15 @@ void JsonWriter::addCommaBeforeField() void JsonWriter::ensureSpace(int noMoreBytesRequired) { assert(!mbClosed && "already extracted data"); - int currentUsed = mPos - mpBuffer; + int currentUsed = mPos - mpBuffer->buffer; if (currentUsed + noMoreBytesRequired >= mSpaceAllocated) { auto newSize = (currentUsed + noMoreBytesRequired) * 2; - mpBuffer = static_cast<char*>(realloc(mpBuffer, newSize)); - mPos = mpBuffer + currentUsed; + rtl_String* pNewBuffer = rtl_string_alloc(newSize); + memcpy(pNewBuffer->buffer, mpBuffer->buffer, currentUsed); + rtl_string_release(mpBuffer); + mpBuffer = pNewBuffer; + mPos = mpBuffer->buffer + currentUsed; mSpaceAllocated = newSize; addValidationMark(); @@ -392,13 +395,13 @@ OString JsonWriter::finishAndGetAsOString() *mPos = 0; mbClosed = true; - OString ret(mpBuffer, mPos - mpBuffer); - return ret; + mpBuffer->length = mPos - mpBuffer->buffer; + return mpBuffer; } bool JsonWriter::isDataEquals(std::string_view s) const { - return std::string_view(mpBuffer, static_cast<size_t>(mPos - mpBuffer)) == s; + return std::string_view(mpBuffer->buffer, static_cast<size_t>(mPos - mpBuffer->buffer)) == s; } } // namespace tools
