TDB2: Improve TF2::get(uuid, task) performance

- Improves "task import" performance test on test machine further from
  13s to 1.9s.
- Active only during "task import".  Might be worth using during other
  mass lookups like "task sync", or could even be a good default.
This commit is contained in:
Wilhelm Schuermann 2015-09-22 18:25:34 +02:00
parent d006a2820b
commit 4fb9307be5
2 changed files with 42 additions and 4 deletions

View file

@ -121,6 +121,19 @@ bool TF2::get (const std::string& uuid, Task& task)
if (! _loaded_tasks)
load_tasks ();
if (_tasks_map.size () > 0 && uuid.size () == 36)
{
// Fast lookup, same result as below. Only used during "task import".
auto i = _tasks_map.find (uuid);
if (i != _tasks_map.end ())
{
task = i->second;
return true;
}
}
else
{
// Slow lookup, same result as above.
for (auto& i : _tasks)
{
if (closeEnough (i.get ("uuid"), uuid, uuid.length ()))
@ -129,6 +142,7 @@ bool TF2::get (const std::string& uuid, Task& task)
return true;
}
}
}
return false;
}
@ -152,6 +166,10 @@ void TF2::add_task (Task& task)
_tasks.push_back (task); // For subsequent queries
_added_tasks.push_back (task); // For commit/synch
// For faster lookup
if (context.cli2.getCommand () == "import")
_tasks_map.insert (std::pair<std::string, Task> (task.get("uuid"), task));
Task::status status = task.getStatus ();
if (task.id == 0 &&
(status == Task::pending ||
@ -170,12 +188,23 @@ void TF2::add_task (Task& task)
////////////////////////////////////////////////////////////////////////////////
bool TF2::modify_task (const Task& task)
{
// Modify in-place.
std::string uuid = task.get ("uuid");
if (context.cli2.getCommand () == "import")
{
// Update map used for faster lookup
auto i = _tasks_map.find (uuid);
if (i != _tasks_map.end ())
{
i->second = task;
}
}
for (auto& i : _tasks)
{
if (i.get ("uuid") == uuid)
{
// Modify in-place.
i = task;
_modified_tasks.push_back (task);
_dirty = true;
@ -302,6 +331,8 @@ void TF2::load_tasks ()
}
_tasks.push_back (task);
if (context.cli2.getCommand () == "import") // For faster lookup only
_tasks_map.insert (std::pair<std::string, Task> (task.get("uuid"), task));
// Maintain mapping for ease of link/dependency resolution.
// Note that this mapping is not restricted by the filter, and is

View file

@ -28,6 +28,7 @@
#define INCLUDED_TDB2
#include <map>
#include <unordered_map>
#include <vector>
#include <string>
#include <stdio.h>
@ -81,6 +82,12 @@ public:
bool _has_ids;
bool _auto_dep_scan;
std::vector <Task> _tasks;
// _tasks_map was introduced mainly for speeding up "task import".
// Iterating over all _tasks for each imported task is slow, making use of
// appropriate data structures is fast.
std::unordered_map <std::string, Task> _tasks_map;
std::vector <Task> _added_tasks;
std::vector <Task> _modified_tasks;
std::vector <std::string> _lines;