Skip to content

refactor: Replace EventLoop::post() with sync() taking kj::FunctionParam - #347

Open
ViniciusCestarii wants to merge 2 commits into
bitcoin-core:masterfrom
ViniciusCestarii:refact-sync
Open

refactor: Replace EventLoop::post() with sync() taking kj::FunctionParam#347
ViniciusCestarii wants to merge 2 commits into
bitcoin-core:masterfrom
ViniciusCestarii:refact-sync

Conversation

@ViniciusCestarii

@ViniciusCestarii ViniciusCestarii commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

EventLoop::post() and the sync() wrapper around it did the same thing, so this merges them into a single sync() method. And also takes the callback as a kj::FunctionParam instead of a kj::Function: FunctionParam stores only a pointer to the caller's callable rather than taking ownership of it, so the callable no longer needs to be copyable or movable, and each call avoids a heap allocation.

This is safe because sync() does not return until the event loop thread has finished running the callback and cleared m_post_fn(now named m_sync_fn), so the pointer is never used after the callable goes out of scope.

@DrahtBot

DrahtBot commented Aug 16, 2026

Copy link
Copy Markdown

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK xyzconstant, ryanofsky

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

@ryanofsky

Copy link
Copy Markdown
Collaborator

Concept ACK 2b23e78. Looks like a nice efficiency improvement!

Note that we should be able to make this more efficient by dropping the pipe used to wake the event loop and using the cross-thread mechanisms newer versions of capnproto provide (kj::Executor, kj::newPromiseAndCrossThreadFulfiller, or kj::EventPort::wake). But this PR is a nice targetted improvement.

@xyzconstant

Copy link
Copy Markdown
Contributor

lgtm ACK 2b23e78

@DrahtBot
DrahtBot requested a review from ryanofsky August 28, 2026 22:43

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Code review ACK 2b23e78. Nice find and simplification!

Comment thread include/mp/proxy-io.h
Comment on lines +271 to +276
//! The function is accepted as a kj::FunctionParam, which only stores a
//! pointer to the caller's callable instead of taking ownership of it. This
//! avoids a heap allocation per call, and is safe because this call is
//! synchronous, so the callable is guaranteed not to go out of scope before
//! the event loop thread is done running it. It also means the callable
//! does not need to be copyable or movable.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In commit "Fix segfault when creating new wallet" (d3800d59d557388b813513ef16f6b840467821d4)

I think this comment would be great to have in the commit message, but would suggest removing it from the code because it is mostly just repeating capnproto API documentation. This seems like a straightforward use of the FunctionParam type as documented. It also seems potentially confusing for callers for the method documentation to be describing why the method definition is safe. It would make more sense to write that in the implementation if necessary.

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.

4 participants