Skip to content

Commit 02a62ba

Browse files
committed
automata: call Vec::shrink_to_fit in a few strategic places
Other parts of `regex-automata` do this implicitly by using `Box<[T]>`, btu it's not always straight-forward to use `Box<[T]>`. (Or, at least, non-annoying.) In some of those cases here, we call `Vec::shrink_to_fit` to decrease memory usage. These are probably the biggest offenders, but I didn't do a thorough investigation here. Fixes #1297
1 parent a76e0a0 commit 02a62ba

File tree

6 files changed

+23
-0
lines changed

6 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
1.11.3 (TBD)
2+
============
3+
TODO
4+
5+
Improvements:
6+
7+
* [BUG #1297](https://github.com/rust-lang/regex/issues/1297):
8+
Improve memory usage by trimming excess memory capacity in some spots.
9+
10+
111
1.11.2 (2025-08-24)
212
===================
313
This is a new patch release of `regex` with some minor fixes. A larger number

regex-automata/src/dfa/dense.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,10 @@ impl Builder {
12741274
}
12751275
// Look for and set the universal starting states.
12761276
dfa.set_universal_starts();
1277+
dfa.tt.table.shrink_to_fit();
1278+
dfa.st.table.shrink_to_fit();
1279+
dfa.ms.slices.shrink_to_fit();
1280+
dfa.ms.pattern_ids.shrink_to_fit();
12771281
Ok(dfa)
12781282
}
12791283

regex-automata/src/dfa/onepass.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ impl<'a> InternalBuilder<'a> {
722722
}
723723
}
724724
self.shuffle_states();
725+
self.dfa.starts.shrink_to_fit();
726+
self.dfa.table.shrink_to_fit();
725727
Ok(self.dfa)
726728
}
727729

regex-automata/src/dfa/sparse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ impl DFA<Vec<u8>> {
393393
new_state.set_next_at(i, next);
394394
}
395395
}
396+
new.tt.sparse.shrink_to_fit();
397+
new.st.table.shrink_to_fit();
396398
debug!(
397399
"created sparse DFA, memory usage: {} (dense memory usage: {})",
398400
new.memory_usage(),

regex-automata/src/nfa/thompson/nfa.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,8 @@ impl Inner {
13381338
self.look_set_prefix_any =
13391339
self.look_set_prefix_any.union(prefix_any);
13401340
}
1341+
self.states.shrink_to_fit();
1342+
self.start_pattern.shrink_to_fit();
13411343
NFA(Arc::new(self))
13421344
}
13431345

regex-automata/src/util/captures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,9 @@ impl GroupInfo {
16061606
}
16071607
}
16081608
group_info.fixup_slot_ranges()?;
1609+
group_info.slot_ranges.shrink_to_fit();
1610+
group_info.name_to_index.shrink_to_fit();
1611+
group_info.index_to_name.shrink_to_fit();
16091612
Ok(GroupInfo(Arc::new(group_info)))
16101613
}
16111614

0 commit comments

Comments
 (0)