Use Replica::pending_tasks (#3661)

This replaces a loop over _all_ tasks with one that fetches only pending
tasks, as determined by the working set.

This should be faster for task DB's with large numbers of completed
tasks, although on my medium-sized installation (~5000 total tasks) the
difference is negligible.
This commit is contained in:
Dustin J. Mitchell
2024-10-30 21:49:04 -04:00
committed by GitHub
parent 8bad3cdcbc
commit 6ff900f3fc
2 changed files with 22 additions and 12 deletions

View File

@@ -370,18 +370,12 @@ const std::vector<Task> TDB2::all_tasks() {
const std::vector<Task> TDB2::pending_tasks() {
if (!_pending_tasks) {
Timer timer;
auto& ws = working_set();
auto largest_index = ws->largest_index();
auto pending_tctasks = replica()->pending_task_data();
std::vector<Task> result;
for (size_t i = 0; i <= largest_index; i++) {
auto uuid = ws->by_index(i);
if (!uuid.is_nil()) {
auto maybe_task = replica()->get_task_data(uuid);
if (maybe_task.is_some()) {
result.push_back(Task(maybe_task.take()));
}
}
for (auto& maybe_tctask : pending_tctasks) {
auto tctask = maybe_tctask.take();
result.push_back(Task(std::move(tctask)));
}
dependency_scan(result);

View File

@@ -114,13 +114,16 @@ mod ffi {
fn commit_reversed_operations(&mut self, ops: Vec<Operation>) -> Result<bool>;
/// Get `TaskData` values for all tasks in the replica.
///
/// This contains `OptionTaskData` to allow C++ to `take` values out of the vector and use
/// them as `rust::Box<TaskData>`. Cxx does not support `Vec<Box<_>>`. Cxx also does not
/// handle `HashMap`, so the result is not a map from uuid to task. The returned Vec is
/// fully populated, so it is safe to call `take` on each value in the returned Vec once .
fn all_task_data(&mut self) -> Result<Vec<OptionTaskData>>;
/// Simiar to all_task_data, but returing only pending tasks (those in the working set).
fn pending_task_data(&mut self) -> Result<Vec<OptionTaskData>>;
/// Get the UUIDs of all tasks.
fn all_task_uuids(&mut self) -> Result<Vec<Uuid>>;
@@ -500,6 +503,15 @@ impl Replica {
.collect())
}
fn pending_task_data(&mut self) -> Result<Vec<ffi::OptionTaskData>, CppError> {
Ok(self
.0
.pending_task_data()?
.drain(..)
.map(|t| Some(t).into())
.collect())
}
fn all_task_uuids(&mut self) -> Result<Vec<ffi::Uuid>, CppError> {
Ok(self
.0
@@ -870,10 +882,14 @@ mod test {
add_undo_point(&mut operations);
create_task(uuid_v4(), &mut operations);
create_task(uuid_v4(), &mut operations);
create_task(uuid_v4(), &mut operations);
let mut t = create_task(uuid_v4(), &mut operations);
cxx::let_cxx_string!(status = "status");
cxx::let_cxx_string!(pending = "pending");
t.update(&status, &pending, &mut operations);
rep.commit_operations(operations).unwrap();
assert_eq!(rep.all_task_data().unwrap().len(), 3);
assert_eq!(rep.pending_task_data().unwrap().len(), 1);
assert_eq!(rep.all_task_uuids().unwrap().len(), 3);
}