commit 3a6d85e46efe2c20748ba17e33f50f9fab584845
parent d89acdce02035974ba540c785e797a6996e99b51
Author: Florian Quèze <florian@queze.net>
Date: Fri, 14 Nov 2025 11:17:12 +0000
Bug 1989077 - Increase the buffer size used in nsNetUtil.cpp, r=necko-reviewers,valentin.
Differential Revision: https://phabricator.services.mozilla.com/D265133
Diffstat:
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml
@@ -13285,6 +13285,13 @@
value: true
mirror: always
+# Default buffer size for buffered stream operations. The actual value used will
+# be floored to the nearest power of two.
+- name: network.buffer.default_size
+ type: RelaxedAtomicUint32
+ value: 65536
+ mirror: always
+
# See the full list of values in nsICookieService.idl.
- name: network.cookie.cookieBehavior
type: RelaxedAtomicInt32
diff --git a/netwerk/base/nsNetUtil.cpp b/netwerk/base/nsNetUtil.cpp
@@ -16,6 +16,7 @@
#include "mozilla/Encoding.h"
#include "mozilla/LoadContext.h"
#include "mozilla/LoadInfo.h"
+#include "mozilla/MathAlgorithms.h"
#include "mozilla/Monitor.h"
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/StaticPrefs_network.h"
@@ -1398,7 +1399,11 @@ Result<nsCOMPtr<nsIInputStream>, nsresult> NS_NewBufferedInputStream(
namespace {
-#define BUFFER_SIZE 8192
+// Returns the buffer size from the pref, floored to the nearest power of two.
+static uint32_t GetBufferSize() {
+ uint32_t prefValue = StaticPrefs::network_buffer_default_size();
+ return uint32_t(1) << FloorLog2(prefValue);
+}
class BufferWriter final : public nsIInputStreamCallback {
public:
@@ -1423,8 +1428,9 @@ class BufferWriter final : public nsIInputStreamCallback {
// Let's make the inputStream buffered if it's not.
if (!NS_InputStreamIsBuffered(mInputStream)) {
nsCOMPtr<nsIInputStream> bufferedStream;
- nsresult rv = NS_NewBufferedInputStream(
- getter_AddRefs(bufferedStream), mInputStream.forget(), BUFFER_SIZE);
+ nsresult rv =
+ NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
+ mInputStream.forget(), GetBufferSize());
NS_ENSURE_SUCCESS(rv, rv);
mInputStream = bufferedStream;
@@ -1517,7 +1523,7 @@ class BufferWriter final : public nsIInputStreamCallback {
}
uint64_t offset = mWrittenData;
- uint64_t length = mCount == -1 ? BUFFER_SIZE : mCount;
+ uint64_t length = mCount == -1 ? GetBufferSize() : mCount;
// Let's try to read data directly.
uint32_t writtenData;
@@ -1603,15 +1609,16 @@ class BufferWriter final : public nsIInputStreamCallback {
MOZ_ASSERT(mCount == -1);
- if (mBufferSize >= mWrittenData + BUFFER_SIZE) {
+ uint32_t bufSize = GetBufferSize();
+ if (mBufferSize >= mWrittenData + bufSize) {
// The buffer is big enough.
return true;
}
CheckedUint32 bufferSize =
- std::max<uint32_t>(static_cast<uint32_t>(mWrittenData), BUFFER_SIZE);
+ std::max<uint32_t>(static_cast<uint32_t>(mWrittenData), bufSize);
while (bufferSize.isValid() &&
- bufferSize.value() < mWrittenData + BUFFER_SIZE) {
+ bufferSize.value() < mWrittenData + bufSize) {
bufferSize *= 2;
}