relate-core 1.5.0

Core values, errors, and IDs for Relate
Documentation
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! The shared lazy load worker pool and the per-open transient memory tracker.
//!
//! One process-wide pool of persistent worker slots serves every open's load
//! phase. A load scope ([`with_load_scope`]) fixes how many slots (`k`) that
//! open may use and which [`OpenTransientTracker`] its transient allocations
//! are charged to; [`run_load_tasks`] then fans tasks out onto slots `0..k`
//! only, so slots are spawned on demand and reused across opens. Opens that
//! share a slot never share a tracker. On wasm32 there are no threads and
//! [`run_load_tasks`] runs its tasks sequentially.

use std::cell::RefCell;
use std::ffi::OsStr;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use crate::{Error, Result};

/// The environment variable that sets the load thread count when the
/// `load_threads` option is absent.
const LOAD_THREADS_ENV: &str = "RELATE_LOAD_THREADS";

/// How many load worker slots one open may use (always at least one).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LoadThreads(NonZeroUsize);

impl LoadThreads {
    /// The thread count as a plain number (>= 1).
    pub fn get(self) -> usize {
        self.0.get()
    }
}

/// Resolve the load thread count: an explicit `load_threads` wins, then
/// `RELATE_LOAD_THREADS`, then the machine's available parallelism. Zero,
/// negative, non-integer and non-UTF-8 values are rejected; an unset or
/// blank environment variable counts as absent.
pub fn resolve_load_threads(load_threads: Option<usize>) -> Result<LoadThreads> {
    resolve(load_threads, std::env::var_os(LOAD_THREADS_ENV).as_deref())
}

fn resolve(load_threads: Option<usize>, env: Option<&OsStr>) -> Result<LoadThreads> {
    let requested = match (load_threads, env) {
        (Some(n), _) => NonZeroUsize::new(n)
            .ok_or_else(|| Error::semantic("invalid load_threads: must be positive"))?,
        (None, Some(raw)) => {
            let text = raw.to_str().ok_or_else(|| {
                Error::semantic(format!("invalid {LOAD_THREADS_ENV}: not valid UTF-8"))
            })?;
            match text.trim() {
                "" => available(),
                text => text.parse::<NonZeroUsize>().map_err(|_| {
                    Error::semantic(format!(
                        "invalid {LOAD_THREADS_ENV} '{text}': must be a positive integer"
                    ))
                })?,
            }
        }
        (None, None) => available(),
    };
    Ok(LoadThreads(requested))
}

fn available() -> NonZeroUsize {
    std::thread::available_parallelism().unwrap_or(NonZeroUsize::MIN)
}

/// Peak transient memory of one open: bytes currently leased and the
/// high-water mark of that sum.
#[derive(Debug, Default)]
pub struct OpenTransientTracker {
    current: AtomicU64,
    peak: AtomicU64,
}

impl OpenTransientTracker {
    /// A tracker with nothing leased.
    pub fn new() -> Self {
        Self::default()
    }

    /// The most bytes that were ever leased at once.
    pub fn peak_bytes(&self) -> u64 {
        self.peak.load(Ordering::Relaxed)
    }

    fn lease(self: &Arc<Self>, bytes: u64) -> TransientLease {
        let now = self.current.fetch_add(bytes, Ordering::Relaxed) + bytes;
        self.peak.fetch_max(now, Ordering::Relaxed);
        TransientLease {
            tracker: Some(Arc::clone(self)),
            bytes,
        }
    }
}

/// Bytes charged to an open's tracker until this guard drops. Inert when
/// taken outside a load scope.
#[derive(Debug)]
pub struct TransientLease {
    tracker: Option<Arc<OpenTransientTracker>>,
    bytes: u64,
}

impl Drop for TransientLease {
    fn drop(&mut self) {
        if let Some(tracker) = &self.tracker {
            tracker.current.fetch_sub(self.bytes, Ordering::Relaxed);
        }
    }
}

/// Charge `bytes` of transient memory to the tracker of the load scope the
/// calling thread is in (load workers inherit their caller's scope).
pub fn record_transient_lease(bytes: u64) -> TransientLease {
    match SCOPE.with(|scope| scope.borrow().as_ref().map(|s| Arc::clone(&s.tracker))) {
        Some(tracker) => tracker.lease(bytes),
        None => TransientLease {
            tracker: None,
            bytes,
        },
    }
}

