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

Document allocation behaviour of mpsc and other channels #6754

Closed
barafael opened this issue Aug 6, 2024 · 4 comments · Fixed by #6773
Closed

Document allocation behaviour of mpsc and other channels #6754

barafael opened this issue Aug 6, 2024 · 4 comments · Fixed by #6773
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync T-docs Topic: documentation

Comments

@barafael
Copy link
Contributor

barafael commented Aug 6, 2024

Say I have a program utilizing tokio and many mpsc channels, and I'm expecting it to take quite some load, occasionally. How do I estimate the memory usage of the channels and the general allocation behaviour of the program?

I can measure and I will see that bounded mpsc allocates chunks at a time, and that it does not seem to deallocate them after allocating them once.

Questions remain: Does the chunk size depend on the number of elements already in the channel in some way? Does the channel NEVER deallocate, or deallocate at some later point in time? How much overhead is there, per channel element? Are there advantages to using channel sizes that are power of 2s? Is it OK to make an mpsc channel with 10 million elements?

Solution: Documentation?

It would be useful in that case to document these behaviours.

It would be understandable if those behaviours are expected to change and thus users should abstain from making any assumptions about them. That would also perhaps be worth a remark.

Additional context
mpsc would be the most useful for me, and I see in general that the question usually comes up with mpsc because of its wide use, but it might also be interesting for broadcast.

@barafael barafael added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Aug 6, 2024
@Darksonn Darksonn added T-docs Topic: documentation M-sync Module: tokio/sync labels Aug 6, 2024
@Darksonn
Copy link
Contributor

Darksonn commented Aug 6, 2024

I would be happy to see things documented, but we may change it in the future.

To give a short answer to your question, the mpsc channel allocates chunks with space for 32 messages. The chunks are in a linked list. When the last message is removed from a chunk, then it is freed. Though there is some logic to reuse chunks instead of freeing them, so if the chunk that messages are currently being added to has no next chunk, it will move the empty chunk there instead of freeing it.

The capacity of the channel has no effect on this logic. The bounded channel is just an unbounded one plus a semaphore. This is so that extremely large capacities don't immediately consume all of your memory.

The channel has one byte of overhead per message, as each 32-element chunk uses 4 pointer-sized fields for bookkeeping.

@barafael
Copy link
Contributor Author

barafael commented Aug 7, 2024

Thanks very much for your detailed response and explanation. Do you think this should be documented, or rather stay implicit, not to be relied upon?

@Darksonn
Copy link
Contributor

Darksonn commented Aug 7, 2024

I would be happy to see things documented. But it will be subject to changes in future releases.

@barafael
Copy link
Contributor Author

barafael commented Aug 7, 2024

I'll do a PR with the info you gave.

barafael added a commit to barafael/tokio that referenced this issue Aug 13, 2024
barafael added a commit to barafael/tokio that referenced this issue Aug 13, 2024
barafael added a commit to barafael/tokio that referenced this issue Aug 14, 2024
barafael added a commit to barafael/tokio that referenced this issue Aug 14, 2024
barafael added a commit to barafael/tokio that referenced this issue Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync T-docs Topic: documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants