Skip to content

Better memory footprint #137

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

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async-task = "4.4.0"
concurrent-queue = "2.5.0"
fastrand = "2.0.0"
futures-lite = { version = "2.0.0", default-features = false }
pin-project-lite = "0.2"
Copy link
Member

Choose a reason for hiding this comment

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

Noting to myself that this is not a new dependency, since futures-lite imports it already.

slab = "0.4.7"

[target.'cfg(target_family = "wasm")'.dependencies]
Expand Down
35 changes: 30 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@
use std::fmt;
use std::marker::PhantomData;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::pin::Pin;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, RwLock, TryLockError};
use std::task::{Poll, Waker};
use std::task::{Context, Poll, Waker};

use async_task::{Builder, Runnable};
use concurrent_queue::ConcurrentQueue;
use futures_lite::{future, prelude::*};
use pin_project_lite::pin_project;
use slab::Slab;

#[cfg(feature = "static")]
Expand Down Expand Up @@ -245,10 +247,7 @@ impl<'a> Executor<'a> {
let entry = active.vacant_entry();
let index = entry.key();
let state = self.state_as_arc();
let future = async move {
let _guard = CallOnDrop(move || drop(state.active().try_remove(index)));
future.await
};
let future = AsyncCallOnDrop::new(future, move || drop(state.active().try_remove(index)));

// Create the task and register it in the set of active tasks.
//
Expand Down Expand Up @@ -1155,6 +1154,32 @@ impl<F: FnMut()> Drop for CallOnDrop<F> {
}
}

pin_project! {
/// A wrapper around a future, running a closure when dropped.
struct AsyncCallOnDrop<Fut, Cleanup: FnMut()> {
#[pin]
future: Fut,
cleanup: CallOnDrop<Cleanup>,
}
}

impl<Fut, Cleanup: FnMut()> AsyncCallOnDrop<Fut, Cleanup> {
fn new(future: Fut, cleanup: Cleanup) -> Self {
Self {
future,
cleanup: CallOnDrop(cleanup),
}
}
}

impl<Fut: Future, Cleanup: FnMut()> Future for AsyncCallOnDrop<Fut, Cleanup> {
type Output = Fut::Output;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().future.poll(cx)
}
}

fn _ensure_send_and_sync() {
use futures_lite::future::pending;

Expand Down
Loading