#[derive(Clone)]
struct Scope {
    threads: LoadThreads,
    tracker: Arc<OpenTransientTracker>,
}

thread_local! {
    static SCOPE: RefCell<Option<Scope>> = const { RefCell::new(None) };
}

/// Restores the previous scope of the thread on drop.
struct ScopeGuard(Option<Scope>);

fn enter(scope: Scope) -> ScopeGuard {
    ScopeGuard(SCOPE.with(|cell| cell.borrow_mut().replace(scope)))
}

impl Drop for ScopeGuard {
    fn drop(&mut self) {
        SCOPE.with(|cell| *cell.borrow_mut() = self.0.take());
    }
}

/// Run `f` as one open's load phase: tasks it fans out through
/// [`run_load_tasks`] use at most `threads` pool slots and charge their
/// transient leases to `tracker`.
pub fn with_load_scope<R>(
    threads: LoadThreads,
    tracker: Arc<OpenTransientTracker>,
    f: impl FnOnce() -> R,
) -> R {
    let _scope = enter(Scope { threads, tracker });
    f()
}

/// The load thread count of the scope the calling thread is in; one outside
/// any scope.
pub fn current_load_threads() -> LoadThreads {
    SCOPE
        .with(|scope| scope.borrow().as_ref().map(|s| s.threads))
        .unwrap_or(LoadThreads(NonZeroUsize::MIN))
}

/// Run `tasks` and return their results in order. Inside a load scope with
/// more than one thread the tasks run on the pool's slots (task `i` on slot
/// `i % min(k, tasks)`); otherwise, and for calls made from a load worker,
/// they run inline on the calling thread. The tasks may borrow from the
/// caller's stack: this returns only once every task has finished. A task's
/// panic is resumed on the caller after the rest have finished.
pub fn run_load_tasks<T: Send, F: FnOnce() -> T + Send>(tasks: Vec<F>) -> Vec<T> {
    #[cfg(not(target_arch = "wasm32"))]
    {
        let scope = SCOPE.with(|scope| scope.borrow().clone());
        if let Some(scope) = scope.filter(|s| s.threads.get() > 1 && tasks.len() > 1) {
            if !native::on_worker() {
                return native::POOL.run(&scope, tasks);
            }
        }
    }
    tasks.into_iter().map(|task| task()).collect()
}

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(PoisonError::into_inner)
}

#[cfg(not(target_arch = "wasm32"))]
mod native {
    use std::cell::Cell;
    use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
    use std::sync::{mpsc, Condvar};
    use std::thread;

    use super::*;

    type Job = Box<dyn FnOnce() + Send + 'static>;

    thread_local! {
        static ON_WORKER: Cell<bool> = const { Cell::new(false) };
    }

    pub(super) static POOL: LoadPool = LoadPool {
        slots: Mutex::new(Vec::new()),
    };

    pub(super) fn on_worker() -> bool {
        ON_WORKER.get()
    }

    /// Persistent worker threads, one per slot, spawned on first use.
    pub(super) struct LoadPool {
        pub(super) slots: Mutex<Vec<mpsc::Sender<Job>>>,
    }

    /// The results of one `run`, and how many of its jobs are unfinished.
    struct Batch<T> {
        state: Mutex<BatchState<T>>,
        done: Condvar,
    }

    struct BatchState<T> {
        pending: usize,
        results: Vec<Option<thread::Result<T>>>,
    }

    impl<T> Batch<T> {
        fn finish(&self, index: usize, result: thread::Result<T>) {
            let mut state = lock(&self.state);
            state.results[index] = Some(result);
            state.pending -= 1;
            if state.pending == 0 {
                self.done.notify_all();
            }
        }
    }

