Details
-
Suggestion
-
Resolution: Unresolved
-
Not Evaluated
-
None
-
5.15.12, 6.6.3, 6.9.3
-
None
-
I've tested on Windows 10 with MSVC 2022 on an R9 5900X.
Description
When using `QDirListing`, `QDirIterator`, or `QDir::entryList()` with name filters, they're converted from wildcards to `QRegularExpression`, and then compiled when matched against the first filename. When only iterating over a single directory or a recursive directory structure, this is fine. However, when iterating over multiple separate directories, because the regular expression object is owned by the `QDirListing` and there's no way to reuse it, the same regular expression gets created and compiled over and over. For one of the applications I'm working on, that adds up to hundreds of milliseconds during startup according to my profiler.
Generally, it's advised to reuse `QRegularExpression` where possible for this reason, but there's no way to do that here.
Possible quick fixes include:
- Being able to reuse the same `QDirListing` instance for multiple directories.
- Adding a new class like `QDirListing`, but which is reusable.
- Allowing `QDirListing` to be constructed with `QRegularExpression` filters directly so they can be precompiled.
- Compiling the `QRegularExpression` instances in `QRegularExpression::createFromWildcard` and storing them in an LRU cache, then returning copies from that cache which share the same compiled code (this is how copying `QRegularExpression` already works). This doesn't mean caching any `QRegularExpression` instances in other circumstances, just this one function that Qt uses for throwaway regular expressions.