Skip to content

Commit 081d430

Browse files
committed
impl: substantially reduce regex stack size
This commit fixes a fairly large regression in the stack size of a Regex introduced in regex 1.4.4. When I dropped thread_local and replaced it with Pool, it turned out that Pool inlined a T into its struct and a Regex in turn had Pool inlined into itself. It further turns out that the T=ProgramCache is itself quite large. We fix this by introducing an indirection in the inner regex type. That is, we use a Box<Pool> instead of a Pool. This shrinks the size of a Regex from 856 bytes to 16 bytes. Interestingly, prior to regex 1.4.4, a Regex was still quite substantial in size, coming in at around 552 bytes. So it looks like the 1.4.4 release didn't dramatically increase it, but it increased it enough that folks started experiencing real problems: stack overflows. Since indirection can lead to worse locality and performance loss, I did run the benchmark suite. I couldn't see any measurable difference. This is generally what I would expect. This is an indirection at a fairly high level. There's lots of other indirection already, and this indirection isn't accessed in a hot path. (The regex cache itself is of course used in hot paths, but by the time we get there, we have already followed this particular pointer.) We also include a regression test that asserts a Regex (and company) are 16 bytes in size. While this isn't an API guarantee, it at least means that increasing the size of Regex will be an intentional thing in the future and not an accidental leakage of implementation details. Fixes #750, Fixes #751 Ref servo/servo#28269
1 parent 951b8b9 commit 081d430

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
1.4.5 (2021-03-14)
2+
==================
3+
This is a small patch release that fixes a regression in the size of a `Regex`
4+
in the 1.4.4 release. Prior to 1.4.4, a `Regex` was 552 bytes. In the 1.4.4
5+
release, it was 856 bytes due to internal changes. In this release, a `Regex`
6+
is now 16 bytes. In general, the size of a `Regex` was never something that was
7+
on my radar, but this increased size in the 1.4.4 release seems to have crossed
8+
a threshold and resulted in stack overflows in some programs.
9+
10+
* [BUG #750](https://github.com/rust-lang/regex/pull/750):
11+
Fixes stack overflows seemingly caused by a large `Regex` size by decreasing
12+
its size.
13+
14+
115
1.4.4 (2021-03-11)
216
==================
317
This is a small patch release that contains some bug fixes. Notably, it also

src/exec.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ pub struct Exec {
3636
/// All read only state.
3737
ro: Arc<ExecReadOnly>,
3838
/// A pool of reusable values for the various matching engines.
39-
pool: Pool<ProgramCache>,
39+
///
40+
/// Note that boxing this value is not strictly necessary, but it is an
41+
/// easy way to ensure that T does not bloat the stack sized used by a pool
42+
/// in the case where T is big. And this turns out to be the case at the
43+
/// time of writing for regex's use of this pool. At the time of writing,
44+
/// the size of a Regex on the stack is 856 bytes. Boxing this value
45+
/// reduces that size to 16 bytes.
46+
pool: Box<Pool<ProgramCache>>,
4047
}
4148

4249
/// `ExecNoSync` is like `Exec`, except it embeds a reference to a cache. This
@@ -1446,11 +1453,11 @@ impl ExecReadOnly {
14461453
lcs_len >= 3 && lcs_len > self.dfa.prefixes.lcp().char_len()
14471454
}
14481455

1449-
fn new_pool(ro: &Arc<ExecReadOnly>) -> Pool<ProgramCache> {
1456+
fn new_pool(ro: &Arc<ExecReadOnly>) -> Box<Pool<ProgramCache>> {
14501457
let ro = ro.clone();
1451-
Pool::new(Box::new(move || {
1458+
Box::new(Pool::new(Box::new(move || {
14521459
AssertUnwindSafe(RefCell::new(ProgramCacheInner::new(&ro)))
1453-
}))
1460+
})))
14541461
}
14551462
}
14561463

tests/test_default.rs

+15
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,18 @@ fn oibits_regression() {
136136

137137
let _ = panic::catch_unwind(|| Regex::new("a").unwrap());
138138
}
139+
140+
// See: https://github.com/rust-lang/regex/issues/750
141+
#[test]
142+
#[cfg(target_pointer_width = "64")]
143+
fn regex_is_reasonably_small() {
144+
use std::mem::size_of;
145+
146+
use regex::bytes;
147+
use regex::{Regex, RegexSet};
148+
149+
assert_eq!(16, size_of::<Regex>());
150+
assert_eq!(16, size_of::<RegexSet>());
151+
assert_eq!(16, size_of::<bytes::Regex>());
152+
assert_eq!(16, size_of::<bytes::RegexSet>());
153+
}

0 commit comments

Comments
 (0)