Skip to content

Commit 5ea3c63

Browse files
authored
sync: document mpsc channel allocation behavior (#6773)
1 parent 56f3f40 commit 5ea3c63

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

tokio/src/sync/mpsc/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@
7878
//! within a Tokio runtime, however it is still not tied to one specific Tokio
7979
//! runtime, and the sender may be moved from one Tokio runtime to another.
8080
//!
81+
//! # Allocation behavior
82+
//!
83+
//! <div class="warning">The implementation details described in this section may change in future
84+
//! Tokio releases.</div>
85+
//!
86+
//! The mpsc channel stores elements in blocks. Blocks are organized in a linked list. Sending
87+
//! pushes new elements onto the block at the front of the list, and receiving pops them off the
88+
//! one at the back. A block can hold 32 messages on a 64-bit target and 16 messages on a 32-bit
89+
//! target. This number is independent of channel and message size. Each block also stores 4
90+
//! pointer-sized values for bookkeeping (so on a 64-bit machine, each message has 1 byte of
91+
//! overhead).
92+
//!
93+
//! When all values in a block have been received, it becomes empty. It will then be freed, unless
94+
//! the channel's first block (where newly-sent elements are being stored) has no next block. In
95+
//! that case, the empty block is reused as the next block.
96+
//!
8197
//! [`Sender`]: crate::sync::mpsc::Sender
8298
//! [`Receiver`]: crate::sync::mpsc::Receiver
8399
//! [bounded-send]: crate::sync::mpsc::Sender::send()

0 commit comments

Comments
 (0)