Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couple of minor cleanups/simplifications in the Node.js buffer impl #2317

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/workerd/api/node/buffer.c++
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ uint32_t writeInto(
string.writeInto(js, buf, options);
auto bytes = decodeHexTruncated(buf, false);
auto amountToCopy = kj::min(bytes.size(), dest.size());
memcpy(dest.begin(), bytes.begin(), amountToCopy);
dest.first(amountToCopy).copyFrom(bytes.first(amountToCopy));
return amountToCopy;
}
}
Expand Down Expand Up @@ -319,22 +319,29 @@ kj::Array<kj::byte> BufferUtil::concat(
uint32_t length) {
if (length == 0) return kj::Array<kj::byte>();

// The Node.js Buffer.concat is interesting in that it doesn't just append
// the buffers together as is. The length parameter is used to determine the
// length of the result which can be lesser or greater than the actual
// combined lengths of the inputs. If the length is lesser, the result will
// be a truncated version of the combined buffers. If the length is greater,
// the result will be the combined buffers with the remaining space filled
// with zeroes.

auto dest = kj::heapArray<kj::byte>(length);
uint32_t offset = 0;
uint32_t remaining = length;
auto ptr = dest.begin();
auto view = dest.asPtr();

for (auto& src : list) {
if (src.size() == 0) continue;
auto amountToCopy = kj::min(src.size(), remaining);
std::copy(src.begin(), src.begin() + amountToCopy, ptr + offset);
offset += amountToCopy;
remaining -= amountToCopy;
// The amount to copy is the lesser of the remaining space in the destination or
// the size of the chunk we're copying.
auto amountToCopy = kj::min(src.size(), view.size());
view.first(amountToCopy).copyFrom(src.first(amountToCopy));
view = view.slice(amountToCopy);
// If there's no more space in the destination, we're done.
if (view == nullptr) return kj::mv(dest);
}
KJ_DASSERT(offset <= length);
if (length - offset > 0) {
dest.slice(offset).fill(0);
}

// Fill any remaining space in the destination with zeroes.
view.fill(0);
return kj::mv(dest);
}

Expand Down
Loading