Skip to content
New issue

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

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

Already on GitHub? # to your account

Coroutines Conversion: Migrate streams compression to coroutines #866

Merged
merged 3 commits into from
Jul 17, 2023
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
15 changes: 7 additions & 8 deletions src/workerd/api/streams/compression.c++
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,15 @@ public:
kj::Promise<void> write(kj::ArrayPtr<const kj::ArrayPtr<const kj::byte>> pieces) override {
KJ_SWITCH_ONEOF(state) {
KJ_CASE_ONEOF(ended, Ended) {
return JSG_KJ_EXCEPTION(FAILED, Error, "Write after close.");
}
KJ_CASE_ONEOF(exception, kj::Exception) {
return kj::cp(exception);
JSG_FAIL_REQUIRE(Error, "Write after close");
}
KJ_CASE_ONEOF(exception, kj::Exception) { kj::throwFatalException(kj::cp(exception)); }
KJ_CASE_ONEOF(open, Open) {
if (pieces.size() == 0) return kj::READY_NOW;
return write(pieces[0].begin(), pieces[0].size()).then([this, pieces]() {
return write(pieces.slice(1, pieces.size()));
});
if (pieces.size() != 0) {
co_await write(pieces[0].begin(), pieces[0].size());
co_await write(pieces.slice(1, pieces.size()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm late to the review, but want to point out that this conversion inhibits tail-call optimization. If pieces is sufficiently large, this could theoretically cause a stack overflow.

To avoid this problem, this recursive write() function would need to be refactored into a for loop.

}
co_return;
}
}
KJ_UNREACHABLE;
Expand Down