1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// The thread's table of the modifier chains of constants that
// `#[composable]` bodies keep.
use std::cell::RefCell;
use cranpose_core::Key;
use super::Modifier;
use crate::thread_state;
// Slots of the table, as a power of two.
const SLOTS: usize = 128;
// The kept chains of the thread, each with the key of the expression that
// made it; empty until the first keep.
pub(crate) type KeptModifiers = RefCell<Vec<Option<(Key, Modifier)>>>;
/// The modifier that `make` gives, for a modifier chain of constants that
/// `#[composable]` found to give the same modifier every time it runs: `key`
/// names the chain's expression. The thread keeps the modifier it made last
/// for `key` in a table of 128 slots, so the chain is made again only when
/// another chain took its slot. It needs no composition: the chain may sit
/// in a closure that runs in an event handler, an effect or another thread.
#[doc(hidden)]
pub fn __kept_modifier(key: Key, make: impl FnOnce() -> Modifier) -> Modifier {
if let Some(modifier) = kept_modifier(key) {
return modifier;
}
let modifier = make();
keep_modifier(key, &modifier);
modifier
}
#[expect(
clippy::cast_possible_truncation,
reason = "the slot is the low bits of the key"
)]
fn slot(key: Key) -> usize {
(key.0 as usize) & (SLOTS - 1)
}
// A copy of the modifier kept for `key`. Out of line and not generic:
// `__kept_modifier` has an instance for each call site, as each passes its
// own closure, so the lookup and the copy would otherwise repeat in each.
#[inline(never)]
fn kept_modifier(key: Key) -> Option<Modifier> {
let mut copy = None;
thread_state::try_with(|state| {
// The borrow spans the copy: every access of the table tries its
// borrow, so a keep that the copy makes only misses.
if let Ok(kept) = state.kept_modifiers.try_borrow()
&& let Some(Some((kept_key, modifier))) = kept.get(slot(key))
&& *kept_key == key
{
copy = Some(modifier.clone());
}
});
copy
}
// Keeps a copy of `modifier` for `key`. Out of line: it runs once for each
// kept chain.
#[inline(never)]
fn keep_modifier(key: Key, modifier: &Modifier) {
let taken = thread_state::try_with(|state| {
let Ok(mut kept) = state.kept_modifiers.try_borrow_mut() else {
return None;
};
if kept.is_empty() {
kept.resize_with(SLOTS, || None);
}
let entry = kept.get_mut(slot(key))?;
entry.replace((key, modifier.clone()))
});
// The chain that held the slot drops after the borrow ends, as its drop
// may reach the table.
drop(taken);
}