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 mpsc example #1

Merged
merged 1 commit into from
Mar 6, 2025
Merged

add mpsc example #1

merged 1 commit into from
Mar 6, 2025

Conversation

matklad
Copy link
Contributor

@matklad matklad commented Mar 6, 2025

A twist on asymmetric, with two changes:

  • there are many producer threads, not one
  • the queue is statically sized, consumer applies backpressure to producers.

This program does O(N) allocations, but has only O(1) peak memory usage.

At the time of writing:

  • smp allocator requires O(N) peak memory
  • debug and c (on MacOS) need O(1), as expected:
λ ps x -o time,rss,command | grep 'mpsc smp'
  0:41.16  44752 ./zig-out/bin/mpsc smp
λ ps x -o time,rss,command | grep 'mpsc smp'
  0:53.26  51472 ./zig-out/bin/mpsc smp
λ ps x -o time,rss,command | grep 'mpsc smp'
  1:09.11  71344 ./zig-out/bin/mpsc smp

λ ps x -o time,rss,command | grep 'mpsc c'
  0:19.76   1616 ./zig-out/bin/mpsc c
λ ps x -o time,rss,command | grep 'mpsc c'
  0:50.78   1616 ./zig-out/bin/mpsc c
λ ps x -o time,rss,command | grep 'mpsc c'
  0:59.46   1616 ./zig-out/bin/mpsc c
λ ps x -o time,rss,command | grep 'mpsc c'
  1:04.80   1728 ./zig-out/bin/mpsc c

A twist on asymmetric, with two changes:

* there are many producer threads, not one
* the queue is statically sized, consumer applies backpressure to
  producers.

This program allocate O(N) memory in total, but it uses O(1) memory at
any given point in time.

At the time of writing:

* smp allocator requires O(N) memory in total
* debug and c (on MacOS) need O(1), as expected:

```
λ ps x -o time,rss,command | grep 'mpsc smp'
  0:41.16  44752 ./zig-out/bin/mpsc smp
λ ps x -o time,rss,command | grep 'mpsc smp'
  0:53.26  51472 ./zig-out/bin/mpsc smp
λ ps x -o time,rss,command | grep 'mpsc smp'
  1:09.11  71344 ./zig-out/bin/mpsc smp

λ ps x -o time,rss,command | grep 'mpsc c'
  0:19.76   1616 ./zig-out/bin/mpsc c
λ ps x -o time,rss,command | grep 'mpsc c'
  0:50.78   1616 ./zig-out/bin/mpsc c
λ ps x -o time,rss,command | grep 'mpsc c'
  0:59.46   1616 ./zig-out/bin/mpsc c
λ ps x -o time,rss,command | grep 'mpsc c'
  1:04.80   1728 ./zig-out/bin/mpsc c
```
@andrewrk
Copy link
Owner

andrewrk commented Mar 6, 2025

Thanks for taking the time to discover and send this, nice find!

@andrewrk andrewrk merged commit 3eae683 into andrewrk:main Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants