Skip to content

Commit b7cfd27

Browse files
committed
src: clean up req.bytes tracking
Simply always tell the caller how many bytes were written, rather than letting them track it. In the case of writing a string, also keep track of the bytes written by the earlier `DoTryWrite()`. Refs: #19562 PR-URL: #19551 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1dc8eb4 commit b7cfd27

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/stream_base-inl.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,15 @@ inline StreamWriteResult StreamBase::Write(
194194
Environment* env = stream_env();
195195
int err;
196196

197+
size_t total_bytes = 0;
197198
for (size_t i = 0; i < count; ++i)
198-
bytes_written_ += bufs[i].len;
199+
total_bytes += bufs[i].len;
200+
bytes_written_ += total_bytes;
199201

200202
if (send_handle == nullptr) {
201203
err = DoTryWrite(&bufs, &count);
202204
if (err != 0 || count == 0) {
203-
return StreamWriteResult { false, err, nullptr };
205+
return StreamWriteResult { false, err, nullptr, total_bytes };
204206
}
205207
}
206208

@@ -230,7 +232,7 @@ inline StreamWriteResult StreamBase::Write(
230232
ClearError();
231233
}
232234

233-
return StreamWriteResult { async, err, req_wrap };
235+
return StreamWriteResult { async, err, req_wrap, total_bytes };
234236
}
235237

236238
template <typename OtherBase>

src/stream_base.cc

+12-11
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@ int StreamBase::Shutdown(const FunctionCallbackInfo<Value>& args) {
6060
inline void SetWriteResultPropertiesOnWrapObject(
6161
Environment* env,
6262
Local<Object> req_wrap_obj,
63-
const StreamWriteResult& res,
64-
size_t bytes) {
63+
const StreamWriteResult& res) {
6564
req_wrap_obj->Set(
6665
env->context(),
6766
env->bytes_string(),
68-
Number::New(env->isolate(), bytes)).FromJust();
67+
Number::New(env->isolate(), res.bytes)).FromJust();
6968
req_wrap_obj->Set(
7069
env->context(),
7170
env->async(),
@@ -91,7 +90,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
9190
MaybeStackBuffer<uv_buf_t, 16> bufs(count);
9291

9392
size_t storage_size = 0;
94-
uint32_t bytes = 0;
9593
size_t offset;
9694

9795
if (!all_buffers) {
@@ -123,7 +121,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
123121
Local<Value> chunk = chunks->Get(i);
124122
bufs[i].base = Buffer::Data(chunk);
125123
bufs[i].len = Buffer::Length(chunk);
126-
bytes += bufs[i].len;
127124
}
128125
}
129126

@@ -140,7 +137,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
140137
if (Buffer::HasInstance(chunk)) {
141138
bufs[i].base = Buffer::Data(chunk);
142139
bufs[i].len = Buffer::Length(chunk);
143-
bytes += bufs[i].len;
144140
continue;
145141
}
146142

@@ -160,12 +156,11 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
160156
bufs[i].base = str_storage;
161157
bufs[i].len = str_size;
162158
offset += str_size;
163-
bytes += str_size;
164159
}
165160
}
166161

167162
StreamWriteResult res = Write(*bufs, count, nullptr, req_wrap_obj);
168-
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res, bytes);
163+
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res);
169164
if (res.wrap != nullptr && storage) {
170165
res.wrap->SetAllocatedStorage(storage.release(), storage_size);
171166
}
@@ -193,7 +188,7 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
193188

194189
if (res.async)
195190
req_wrap_obj->Set(env->context(), env->buffer_string(), args[1]).FromJust();
196-
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res, buf.len);
191+
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res);
197192

198193
return res.err;
199194
}
@@ -228,6 +223,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
228223
// Try writing immediately if write size isn't too big
229224
char stack_storage[16384]; // 16kb
230225
size_t data_size;
226+
size_t synchronously_written = 0;
231227
uv_buf_t buf;
232228

233229
bool try_write = storage_size <= sizeof(stack_storage) &&
@@ -243,7 +239,11 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
243239
uv_buf_t* bufs = &buf;
244240
size_t count = 1;
245241
err = DoTryWrite(&bufs, &count);
246-
bytes_written_ += data_size;
242+
// Keep track of the bytes written here, because we're taking a shortcut
243+
// by using `DoTryWrite()` directly instead of using the utilities
244+
// provided by `Write()`.
245+
synchronously_written = count == 0 ? data_size : data_size - buf.len;
246+
bytes_written_ += synchronously_written;
247247

248248
// Immediate failure or success
249249
if (err != 0 || count == 0) {
@@ -299,8 +299,9 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
299299
}
300300

301301
StreamWriteResult res = Write(&buf, 1, send_handle, req_wrap_obj);
302+
res.bytes += synchronously_written;
302303

303-
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res, data_size);
304+
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res);
304305
if (res.wrap != nullptr) {
305306
res.wrap->SetAllocatedStorage(data.release(), data_size);
306307
}

src/stream_base.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct StreamWriteResult {
2323
bool async;
2424
int err;
2525
WriteWrap* wrap;
26+
size_t bytes;
2627
};
2728

2829

0 commit comments

Comments
 (0)