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

Move coCapture() to libkj #82

Merged
merged 2 commits into from Oct 7, 2022
Merged

Conversation

harrishancock
Copy link
Collaborator

@harrishancock harrishancock commented Oct 7, 2022

It bugged me that coCapture() didn't live in libkj -- it's something that pretty much everyone who uses coroutines will need.

So far this branch only applies a couple cleanups to coCapture() to make it more palatable to libkj. I then copied the result verbatim to this counterpart capnproto PR: capnproto/capnproto#1561. Once that is merged, I'll add another commit to update workerd's capnproto dependency and kj::-qualify any uses of the function.

Mostly this means avoiding including stdlib headers and kj/debug.h if possible.

template<typename ...Args>
static auto coInvoke(Functor functor, Args&&... args) -> ReturnType<Args&&...> {
static auto coInvoke(Functor functor, Args&&... args)
Copy link
Collaborator

Choose a reason for hiding this comment

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

the name functor here confuses me a lot.

As far as I remember the theory, in expression map(list, f) - map is a functor. f is a function.
Or more exactly: map(*, f) is a list->list functor. (https://en.wikipedia.org/wiki/Functor_(functional_programming)#:~:text=In%20functional%20programming%2C%20a%20functor,in%20Haskell%20using%20type%20class)

So in this context CaptureForCoroutine is a functor. And it operates on functions.

Wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree with you, but the misnomer is already quite prevalent in this codebase. I refrained from changing the names (I prefer "function object", or maybe nowadays "invokable", myself) to avoid opening the can of worms. :)

Copy link
Member

Choose a reason for hiding this comment

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

It is a misnomer, but it's a broader C++ community misnomer, not one unique to this code. That wiki page itself hints at why it ended up getting used here:

In C++, the name functor is commonly used to refer to a function object, even though the ISO/IEC 14882 standard specification itself exclusively uses the latter term.

I don't think anyone would be too upset if you just renamed this to fn though.

maybeFunctor, "Attempted to invoke CaptureForCoroutine functor multiple times"));
KJ_IREQUIRE(maybeFunctor != nullptr,
"Attempted to invoke CaptureForCoroutine functor multiple times");
auto localFunctor = kj::mv(*kj::_::readMaybe(maybeFunctor));
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not KJ_IF_MAYBE? Can't you write the same code using it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's just a microoptimization that the compiler could also probably figure out. If I used a KJ_IF_MAYBE, I would be adding a redundant nullness check.

@kentonv
Copy link
Member

kentonv commented Oct 7, 2022

Hmm did GitHub automatically add all codeowners as reviewers? That's a little annoying, I'd prefer that people still choose a specific reviewer.

@harrishancock harrishancock merged commit 1353f0a into main Oct 7, 2022
@harrishancock
Copy link
Collaborator Author

Hmm did GitHub automatically add all codeowners as reviewers? That's a little annoying, I'd prefer that people still choose a specific reviewer.

Yup.

@kentonv kentonv deleted the harris/move-co-capture-to-kj branch October 7, 2022 20:28
@mikea
Copy link
Collaborator

mikea commented Oct 7, 2022

@moderation
Copy link

I think you missed an include here: https://github.com/cloudflare/workerd/blob/main/src/workerd/io/worker.c%2B%2B#L18

@harrishancock

Came to report this too

src/workerd/io/worker.c++:18:10: fatal error: 'workerd/util/co-capture.h' file not found
#include <workerd/util/co-capture.h>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

@harrishancock
Copy link
Collaborator Author

D'oh! Thanks for the reports, fix up here: #84

@dom96
Copy link
Collaborator

dom96 commented Oct 11, 2022

Hmm did GitHub automatically add all codeowners as reviewers? That's a little annoying, I'd prefer that people still choose a specific reviewer.

There is a feature request for this here: https://github.com/orgs/community/discussions/35673.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants