Bug 1468108 - Increase the buffer size and avoid extra runnables in NS_ReadInputStreamToBuffer, r=valentin
authorAndrea Marchesini <amarchesini@mozilla.com>
Sun, 17 Jun 2018 20:30:58 -0400
changeset 422816 61882e8a3cf3ffe6a23f90f54d6cd6091f0b6544
parent 422805 c40cc0a89bc70511751d17ed0cdd569a74abac88
child 422817 f804cc575bba9c6dcb4e3770f7dafe4d8934e73c
child 422823 ace527e70c2588fc49a07e6227aa42ab51e88988
push id34150
push usernerli@mozilla.com
push dateMon, 18 Jun 2018 09:02:38 +0000
treeherdermozilla-central@f804cc575bba [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin
bugs1468108
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1468108 - Increase the buffer size and avoid extra runnables in NS_ReadInputStreamToBuffer, r=valentin
netwerk/base/nsNetUtil.cpp
--- a/netwerk/base/nsNetUtil.cpp
+++ b/netwerk/base/nsNetUtil.cpp
@@ -1515,17 +1515,17 @@ NS_NewBufferedInputStream(nsIInputStream
             in.forget(aResult);
         }
     }
     return rv;
 }
 
 namespace {
 
-#define BUFFER_SIZE 4096
+#define BUFFER_SIZE 8192
 
 class BufferWriter final : public Runnable
                          , public nsIInputStreamCallback
 {
 public:
     NS_DECL_ISUPPORTS_INHERITED
 
     BufferWriter(nsIInputStream* aInputStream,
@@ -1673,74 +1673,73 @@ private:
     NS_IMETHOD
     Run() override
     {
         MOZ_ASSERT(mAsyncInputStream);
         MOZ_ASSERT(!mInputStream);
 
         MonitorAutoLock lock(mMonitor);
 
-        if (mCompleted) {
-            return NS_OK;
-        }
-
-        if (mCount == 0) {
-            OperationCompleted(lock, NS_OK);
-            return NS_OK;
-        }
-
-        if (mCount == -1 && !MaybeExpandBufferSize()) {
-            OperationCompleted(lock, NS_ERROR_OUT_OF_MEMORY);
-            return NS_OK;
-        }
-
-        uint64_t offset = mWrittenData;
-        uint64_t length = mCount == -1 ? BUFFER_SIZE : mCount;
-
-        // Let's try to read it directly.
-        uint32_t writtenData;
-        nsresult rv = mAsyncInputStream->ReadSegments(NS_CopySegmentToBuffer,
-                                                     static_cast<char*>(mBuffer) + offset,
-                                                     length, &writtenData);
-
-        // Operation completed.
-        if (NS_SUCCEEDED(rv) && writtenData == 0) {
-            OperationCompleted(lock, NS_OK);
+        while (true) {
+            if (mCompleted) {
+                return NS_OK;
+            }
+
+            if (mCount == 0) {
+                OperationCompleted(lock, NS_OK);
+                return NS_OK;
+            }
+
+            if (mCount == -1 && !MaybeExpandBufferSize()) {
+                OperationCompleted(lock, NS_ERROR_OUT_OF_MEMORY);
+                return NS_OK;
+            }
+
+            uint64_t offset = mWrittenData;
+            uint64_t length = mCount == -1 ? BUFFER_SIZE : mCount;
+
+            // Let's try to read it directly.
+            uint32_t writtenData;
+            nsresult rv = mAsyncInputStream->ReadSegments(NS_CopySegmentToBuffer,
+                                                         static_cast<char*>(mBuffer) + offset,
+                                                         length, &writtenData);
+
+            // Operation completed.
+            if (NS_SUCCEEDED(rv) && writtenData == 0) {
+                OperationCompleted(lock, NS_OK);
+                return NS_OK;
+            }
+
+            // If we succeeded, let's try to read again.
+            if (NS_SUCCEEDED(rv)) {
+                mWrittenData += writtenData;
+                if (mCount != -1) {
+                    MOZ_ASSERT(mCount >= writtenData);
+                    mCount -= writtenData;
+                }
+
+                continue;
+            }
+
+            // Async wait...
+            if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
+                rv = mAsyncInputStream->AsyncWait(this, 0, length, mTaskQueue);
+                if (NS_WARN_IF(NS_FAILED(rv))) {
+                    OperationCompleted(lock, rv);
+                }
+                return NS_OK;
+            }
+
+            // Error.
+            OperationCompleted(lock, rv);
             return NS_OK;
         }
 
-        // If we succeeded, let's try to read again.
-        if (NS_SUCCEEDED(rv)) {
-            mWrittenData += writtenData;
-            if (mCount != -1) {
-                MOZ_ASSERT(mCount >= writtenData);
-                mCount -= writtenData;
-            }
-
-            nsCOMPtr<nsIRunnable> runnable = this;
-            rv = mTaskQueue->Dispatch(runnable.forget());
-            if (NS_WARN_IF(NS_FAILED(rv))) {
-                OperationCompleted(lock, rv);
-            }
-
-            return NS_OK;
-        }
-
-        // Async wait...
-        if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
-            rv = mAsyncInputStream->AsyncWait(this, 0, length, mTaskQueue);
-            if (NS_WARN_IF(NS_FAILED(rv))) {
-                OperationCompleted(lock, rv);
-            }
-            return NS_OK;
-        }
-
-        // Error.
-        OperationCompleted(lock, rv);
-        return NS_OK;
+        MOZ_ASSERT_UNREACHABLE("We should not be here");
+        return NS_ERROR_FAILURE;
     }
 
     NS_IMETHOD
     OnInputStreamReady(nsIAsyncInputStream* aStream) override
     {
         MOZ_ASSERT(aStream == mAsyncInputStream);
         // The stream is ready, let's read it again.
         return Run();