From f4a639e07891c30dd284f0e434ff046dc81647cc Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 3 Mar 2021 20:33:02 +1100 Subject: [PATCH] Re-use the Regex VM when completing filenames to reduce allocations By re-using the VM we avoid re-allocating the threads and saves buffers over and over again. We can just re-use the ones from the previous matching. --- src/file.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/file.cc b/src/file.cc index 7ad6f48d..3424afef 100644 --- a/src/file.cc +++ b/src/file.cc @@ -512,14 +512,20 @@ CandidateList complete_filename(StringView prefix, const Regex& ignored_regex, auto [dirname, fileprefix] = split_path(prefix); auto parsed_dirname = parse_filename(dirname); - const bool check_ignored_regex = not ignored_regex.empty() and - not regex_match(fileprefix.begin(), fileprefix.end(), ignored_regex); + Optional> vm; + if (not ignored_regex.empty()) + { + vm.emplace(*ignored_regex.impl()); + if (vm->exec(fileprefix.begin(), fileprefix.end(), fileprefix.begin(), fileprefix.end(), RegexExecFlags::None)) + vm.reset(); + } + const bool only_dirs = (flags & FilenameFlags::OnlyDirectories); - auto filter = [&ignored_regex, check_ignored_regex, only_dirs](const dirent& entry, struct stat& st) + auto filter = [&vm, only_dirs](const dirent& entry, struct stat& st) { StringView name{entry.d_name}; - return (not check_ignored_regex or not regex_match(name.begin(), name.end(), ignored_regex)) and + return (not vm or not vm->exec(name.begin(), name.end(), name.begin(), name.end(), RegexExecFlags::None)) and (not only_dirs or S_ISDIR(st.st_mode)); }; auto files = list_files(parsed_dirname, filter);