Skip to content

Commit

Permalink
Use Replica::pending_tasks (#3661)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
djmitche authored Oct 31, 2024
1 parent 8bad3cd commit 6ff900f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
14 changes: 4 additions & 10 deletions src/TDB2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 18 additions & 2 deletions src/taskchampion-cpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 6ff900f

Please sign in to comment.