From 5241e224b454036968def90236218a34a032cc0b Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Tue, 13 Aug 2024 22:18:12 +0200 Subject: [PATCH] sync: document mpsc channel allocation behavior Fixes: #6754 --- tokio/src/sync/mpsc/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tokio/src/sync/mpsc/mod.rs b/tokio/src/sync/mpsc/mod.rs index 052620be1a9..371da5424e1 100644 --- a/tokio/src/sync/mpsc/mod.rs +++ b/tokio/src/sync/mpsc/mod.rs @@ -78,6 +78,20 @@ //! within a Tokio runtime, however it is still not tied to one specific Tokio //! runtime, and the sender may be moved from one Tokio runtime to another. //! +//! # Allocation behavior +//! +//!
The behavior documented here may change
+//! +//! The mpsc channel stores elements in chunks. Chunks are organized in a linked list. Sending +//! pushes new elements onto the front of the list, and receiving pops them off the back. A chunk +//! can hold 32 messages (regardless of channel and message size). Each chunk also stores 4 +//! pointer-sized values for bookkeeping (so on a 64-bit machine, each message has 1 byte of +//! overhead). +//! +//! When all values in a chunk have been received, it becomes empty. It will then be freed, unless +//! the channels first chunk (where newly-sent elements are being stored) has no next chunk. In +//! that case, the empty chunk is reused as the next chunk. +//! //! [`Sender`]: crate::sync::mpsc::Sender //! [`Receiver`]: crate::sync::mpsc::Receiver //! [bounded-send]: crate::sync::mpsc::Sender::send()