Skip to content

Commit

Permalink
src,stream: improve WriteString
Browse files Browse the repository at this point in the history
Introduce HasDoTryWrite and make use of it in WriteString

PR-URL: #51155
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
ywave620 authored and richardlau committed Mar 25, 2024
1 parent 06a6eef commit b616f6f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}};
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/stream_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
1 change: 1 addition & 0 deletions src/stream_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b616f6f

Please sign in to comment.