From 6d48cec06321d6e2f36a871a21143c37d78c9fe0 Mon Sep 17 00:00:00 2001 From: rogertyang Date: Thu, 14 Dec 2023 19:53:53 +0800 Subject: [PATCH] src,stream: improve WriteString Introduce HasDoTryWrite and make use of it in WriteString --- src/stream_base.cc | 4 ++-- src/stream_base.h | 1 + src/stream_wrap.h | 1 + 3 files changed, 4 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..c60eb80cc11556 100644 --- a/src/stream_base.h +++ b/src/stream_base.h @@ -244,6 +244,7 @@ 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); + 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..0ff07c15ad2742 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,