iced-rs / iced

A cross-platform GUI library for Rust, inspired by Elm
https://iced.rs
MIT License
22.96k stars 1.06k forks source link

Implement backpressure mechanism for `iced_winit::Proxy` #2389

Closed hecrj closed 1 month ago

hecrj commented 1 month ago

The EventLoopProxy in winit is implemented using an unbounded mpsc channel. In practice, this means that we have to be careful when publishing to this channel, as it will simply keep allocating additional memory when needed. And guess what? We are not careful at all!

Currently, if a Subscription decides to start producing messages very quickly—faster than update can process them—the messages will start piling up in the EventLoopProxy and memory usage will increase indefinitely. Furthermore, this memory will stay allocated as channel capacity once the application catches up and the messages are processed. I suspect this could be the root cause of some high memory usage reports (like https://github.com/squidowl/halloy/issues/309).

The solution implemented in this PR consists in connecting the output of an async, bounded mpsc channel to the EventLoopProxy input while freeing new message slots when published messages are processed by update—effectively achieving backpressure for all of our async abstractions.