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

Add recv_many for mpsc channels #6010

Merged
merged 37 commits into from
Oct 17, 2023
Merged

Add recv_many for mpsc channels #6010

merged 37 commits into from
Oct 17, 2023

Commits on Oct 9, 2023

  1. Method recv_many returns a Vec<T> for mpsc channels

    Circumstances may exist where multiple messages are queued for the
    receiver by the time the receiving task runs -- and the receiving
    task itself may benefit from batch as opposed to item-by-item
    processing, e.g., update a row based on a primary key but
    only the most recent update is needed; recv_many
    enables the receiver to process messages last-first
    and skip earlier updates for rows already updated.
    
    The change involved introducing a peek function to sync/mpsc/list.rs;
    it is based on fn pop.  fn recv_many returns Poll<Vec<T>> contrasting
    recv's Poll<Option<T>>.  The new function returns
    Ready(vec![]) where recv returned Ready(None).
    
    The mpsc.rs trait Semaphore was extended with two functions,
    num_acquired() and add_permits(n) -- which are used to size
    the vector and update the underlying semaphores in a single
    call.
    
    Simple tests were added to tests/sync_mpsc.rs.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    f13ea3e View commit details
    Browse the repository at this point in the history
  2. Update recv_many to populate a passed-in buffer.

    The implementation uses a passed-in buffer to return results.
    The buffer is always cleared, and its capacity may be
    increased to match the number of queued messages.
    This avoids excessive memory allocation.
    
    This commit also adds new benchmarks in benches/sync_mpsc_recv_many.rs
    It borrows substantially from benches/sync_mpsc.rs
    
    The benchmarks are framed around the idea that only the last value
    is needed, e.g., what might be typical in a dashboard.
    
    The following benchmarks compare extracting the last value from
    5_000 recv's vs. a variable number of recv_many:
      contention_bounded_updater_recv vs.  contention_bounded_updater_recv_many
      contention_bounded_full_updater_recv vs.  contention_bounded_full_updater_recv_many
      contention_unbounded_updater_recv vs. contention_unbounded_updater_recv_many
      uncontented_bounded_updater_recv vs.  uncontented_bounded_updater_recv_many
      uncontented_unbounded_updater_recv vs.  uncontented_unbounded_updater_recv_many
    
    Initial tests suggests a ~30% performance improvement for the
    uncontented tests.
    
    The benchmarks contention_bounded_updater_publish_recv and
    contention_bounded_updater_publish_recv_many are different;
    first the number of messages recevied are only 1_000.
    In these test, we contemplate an actor who needs to receive a message
    and forward it over an API that takes 1ns to complete.
    An actor accepting a single message-at-a-time
    will hit the 1ns lag repeatedly; the actor that can pick only
    the last message out of the buffer from recv_many has much less work.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    0133425 View commit details
    Browse the repository at this point in the history
  3. fix rustfmt issues

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    7d0d8db View commit details
    Browse the repository at this point in the history
  4. fix returning the result of a binding from a block

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    487f887 View commit details
    Browse the repository at this point in the history
  5. fix rustfmt braces around continue

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    7086b34 View commit details
    Browse the repository at this point in the history
  6. Update from bencher and merge benchmarks into sync_mpsc.rs

    Previous benchmarks in sync_mpsc_recv_many.rs included a
    contrived example that is removed.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    9a170b0 View commit details
    Browse the repository at this point in the history
  7. fix Cargo.toml to reflect deletion of old benchmark

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    01b2270 View commit details
    Browse the repository at this point in the history
  8. Simplified recv_many implementation

    Removed methods num_acquired and peek; allow caller to control
    the number of retrieved elements by setting the capacity of the
    result buffer.  In the event the result buffer has no capacity,
    the capacity is set to BLOCK_CAP.  Added examples to the
    documentation.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    8c8b06f View commit details
    Browse the repository at this point in the history
  9. Fix test send_recv_unbounded for arbitrarty BLOCK_CAP values

    Also, update test async_send_recv_many_with_buffer to test that
    buffer capacity is not increased by recv_many.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    dc81be3 View commit details
    Browse the repository at this point in the history
  10. Fix for rustfmt

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    8f5b00c View commit details
    Browse the repository at this point in the history
  11. switch to break to exit loop

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    3c1dc33 View commit details
    Browse the repository at this point in the history
  12. resolve return value 0 for recv_many

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    757bc33 View commit details
    Browse the repository at this point in the history
  13. Change to recv_many to no longer clear the output buffer

    This commit changes the behavior of recv_many
    to no longer clear the supplied buffer on each call.
    Now, recv_many will append new messages to the end of
    the supplied buffer until it reaches capacity.  The
    return value is the number of new messages added to
    the buffer.
    This may be useful for a caller who expects a fixed number
    of messages and wishes to avoid copying.
    At the time of the call, recv_many now asserts that
    the capacity of the buffer exceeds its length (fail-fast).
    The caller must appropriately clear/resize the buffer between
    calls if it is to be reused.
    The method recv_many no longer changes the capacity of the
    supplied buffer.
    
    Unit and doc and doc tests were updated to test and document
    the new behavior.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    0948857 View commit details
    Browse the repository at this point in the history
  14. Filled buffer handling now fail-safe, not fail-fast

    Implements approach of Darksonn, where the buffer is extended
    when the capacity is equal to the length at the time of the call
    to recv_many.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    1dc2dbf View commit details
    Browse the repository at this point in the history
  15. improved doctest example

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    6bf430c View commit details
    Browse the repository at this point in the history
  16. fixed nested if; resolve documentation issues for recv_many

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    cdc3665 View commit details
    Browse the repository at this point in the history
  17. Update tokio/src/sync/mpsc/unbounded.rs

    Co-authored-by: Alice Ryhl <[email protected]>
    aschweig and Darksonn committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    4c1321f View commit details
    Browse the repository at this point in the history
  18. Update tokio/src/sync/mpsc/unbounded.rs

    Co-authored-by: Alice Ryhl <[email protected]>
    aschweig and Darksonn committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    27ae226 View commit details
    Browse the repository at this point in the history
  19. Updated doctests to be similar; clarified documentation

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    9a1d9bc View commit details
    Browse the repository at this point in the history
  20. accomodate that with_capacity(n) gives capacity of at least n

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    7fca954 View commit details
    Browse the repository at this point in the history
  21. Improved documentation for recv_many and added test

    The documentation is changed to better highlight the limited
    circumstance in which `recv_many` may return 0,
    '..will never return 0 unless...'.
    Two new tests are somewhat analogous to the doctests were added:
      - send_recv_many_bounded_capacity()
      - send_recv_many_unbounded_capacity()
    These ensure that for buffers that have not reached their
    capacity a call to `recv_many` only fills up to the capacity
    leaving other messages still queued. A subsequent
    call on a filled buffer would reserve additional elements.  The test
    also looks at pending messages holding the channel open --
    compared to the doctest example which showcases non-dropped
    senders.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    fe44b32 View commit details
    Browse the repository at this point in the history
  22. Update tokio/src/sync/mpsc/bounded.rs

    Co-authored-by: Alice Ryhl <[email protected]>
    aschweig and Darksonn committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    aac0eda View commit details
    Browse the repository at this point in the history
  23. Update tokio/src/sync/mpsc/bounded.rs

    Co-authored-by: Alice Ryhl <[email protected]>
    aschweig and Darksonn committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    088537c View commit details
    Browse the repository at this point in the history
  24. change recv_many doctests to use drop not scope

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    effb0e5 View commit details
    Browse the repository at this point in the history
  25. adopt improved wording on buffer capacity behavior

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    1fb08df View commit details
    Browse the repository at this point in the history
  26. Only reserve if no unused capacity and there is 1+ value(s) to receive

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    bd1ef3a View commit details
    Browse the repository at this point in the history
  27. Updated recv_many to accept a new argument limit

    Positive values of `limit` set the maximum number of elements
    that can be added to `buffer` in a single call.
    A zero value of `limit` sets the maximum number of elements
    that can be added to a default value, currently `super::BLOCK_CAP`.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    d31fd17 View commit details
    Browse the repository at this point in the history
  28. rename to_go to remaining

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    3cd2e46 View commit details
    Browse the repository at this point in the history
  29. Change behavior of recv_many(buf, 0) to return 0 immediately.

    Returning 0 immediately when limit=0 is consistent with
    the current take(0) behavior.  Tests were updated.
    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    326fa53 View commit details
    Browse the repository at this point in the history
  30. Fix doc issues

    Aaron Schweiger authored and aschweig committed Oct 9, 2023
    Configuration menu
    Copy the full SHA
    e8950ce View commit details
    Browse the repository at this point in the history

Commits on Oct 11, 2023

  1. Update tokio/src/sync/mpsc/chan.rs

    Co-authored-by: Alice Ryhl <[email protected]>
    aschweig and Darksonn committed Oct 11, 2023
    Configuration menu
    Copy the full SHA
    ead4ca6 View commit details
    Browse the repository at this point in the history
  2. Update tokio/src/sync/mpsc/chan.rs

    Co-authored-by: Alice Ryhl <[email protected]>
    aschweig and Darksonn committed Oct 11, 2023
    Configuration menu
    Copy the full SHA
    2fcb715 View commit details
    Browse the repository at this point in the history
  3. check the coop budget in limit==0 case

    Aaron Schweiger authored and Aaron Schweiger committed Oct 11, 2023
    Configuration menu
    Copy the full SHA
    ba46b7c View commit details
    Browse the repository at this point in the history
  4. clean up some recv_many tests

    Aaron Schweiger authored and Aaron Schweiger committed Oct 11, 2023
    Configuration menu
    Copy the full SHA
    b1d1cae View commit details
    Browse the repository at this point in the history
  5. Update tokio/src/sync/mpsc/bounded.rs

    Co-authored-by: Alice Ryhl <[email protected]>
    aschweig and Darksonn committed Oct 11, 2023
    Configuration menu
    Copy the full SHA
    f162ca7 View commit details
    Browse the repository at this point in the history
  6. Reflow doc comments; adjust comment in chan.rs

    Aaron Schweiger authored and Aaron Schweiger committed Oct 11, 2023
    Configuration menu
    Copy the full SHA
    9f39dc7 View commit details
    Browse the repository at this point in the history

Commits on Oct 13, 2023

  1. Configuration menu
    Copy the full SHA
    27e39d9 View commit details
    Browse the repository at this point in the history