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

pull ViniciusCestarii wants to merge 2 commits into bitcoin-core:master from ViniciusCestarii:refact-sync changing 4 files +20 −27
  1. ViniciusCestarii commented at 2:56 PM on August 16, 2026: 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.

  2. DrahtBot commented at 2:56 PM on August 16, 2026: none

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--021abf342d371248e50ceaed478a90ca-->

    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 <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. ryanofsky commented at 11:03 PM on August 25, 2026: collaborator

    Concept ACK 2b23e78b35fe0208a782db17e49b04c4f03aa088. 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.

  4. xyzconstant commented at 10:43 PM on August 28, 2026: contributor

    lgtm ACK 2b23e78b35fe0208a782db17e49b04c4f03aa088

  5. DrahtBot requested review from ryanofsky on Aug 28, 2026
  6. in include/mp/proxy-io.h:276 in d6f7443370
     281 | +    //! The function is accepted as a kj::FunctionParam, which only stores a
     282 | +    //! pointer to the caller's callable instead of taking ownership of it. This
     283 | +    //! avoids a heap allocation per call, and is safe because this call is
     284 | +    //! synchronous, so the callable is guaranteed not to go out of scope before
     285 | +    //! the event loop thread is done running it. It also means the callable
     286 | +    //! does not need to be copyable or movable.
    


    ryanofsky commented at 8:41 PM on August 31, 2026:

    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.


    ViniciusCestarii commented at 1:20 PM on September 1, 2026:

    True, thanks. Removed from the code and added as message of commit 2330fbe81e255996710e3e258cb6a5ea94cfb250

  7. ryanofsky approved
  8. ryanofsky commented at 8:52 PM on August 31, 2026: collaborator

    Code review ACK 2b23e78b35fe0208a782db17e49b04c4f03aa088. Nice find and simplification!

  9. refactor: replace EventLoop::post() with sync() taking kj::FunctionParam
    kj::FunctionParam 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 EventLoop:sync() 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.
    2330fbe81e
  10. refactor: rename EventLoop::m_post_fn to m_sync_fn 5c49666a1d
  11. ViniciusCestarii force-pushed on Sep 1, 2026
  12. ViniciusCestarii commented at 1:23 PM on September 1, 2026: contributor

    Thanks for the reviews! Forced push 5c49666a1d8860028b6ed04b3aead64d06979d6d addressing suggestion #347 (review).

  13. xyzconstant commented at 8:07 PM on September 1, 2026: contributor

    re-ACK 5c49666a1d8860028b6ed04b3aead64d06979d6d

  14. DrahtBot requested review from ryanofsky on Sep 1, 2026
  15. ryanofsky commented at 5:59 PM on September 8, 2026: collaborator

    Code review ACK 5c49666a1d8860028b6ed04b3aead64d06979d6d. Just moved since comment since last review

  16. ryanofsky merged this on Sep 8, 2026
  17. ryanofsky closed this on Sep 8, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/libmultiprocess. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-09-16 09:30 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me