Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nfa,dfa: call shrink_to_vec in places
This commit reduces the *actual* heap memory used by noncontiguous NFAs, contiguous NFAs and DFAs. Previously, all three would report memory usage that only included what was actually being used within each Vec. But each Vec might actually have a lot more heap memory inside of it in the form of unused capacity. Potentially ~twice as much. This is because a Vec amortizes its growth. So when Vecs are used to build NFAs and DFAs, some of them wind up with potentially a lot of additional capacity that goes unused and unreported. This commit partially fixes that by calling shrink_to_vec in a few strategic places. However, there are still some nested Vecs, notably in the noncontiguous NFA, that remain unshrinked. I tried doing it, but the perf impact it had on construction was non-trivial. This is a good example of one of those things where you of course know that Vecs amortize their growth by reserving potentially extra capacity that yuo won't end up using, but still remain ignorant of the broader effects of this strategy in aggregate. Especially at the extremes when you start trying to build Aho-Corasick automatons with millions of patterns. We should think more carefully about using linked lists in the non-contiguous NFA to reduce memory usage, or perhaps thinking of some other way to avoid nested Vecs. It turns out that they end up penalizing us quite a bit. This commit was inspired by the investigation I did as part of this issue: G-Research/ahocorasick_rs#62
- Loading branch information