include/tools/json_writer.hxx     |   15 ++++-----------
 tools/source/misc/json_writer.cxx |   29 ++++++++++++++++-------------
 2 files changed, 20 insertions(+), 24 deletions(-)

New commits:
commit 02d225f2a46b3d531e187976d8f2c39ed390899c
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Thu Oct 14 00:15:11 2021 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu Oct 14 07:40:52 2021 +0200

    Simplify JsonWriter a bit
    
    Move ensureSpace code to json_writer.cxx, and merge with reallocBuffer.
    The methods are private, and are only used in methods in the same .CXX,
    so the code will get inlined as compiler decides anyway, but becomes
    simpler.
    
    Make extractDataAs* to consider the known size of the data, to avoid
    calculating null-terminated size. To do that, the code is moved from
    extractData to private extractDataImpl, which returns both pointer
    and size.
    
    Change-Id: I7c0e425b5c584089c6e866c31d4cfdb5e242d66b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123568
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx
index 45df53c61c5f..72ed59edadc5 100644
--- a/include/tools/json_writer.hxx
+++ b/include/tools/json_writer.hxx
@@ -17,6 +17,7 @@
 
 #include <string>
 #include <string_view>
+#include <utility>
 
 /** Simple JSON encoder designed specifically for LibreOfficeKit purposes.
  *
@@ -73,7 +74,7 @@ public:
 
     /** Hands ownership of the underlying storage buffer to the caller,
      * after this no more document modifications may be written. */
-    char* extractData();
+    char* extractData() { return extractDataImpl().first; }
     OString extractAsOString();
     std::string extractAsStdString();
 
@@ -85,17 +86,9 @@ private:
     void endArray();
     void endStruct();
     void addCommaBeforeField();
-    void reallocBuffer(int noMoreBytesRequired);
     void writeEscapedOUString(const OUString& rPropVal);
-
-    // this part inline to speed up the fast path
-    inline void ensureSpace(int noMoreBytesRequired)
-    {
-        assert(mpBuffer && "already extracted data");
-        int currentUsed = mPos - mpBuffer;
-        if (currentUsed + noMoreBytesRequired >= mSpaceAllocated)
-            reallocBuffer(noMoreBytesRequired);
-    }
+    std::pair<char*, int> extractDataImpl();
+    void ensureSpace(int noMoreBytesRequired);
 };
 
 /**
diff --git a/tools/source/misc/json_writer.cxx 
b/tools/source/misc/json_writer.cxx
index 7024b580c7fd..d6e34179f930 100644
--- a/tools/source/misc/json_writer.cxx
+++ b/tools/source/misc/json_writer.cxx
@@ -388,18 +388,22 @@ void JsonWriter::addCommaBeforeField()
     }
 }
 
-void JsonWriter::reallocBuffer(int noMoreBytesRequired)
+void JsonWriter::ensureSpace(int noMoreBytesRequired)
 {
+    assert(mpBuffer && "already extracted data");
     int currentUsed = mPos - mpBuffer;
-    auto newSize = std::max<int>(mSpaceAllocated * 2, (currentUsed + 
noMoreBytesRequired) * 2);
-    mpBuffer = static_cast<char*>(realloc(mpBuffer, newSize));
-    mPos = mpBuffer + currentUsed;
-    mSpaceAllocated = newSize;
+    if (currentUsed + noMoreBytesRequired >= mSpaceAllocated)
+    {
+        auto newSize = (currentUsed + noMoreBytesRequired) * 2;
+        mpBuffer = static_cast<char*>(realloc(mpBuffer, newSize));
+        mPos = mpBuffer + currentUsed;
+        mSpaceAllocated = newSize;
+    }
 }
 
 /** Hands ownership of the underlying storage buffer to the caller,
   * after this no more document modifications may be written. */
-char* JsonWriter::extractData()
+std::pair<char*, int> JsonWriter::extractDataImpl()
 {
     assert(mStartNodeCount == 0 && "did not close all nodes");
     assert(mpBuffer && "data already extracted");
@@ -409,24 +413,23 @@ char* JsonWriter::extractData()
     ++mPos;
     // null-terminate
     *mPos = 0;
+    const int sz = mPos - mpBuffer;
     mPos = nullptr;
-    char* pRet = nullptr;
-    std::swap(pRet, mpBuffer);
-    return pRet;
+    return { std::exchange(mpBuffer, nullptr), sz };
 }
 
 OString JsonWriter::extractAsOString()
 {
-    char* pChar = extractData();
-    OString ret(pChar);
+    auto[pChar, sz] = extractDataImpl();
+    OString ret(pChar, sz);
     free(pChar);
     return ret;
 }
 
 std::string JsonWriter::extractAsStdString()
 {
-    char* pChar = extractData();
-    std::string ret(pChar);
+    auto[pChar, sz] = extractDataImpl();
+    std::string ret(pChar, sz);
     free(pChar);
     return ret;
 }

Reply via email to