Skip to content

Commit

Permalink
impl: substantially reduce regex stack size
Browse files Browse the repository at this point in the history
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.

Fixes #750, Fixes #751

Ref servo/servo#28269
  • Loading branch information
BurntSushi committed Mar 14, 2021
1 parent 951b8b9 commit 4c9ba9d
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ pub struct Exec {
/// All read only state.
ro: Arc<ExecReadOnly>,
/// A pool of reusable values for the various matching engines.
pool: Pool<ProgramCache>,
///
/// Note that boxing this value is not strictly necessary, but it is an
/// easy way to ensure that T does not bloat the stack sized used by a pool
/// in the case where T is big. And this turns out to be the case at the
/// time of writing for regex's use of this pool. At the time of writing,
/// the size of a Regex on the stack is 856 bytes. Boxing this value
/// reduces that size to 16 bytes.
pool: Box<Pool<ProgramCache>>,
}

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

fn new_pool(ro: &Arc<ExecReadOnly>) -> Pool<ProgramCache> {
fn new_pool(ro: &Arc<ExecReadOnly>) -> Box<Pool<ProgramCache>> {
let ro = ro.clone();
Pool::new(Box::new(move || {
Box::new(Pool::new(Box::new(move || {
AssertUnwindSafe(RefCell::new(ProgramCacheInner::new(&ro)))
}))
})))
}
}

Expand Down

0 comments on commit 4c9ba9d

Please sign in to comment.