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 <luigipinca@gmail.com>
This commit is contained in:
ywave620
2023-12-30 18:48:03 +08:00
committed by GitHub
parent 55f3993e3e
commit 461ffcadff
3 changed files with 5 additions and 2 deletions

View File

@@ -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<Value>& 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,

View File

@@ -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`.

View File

@@ -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,