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
//! A small pool of [`LazyDfa`] instances, one per concurrent search.
//!
//! A lazy DFA builds its states on demand, so a search needs `&mut` access to
//! the state cache. Sharing one instance behind a lock therefore makes
//! concurrent searches on a single `Regex` contend for the whole duration of
//! every search. Handing each search its own instance removes the contention:
//! the pool lock is held only to take an instance out and to put it back.
//!
//! The cache is only a cache — every instance runs the same subset
//! construction over the same NFA — so pooling changes which instance computes
//! a state, never what that state is.
use Mutex;
use crateLazyDfa;
/// How many idle instances the pool keeps.
///
/// Each cached DFA can grow to its own cache limit in states, so the pool is
/// capped rather than left to grow with the peak thread count: a burst of
/// threads would otherwise leave that memory retained for the lifetime of the
/// `Regex`. Eight covers the common case of a handful of worker threads
/// without keeping the cache of a thread that ran once; past it, a search
/// still gets a correct instance by cloning the template, and drops it on
/// completion.
const MAX_IDLE: usize = 8;
/// Hands out [`LazyDfa`] instances for the duration of a single search.
pub