    /// Blocks on drop until the batch has no unfinished job, including while
    /// `run` unwinds, so no job outlives the borrows its task captured.
    struct WaitOnDrop<'a, T>(&'a Batch<T>);

    impl<T> Drop for WaitOnDrop<'_, T> {
        fn drop(&mut self) {
            let mut state = lock(&self.0.state);
            while state.pending > 0 {
                state = self
                    .0
                    .done
                    .wait(state)
                    .unwrap_or_else(PoisonError::into_inner);
            }
        }
    }

    impl LoadPool {
        fn sender(&self, slot: usize) -> mpsc::Sender<Job> {
            let mut slots = lock(&self.slots);
            while slots.len() <= slot {
                let (tx, rx) = mpsc::channel::<Job>();
                thread::Builder::new()
                    .name(format!("relate-load-{}", slots.len()))
                    .spawn(move || {
                        ON_WORKER.set(true);
                        rx.into_iter().for_each(|job| job());
                    })
                    .expect("spawn load worker");
                slots.push(tx);
            }
            slots[slot].clone()
        }

        pub(super) fn run<'a, T: Send + 'a, F: FnOnce() -> T + Send + 'a>(
            &self,
            scope: &Scope,
            tasks: Vec<F>,
        ) -> Vec<T> {
            let lanes = scope.threads.get().min(tasks.len());
            let batch = Arc::new(Batch {
                state: Mutex::new(BatchState {
                    pending: 0,
                    results: tasks.iter().map(|_| None).collect(),
                }),
                done: Condvar::new(),
            });
            let wait = WaitOnDrop(&batch);
            for (index, task) in tasks.into_iter().enumerate() {
                let sender = self.sender(index % lanes);
                let (done, scope) = (Arc::clone(&batch), scope.clone());
                let job: Box<dyn FnOnce() + Send + 'a> = Box::new(move || {
                    let _scope = enter(scope);
                    let result = catch_unwind(AssertUnwindSafe(task));
                    done.finish(index, result);
                });
                // SAFETY: `wait` blocks until every sent job has called
                // `finish`, which happens after the task (and everything it
                // borrowed) is consumed, so no job outlives `'a`.
                let job: Job = unsafe { std::mem::transmute(job) };
                lock(&batch.state).pending += 1;
                if sender.send(job).is_err() {
                    lock(&batch.state).pending -= 1;
                    panic!("load worker pool is unavailable");
                }
            }
            drop(wait);
            let results = std::mem::take(&mut lock(&batch.state).results);
            results
                .into_iter()
                .map(|result| match result.expect("every load task finished") {
                    Ok(value) => value,
                    Err(panic) => resume_unwind(panic),
                })
                .collect()
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;
    use std::ffi::OsString;
    use std::sync::atomic::AtomicUsize;
    use std::sync::Barrier;
    use std::thread::{self, ThreadId};

    use super::*;

    fn threads(n: usize) -> LoadThreads {
        resolve_load_threads(Some(n)).unwrap()
    }

    fn scope_with<R>(k: usize, tracker: &Arc<OpenTransientTracker>, f: impl FnOnce() -> R) -> R {
        with_load_scope(threads(k), Arc::clone(tracker), f)
    }

    fn thread_ids(count: usize) -> Vec<(ThreadId, String)> {
        run_load_tasks(
            (0..count)
                .map(|_| {
                    || {
                        (
                            thread::current().id(),
                            thread::current().name().unwrap().to_owned(),
                        )
                    }
                })
                .collect(),
        )
    }

    #[test]
    fn load_pool_resolve_rejects_bad_values() {
        let semantic = |r: Result<LoadThreads>| match r.unwrap_err() {
            Error::Semantic(msg) => msg,
            other => panic!("not semantic: {other:?}"),
        };
        assert!(semantic(resolve(Some(0), None)).contains("load_threads"));
        let mut bad = vec![OsString::from("0"), "-1".into(), "two".into(), "1.5".into()];
        #[cfg(unix)]
        bad.push(std::os::unix::ffi::OsStringExt::from_vec(vec![0x66, 0xff]));
        for raw in bad {
            let msg = semantic(resolve(None, Some(&raw)));
            assert!(msg.contains("RELATE_LOAD_THREADS"), "{raw:?}: {msg}");
        }
    }

    #[test]
    fn load_pool_resolve_accepts_values_and_defaults() {
        let parallelism = std::thread::available_parallelism().unwrap().get();
        assert_eq!(resolve(Some(3), None).unwrap().get(), 3);
        assert_eq!(resolve(Some(3), Some(OsStr::new("9"))).unwrap().get(), 3);
        assert_eq!(resolve(None, Some(OsStr::new(" 4 "))).unwrap().get(), 4);
        assert_eq!(resolve(None, None).unwrap().get(), parallelism);
        assert_eq!(
            resolve(None, Some(OsStr::new("  "))).unwrap().get(),
            parallelism
        );
    }

    #[test]
    fn load_pool_slots_are_reused_and_capped_by_scope() {
        let seen: Mutex<HashSet<(ThreadId, String)>> = Mutex::default();
        thread::scope(|s| {
            for _ in 0..4 {
                s.spawn(|| {
                    let tracker = Arc::default();
                    for _ in 0..5 {
                        let ids = scope_with(2, &tracker, || thread_ids(6));
                        lock(&seen).extend(ids);
                    }
                });
            }
        });
        let seen = lock(&seen);
        assert!(seen.len() <= 2, "{seen:?}");
        assert!(seen
            .iter()
            .all(|(_, name)| name == "relate-load-0" || name == "relate-load-1"));
        assert!(lock(&native::POOL.slots).len() <= 2);
    }

    #[test]
    fn load_pool_borrows_stack_and_resumes_panics_after_all_finish() {
        let finished = AtomicUsize::new(0);
        let data = [1_u64, 2, 3, 4];
        let tracker = Arc::default();
        let sums = scope_with(2, &tracker, || {
            run_load_tasks(data.iter().map(|n| || *n * 10).collect())
        });
        assert_eq!(sums, [10, 20, 30, 40]);

        let outcome = std::panic::catch_unwind(|| {
            scope_with(2, &tracker, || {
                run_load_tasks(
                    (0..4)
                        .map(|i| {
                            let finished = &finished;
                            move || {
                                if i == 0 {
                                    panic!("boom");
                                }
                                thread::sleep(std::time::Duration::from_millis(30));
                                finished.fetch_add(1, Ordering::SeqCst);
                            }
                        })
                        .collect(),
                )
            })
        });
        assert_eq!(*outcome.unwrap_err().downcast::<&str>().unwrap(), "boom");
        assert_eq!(finished.load(Ordering::SeqCst), 3);
        // The pool survives a panicking task.
        assert_eq!(scope_with(2, &tracker, || thread_ids(2)).len(), 2);
    }

    #[test]
    fn load_pool_nested_calls_run_inline_on_the_worker() {
        let tracker = Arc::default();
        let nested = scope_with(2, &tracker, || {
            run_load_tasks(
                (0..2)
                    .map(|_| {
                        || {
                            let outer = thread::current().id();
                            let inner = run_load_tasks(vec![|| thread::current().id(); 3]);
                            (outer, inner, current_load_threads().get())
                        }
                    })
                    .collect(),
            )
        });
        for (outer, inner, k) in nested {
            assert_eq!(inner, [outer; 3]);
            assert_eq!(k, 2);
        }
        assert_eq!(current_load_threads().get(), 1);
    }

    #[test]
    fn load_pool_tracker_peaks_are_isolated_per_scope() {
        let trackers: Vec<Arc<OpenTransientTracker>> = (0..2).map(|_| Arc::default()).collect();
        thread::scope(|s| {
            for (tracker, bytes) in trackers.iter().zip([100_u64, 7]) {
                s.spawn(move || {
                    let barrier = Barrier::new(2);
                    scope_with(2, tracker, || {
                        run_load_tasks(
                            (0..2)
                                .map(|_| {
                                    let barrier = &barrier;
                                    move || {
                                        let _lease = record_transient_lease(bytes);
                                        barrier.wait();
                                    }
                                })
                                .collect(),
                        )
                    });
                });
            }
        });
        assert_eq!(trackers[0].peak_bytes(), 200);
        assert_eq!(trackers[1].peak_bytes(), 14);
        assert_eq!(trackers[0].current.load(Ordering::Relaxed), 0);
        drop(record_transient_lease(5)); // outside a scope: inert
    }
}