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
//! Thread-local string pool for the transformation pipeline.
//!
//! The string pool ([`STRING_POOL`]) reduces allocation churn by recycling `String`
//! buffers across matcher calls within each thread.
//!
//! # Safety model
//!
//! Thread-local statics use `UnsafeCell` with `#[thread_local]` (a nightly feature)
//! to avoid the closure overhead of the `thread_local!` macro. Safety relies on two
//! invariants:
//!
//! 1. `#[thread_local]` guarantees single-threaded access — no data races.
//! 2. No public function in this module is re-entrant: the borrow from `UnsafeCell::get()`
//! is always dropped before any call that could re-enter the same pool.
use UnsafeCell;
/// Maximum number of [`String`] buffers retained in the pool between calls; excess are dropped.
const STRING_POOL_MAX: usize = 128;
/// Pool of reusable [`String`] buffers, one per thread.
///
/// # Safety
///
/// Uses `#[thread_local]` + `UnsafeCell` to eliminate the `thread_local!` macro's
/// `.with()` closure overhead. Single-threaded access is guaranteed by the
/// `#[thread_local]` attribute. No function in this module is re-entrant while the
/// mutable reference from `UnsafeCell::get()` is live.
pub static STRING_POOL: = new;
/// Pops a reusable [`String`] from the thread-local pool, or allocates a new one.
///
/// Returns a cleared `String` with capacity ≥ `capacity`. If a buffer is
/// available in the pool, it is reused: `clear()` is called to reset its
/// length, and `reserve` grows it if its existing capacity falls short.
/// If the pool is empty, a fresh `String::with_capacity(capacity)` is allocated.
///
/// Callers should pass the expected output length as `capacity` to avoid
/// mid-write reallocations. Passing 0 is valid and simply pops an arbitrary-
/// capacity buffer from the pool.
///
/// ```ignore
/// let mut buf = get_string_from_pool(64);
/// buf.push_str("hello");
/// // ... use buf ...
/// return_string_to_pool(buf); // recycle for next call
/// ```
///
/// # Safety
///
/// Accesses [`STRING_POOL`] via `UnsafeCell::get()`. Safe because
/// `#[thread_local]` guarantees single-threaded ownership and this function
/// is not re-entrant while the returned `String` is live.
pub
/// Returns a [`String`] to the thread-local pool for future reuse.
///
/// If the pool already holds [`STRING_POOL_MAX`] buffers, the string is
/// dropped immediately rather than growing the pool without bound. This
/// caps per-thread memory usage while still amortizing allocation costs
/// across the most common working sets.
///
/// Callers should only return buffers that were obtained from
/// [`get_string_from_pool`]; returning arbitrary strings is safe but may
/// leave oversized buffers in the pool.
///
/// # Safety
///
/// Accesses [`STRING_POOL`] via `UnsafeCell::get()`. Safe because
/// `#[thread_local]` guarantees single-threaded ownership and this function
/// is not re-entrant.
pub