From b616f6fa06d006ae173fd08192e01c99a14ce6d2 Mon Sep 17 00:00:00 2001 From: ywave620 <60539365+ywave620@users.noreply.github.com> Date: Sat, 30 Dec 2023 18:48:03 +0800 Subject: [PATCH] src,stream: improve WriteString Introduce HasDoTryWrite and make use of it in WriteString PR-URL: https://github.com/nodejs/node/pull/51155 Reviewed-By: Luigi Pinca --- src/stream_base.cc | 4 ++-- src/stream_base.h | 2 ++ src/stream_wrap.h | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/stream_base.cc b/src/stream_base.cc index cfb1e3ae6a2d7d..74001d5b6cee00 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -91,7 +91,7 @@ StreamWriteResult StreamBase::Write(uv_buf_t* bufs, for (size_t i = 0; i < count; ++i) total_bytes += bufs[i].len; bytes_written_ += total_bytes; - if (send_handle == nullptr && !skip_try_write) { + if (send_handle == nullptr && HasDoTryWrite() && !skip_try_write) { err = DoTryWrite(&bufs, &count); if (err != 0 || count == 0) { return StreamWriteResult{false, err, nullptr, total_bytes, {}}; @@ -365,7 +365,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo& args) { size_t synchronously_written = 0; uv_buf_t buf; - bool try_write = storage_size <= sizeof(stack_storage) && + bool try_write = HasDoTryWrite() && storage_size <= sizeof(stack_storage) && (!IsIPCPipe() || send_handle_obj.IsEmpty()); if (try_write) { data_size = StringBytes::Write(isolate, diff --git a/src/stream_base.h b/src/stream_base.h index 8f6a7b22ea1f2f..62a8928e144ad6 100644 --- a/src/stream_base.h +++ b/src/stream_base.h @@ -244,6 +244,8 @@ class StreamResource { // `*bufs` and `*count` accordingly. This is a no-op by default. // Return 0 for success and a libuv error code for failures. virtual int DoTryWrite(uv_buf_t** bufs, size_t* count); + // Indicates whether this subclass overrides the DoTryWrite + virtual inline bool HasDoTryWrite() const { return false; } // Initiate a write of data. // Upon an immediate failure, a libuv error code is returned, // w->Done() will never be called and caller should free `bufs`. diff --git a/src/stream_wrap.h b/src/stream_wrap.h index 197303990c90bc..85a675d04f22f0 100644 --- a/src/stream_wrap.h +++ b/src/stream_wrap.h @@ -52,6 +52,7 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase { // Resource implementation int DoShutdown(ShutdownWrap* req_wrap) override; int DoTryWrite(uv_buf_t** bufs, size_t* count) override; + inline bool HasDoTryWrite() const override { return true; } int DoWrite(WriteWrap* w, uv_buf_t* bufs, size_t count,