Skip to content

Commit

Permalink
Parallelize dirscan for grep.
Browse files Browse the repository at this point in the history
  • Loading branch information
grafikrobot committed Aug 17, 2023
1 parent 4e73bab commit 0f719a6
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/engine/mod_regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ struct regex_grep_task
grep_tasks = b2::task::executor::get().make();
}

void dir_scan(value_ref dir)
{
grep_tasks->queue([this, dir] {
file_dirscan(dir,
reinterpret_cast<scanback>(&regex_grep_task::dirscan_callback),
this);
});
}

static void dirscan_callback(regex_grep_task * self,
OBJECT * path,
int found,
Expand All @@ -259,22 +268,17 @@ struct regex_grep_task
{
if (glob(glob_pattern->str(), filename.c_str()) == 0)
{
// We have a glob match for this file. We can queue it up
// for the possibly parallel grep.
grep_tasks->queue([this, filepath] {
auto filedata = file_preload(filepath);
file_grep(filepath, *filedata);
});
// We have a glob match for this file.
auto filedata = file_preload(filepath);
file_grep(filepath, *filedata);
}
}
return;
}
// Subdir, or equivalent. If indicated, recurse scan subdirectories.
if (recursive_glob)
{
file_dirscan(file,
reinterpret_cast<scanback>(&regex_grep_task::dirscan_callback),
this);
dir_scan(file);
return;
}
}
Expand Down Expand Up @@ -318,8 +322,11 @@ struct regex_grep_task
}

// Append this file's results to the global results.
scope_lock_t lock(mx);
intermediate.emplace_back(result.release());
if (!result->empty())
{
scope_lock_t lock(mx);
intermediate.emplace_back(result.release());
}
}

void wait() { grep_tasks->wait(); }
Expand Down Expand Up @@ -362,9 +369,7 @@ list_ref b2::regex_grep(list_cref directories,
regex_grep_task task(list_cref(*globs), patterns, sub_expr, options);
for (auto dir : directories)
{
file_dirscan(dir,
reinterpret_cast<scanback>(&regex_grep_task::dirscan_callback),
&task);
task.dir_scan(dir);
}
task.wait();

Expand Down

0 comments on commit 0f719a6

Please sign in to comment.