Skip to content

Performance Improvements and Refactoring: Replace flume with crossbeam SegQueue - #376

Open
fereidani wants to merge 4 commits into
monoio-rs:masterfrom
fereidani:master
Open

Performance Improvements and Refactoring: Replace flume with crossbeam SegQueue#376
fereidani wants to merge 4 commits into
monoio-rs:masterfrom
fereidani:master

Conversation

@fereidani

@fereidani fereidani commented Nov 12, 2025

Copy link
Copy Markdown

Hi,
This PR improves performance in various parts of the code and includes some refactoring.

Main changes are:

  1. In the iouring driver, I removed two heap allocations, which simplifies the implementation and should slightly improve performance. (will send it in separate PR with correct Pin usage)
  2. I removed flume. As the creator of the kanal, I respect the flume project as a safe channel implementation, but in this library, the channel structure is unnecessary and not required since it is effectively being used as a queue. Queues always outperform channels because they are simpler structures with fewer features. Using the lock-free crossbeam unbounded SegQueue should improve performance under high contention, as the use case does not require blocking recv, which is a channel feature.
  3. Slight refactoring and improvements in other parts of project.

@CLAassistant

CLAassistant commented Nov 12, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR aims to improve performance by removing heap allocations in the iouring driver and replacing the flume channel with crossbeam_queue::SegQueue for better lock-free performance under high contention. However, there are critical memory safety issues that must be addressed before merging.

Key Changes:

  • Replace flume channel with crossbeam_queue::SegQueue for cross-thread waker communication
  • Attempt to eliminate heap allocations in iouring driver by replacing Box::leak with UnsafeCell fields
  • Update naming from waker_sender to waker_queue throughout the codebase

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
monoio/tests/tcp_connect.rs Removes unused LazyLock import
monoio/src/utils/ctrlc.rs Changes memory deallocation from Box::from_raw to drop_in_place (introduces memory leak)
monoio/src/runtime.rs Replaces flume Sender with Arc in waker cache
monoio/src/driver/uring/mod.rs Replaces Box::leak allocations with UnsafeCell fields and switches to SegQueue (introduces critical memory safety bug)
monoio/src/driver/thread.rs Updates global registry functions to use SegQueue instead of flume Sender
monoio/src/driver/legacy/mod.rs Updates legacy driver to use SegQueue instead of flume
monoio/Cargo.toml Removes flume dependency and adds crossbeam-queue

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread monoio/src/utils/ctrlc.rs Outdated
Comment thread monoio/src/driver/uring/mod.rs Outdated
@fereidani fereidani changed the title Performance Improvements and Refactoring: Remove Heap Allocations in iouring Driver and Replace flume with crossbeam SegQueue Performance Improvements and Refactoring: Replace flume with crossbeam SegQueue Nov 13, 2025
@ihciah

ihciah commented Jan 8, 2026

Copy link
Copy Markdown
Member

Thanks for the improvement!
Concern: Switching from flume channels to Arc<SegQueue> removes the “receiver dropped → send fails” semantics. SegQueue has no close signal, and Context caches the Arc, so even after unregister_waker_queue other threads can still push into a queue that will never be drained. If the target runtime/driver is dropped, this can lead to unbounded accumulation (potential memory/task leak), which is a behavioral regression vs the previous flume sender.
Consider caching Weak and upgrade before pushing, adding a generation/alive flag, or preserving channel semantics. Does this make sense?
BTW, is there any rough reference data regarding the performance improvement when switching from channels to queues? Thank you!

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.

4 participants