From 48ed6b4981362bfe58ddfd72f1ec93c8a0f4352c Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Sun, 27 Oct 2024 15:16:38 -0400 Subject: [PATCH] Use `Replica::pending_tasks` 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. --- src/TDB2.cpp | 14 ++++---------- src/taskchampion-cpp/src/lib.rs | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/TDB2.cpp b/src/TDB2.cpp index d5dacd769..2b01bc3b5 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -370,18 +370,12 @@ const std::vector TDB2::all_tasks() { const std::vector 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 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); diff --git a/src/taskchampion-cpp/src/lib.rs b/src/taskchampion-cpp/src/lib.rs index d58af7a60..622eebc49 100644 --- a/src/taskchampion-cpp/src/lib.rs +++ b/src/taskchampion-cpp/src/lib.rs @@ -114,13 +114,16 @@ mod ffi { fn commit_reversed_operations(&mut self, ops: Vec) -> Result; /// 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`. Cxx does not support `Vec>`. 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>; + /// Simiar to all_task_data, but returing only pending tasks (those in the working set). + fn pending_task_data(&mut self) -> Result>; + /// Get the UUIDs of all tasks. fn all_task_uuids(&mut self) -> Result>; @@ -500,6 +503,15 @@ impl Replica { .collect()) } + fn pending_task_data(&mut self) -> Result, CppError> { + Ok(self + .0 + .pending_task_data()? + .drain(..) + .map(|t| Some(t).into()) + .collect()) + } + fn all_task_uuids(&mut self) -> Result, 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); }