[Libreoffice-commits] online.git: common/IoUtil.cpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Mon Nov 28 04:56:31 UTC 2016


 common/IoUtil.cpp |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

New commits:
commit b16fb68ec61188365db40a38f7f5209b65158988
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sat Nov 26 22:39:48 2016 -0500

    loolwsd: shrink SocketProcessor buffer when too large
    
    When receiving large messages the dynamic socket buffer
    is resized to accomodate the incoming large message.
    
    This buffer was previously never reduced in size.
    This is an obvious leak that is now avoided.
    
    When the buffer grows beyond quadruple the default
    size, it is shrunk back to the default. This gives
    a decent balance between memory waste and unnecessary
    resizing up and down after each large message received.
    
    Change-Id: Ic3996c80e96592af6f12c4abd1dd737bdc07b814
    Reviewed-on: https://gerrit.libreoffice.org/31287
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/common/IoUtil.cpp b/common/IoUtil.cpp
index 768ab95..b4751ac 100644
--- a/common/IoUtil.cpp
+++ b/common/IoUtil.cpp
@@ -54,12 +54,12 @@ void SocketProcessor(const std::shared_ptr<LOOLWebSocket>& ws,
     int n = -1;
     bool stop = false;
     std::vector<char> payload(READ_BUFFER_SIZE);
+    payload.resize(0);
     try
     {
+        // We poll, no need for timeout.
         ws->setReceiveTimeout(0);
 
-        payload.resize(0);
-
         for (;;)
         {
             stop = stopPredicate();
@@ -161,6 +161,14 @@ void SocketProcessor(const std::shared_ptr<LOOLWebSocket>& ws,
                 LOG_INF("SocketProcessor [" << name << "]: Handler flagged to finish.");
                 break;
             }
+
+            if (payload.capacity() > READ_BUFFER_SIZE * 4)
+            {
+                LOG_INF("Compacting buffer of SocketProcessor [" << name << "] from " <<
+                        payload.capacity() / 1024 << "KB to " << READ_BUFFER_SIZE / 1024 << "KB.");
+                payload = std::vector<char>(READ_BUFFER_SIZE);
+                payload.resize(0);
+            }
         }
     }
     catch (const std::exception& exc)


More information about the Libreoffice-commits mailing list