Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ All notable changes to this project will be documented in this file.
* Retry spurious atomic failures when completing `Once` initialization. ([#126](https://github.com/fast/mea/pull/126))
* Make cloning a `WaitGroup` panic on counter overflow instead of silently losing track of a handle.
* Align `Condvar` with standard condition-variable semantics by notifying only current waiters and passing a cancelled `notify_one` wakeup to another current waiter instead of storing a permit.
* Clean up uninitialized `OnceMap` and `singleflight` entries once no callers remain after a failure, panic, or cancellation.

### Improvements

* Remove unnecessary `Clone` bounds from `singleflight` keys and custom hashers used by keyed once primitives.

## v0.6.5 (2026-07-30)

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mea/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
hashbrown = { version = "0.17.1", default-features = false, features = [
"inline-more",
] }
slab = { version = "0.4.11" }

[dev-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions mea/benches/primitives/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

mod once_map;
mod oneshot;
mod singleflight;
mod support;

fn main() {
divan::main();
Expand Down
69 changes: 69 additions & 0 deletions mea/benches/primitives/once_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024 tison <wander4096@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use divan::Bencher;
use divan::black_box;
use mea::once::OnceMap;

use super::support::defer_input_drop;
use super::support::noop_context;
use super::support::poll_ready;

const CACHED_ENTRY_COUNTS: &[usize] = &[0, 64, 1024];

#[divan::bench]
fn compute_vacant(bencher: Bencher) {
let mut context = noop_context();
bencher
.with_inputs(OnceMap::<usize, usize>::new)
.bench_local_values(|map| {
let result = black_box(poll_ready(
map.compute(black_box(0), || async { black_box(1) }),
&mut context,
));
defer_input_drop(map, result)
});
}

#[divan::bench]
fn compute_occupied(bencher: Bencher) {
let mut context = noop_context();
bencher
.with_inputs(|| [(0, 1)].into_iter().collect::<OnceMap<_, _>>())
.bench_local_values(|map| {
let result = black_box(poll_ready(
map.compute(black_box(0), || async { black_box(2) }),
&mut context,
));
defer_input_drop(map, result)
});
}

#[divan::bench(args = CACHED_ENTRY_COUNTS)]
fn try_compute_error(bencher: Bencher, cached_entries: usize) {
let mut context = noop_context();
bencher
.with_inputs(|| {
(0..cached_entries)
.map(|key| (key, key))
.collect::<OnceMap<_, _>>()
})
.bench_local_values(|map| {
let result = black_box(poll_ready(
map.try_compute(black_box(usize::MAX), || async { Err::<usize, ()>(()) }),
&mut context,
));
defer_input_drop(map, result)
});
}
49 changes: 49 additions & 0 deletions mea/benches/primitives/singleflight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 tison <wander4096@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use divan::Bencher;
use divan::black_box;
use mea::singleflight::Group;

use super::support::defer_input_drop;
use super::support::noop_context;
use super::support::poll_ready;

#[divan::bench]
fn work_ready(bencher: Bencher) {
let mut context = noop_context();
bencher
.with_inputs(Group::<usize, usize>::new)
.bench_local_values(|group| {
let result = black_box(poll_ready(
group.work(black_box(0), || async { black_box(1) }),
&mut context,
));
defer_input_drop(group, result)
});
}

#[divan::bench]
fn try_work_error(bencher: Bencher) {
let mut context = noop_context();
bencher
.with_inputs(Group::<usize, usize>::new)
.bench_local_values(|group| {
let result = black_box(poll_ready(
group.try_work(black_box(0), || async { Err::<usize, ()>(()) }),
&mut context,
));
defer_input_drop(group, result)
});
}
37 changes: 37 additions & 0 deletions mea/benches/primitives/support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 tison <wander4096@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::future::Future;
use std::pin::pin;
use std::task::Context;
use std::task::Poll;
use std::task::Waker;

pub(super) fn noop_context() -> Context<'static> {
Context::from_waker(Waker::noop())
}

pub(super) fn poll_ready<F: Future>(future: F, context: &mut Context<'_>) -> F::Output {
let mut future = pin!(future);
match future.as_mut().poll(context) {
Poll::Ready(output) => output,
Poll::Pending => panic!("benchmark future should be ready"),
}
}

// Move the input into the benchmark output so Divan drops it outside the timed section.
#[inline]
pub(super) fn defer_input_drop<I, O>(input: I, output: O) -> (I, O) {
(input, output)
}
12 changes: 1 addition & 11 deletions mea/src/admission/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,15 @@
// limitations under the License.

use std::collections::hash_map::DefaultHasher;
use std::future::Future;
use std::hash::BuildHasherDefault;
use std::pin::Pin;
use std::pin::pin;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::task::Context;
use std::task::Poll;
use std::task::Waker;

use super::FairShare;

fn poll_once<F>(future: Pin<&mut F>) -> Poll<F::Output>
where
F: Future,
{
future.poll(&mut Context::from_waker(Waker::noop()))
}
use crate::poll_once;

#[test]
#[should_panic(expected = "FairShare requires at least one permit")]
Expand Down
12 changes: 1 addition & 11 deletions mea/src/condvar/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
use std::task::Waker;

use tokio::task::JoinHandle;

use crate::condvar::Condvar;
use crate::mutex::Mutex;
use crate::poll_once;
use crate::test_runtime;

fn poll_once<F>(future: Pin<&mut F>) -> Poll<F::Output>
where
F: Future,
{
future.poll(&mut Context::from_waker(Waker::noop()))
}

fn expect_ready<T>(poll: Poll<T>) -> T {
match poll {
Poll::Ready(value) => value,
Expand Down
3 changes: 3 additions & 0 deletions mea/src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
mod countdown;
pub(crate) use countdown::*;

mod once_table;
pub(crate) use once_table::*;

mod mutex;
pub(crate) use mutex::*;

Expand Down
Loading