include/tools/json_writer.hxx | 6 +++--- tools/source/misc/json_writer.cxx | 21 ++++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-)
New commits: commit 06daf1719ef6dfb5b01d7ff09ce6df4c5c332cd5 Author: Noel Grandin <noelgran...@gmail.com> AuthorDate: Sat Mar 2 16:47:10 2024 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Sat Mar 2 20:11:07 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 <noel.gran...@collabora.co.uk> diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx index 9efa358a5e60..eca19a6e736a 100644 --- a/include/tools/json_writer.hxx +++ b/include/tools/json_writer.hxx @@ -33,7 +33,7 @@ class TOOLS_DLLPUBLIC JsonWriter ~ScopedJsonWriterNode() { mrWriter.endNode(closing); } }; - char* mpBuffer; + rtl_String* mpBuffer; char* mPos; int mSpaceAllocated; int mStartNodeCount; @@ -96,14 +96,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 86021dfcc5ca..0ffc5cb903f9 100644 --- a/tools/source/misc/json_writer.cxx +++ b/tools/source/misc/json_writer.cxx @@ -20,8 +20,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) @@ -38,7 +38,7 @@ JsonWriter::JsonWriter() JsonWriter::~JsonWriter() { assert(mbClosed && "forgot to extract data?"); - free(mpBuffer); + rtl_string_release(mpBuffer); } JsonWriter::ScopedJsonWriterNode<'}'> JsonWriter::startNode(std::string_view pNodeName) @@ -291,12 +291,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(); @@ -339,13 +342,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