mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 01:33:19 +00:00
Improve startup time
This commit is contained in:
parent
d90bfbc483
commit
597ea0ac2d
10 changed files with 82 additions and 23 deletions
|
@ -26,18 +26,40 @@ class Timer
|
||||||
void start()
|
void start()
|
||||||
{
|
{
|
||||||
startTime = std::chrono::high_resolution_clock::now();
|
startTime = std::chrono::high_resolution_clock::now();
|
||||||
|
isRunning = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop()
|
||||||
|
{
|
||||||
|
const auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
duration += std::chrono::duration_cast<std::chrono::microseconds>(now - startTime).count();
|
||||||
|
isRunning = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset()
|
||||||
|
{
|
||||||
|
duration = 0;
|
||||||
|
isRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printTimeElapsed(const std::string &message) const
|
void printTimeElapsed(const std::string &message) const
|
||||||
{
|
{
|
||||||
const auto now = std::chrono::high_resolution_clock::now();
|
long durationToShow = duration;
|
||||||
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - startTime);
|
if (isRunning) {
|
||||||
std::cout << "[Debug - Timer] " << message << ": " << ((double)duration.count() / 1000000)
|
const auto now = std::chrono::high_resolution_clock::now();
|
||||||
|
durationToShow +=
|
||||||
|
std::chrono::duration_cast<std::chrono::microseconds>(now - startTime).count();
|
||||||
|
}
|
||||||
|
std::cout << "[Debug - Timer] " << message << ": " << ((double)durationToShow / 1000000)
|
||||||
<< " seconds" << std::endl;
|
<< " seconds" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::time_point<std::chrono::system_clock> startTime;
|
std::chrono::time_point<std::chrono::system_clock> startTime;
|
||||||
|
|
||||||
|
// Timer are always running when created. You can use .reset() after creation.
|
||||||
|
bool isRunning = true;
|
||||||
|
long duration;
|
||||||
};
|
};
|
||||||
} // namespace cpputils::debug
|
} // namespace cpputils::debug
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ void Backend::addTodoFromRawFormat(QString filePath, QString text, QString date)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
file.value().get().addTask(
|
file.value().get().addTask(
|
||||||
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString())
|
mirai::TodoMdFormat::stringToTask(text.toStdString(), date.toStdString())
|
||||||
);
|
);
|
||||||
mirai.save();
|
mirai.save();
|
||||||
view.update();
|
view.update();
|
||||||
|
@ -137,7 +137,7 @@ void Backend::updateTodoFromRawFormat(int todoIndex, QString text, QString date)
|
||||||
QMLTaskItem &taskItem = QMLTasks[todoIndex];
|
QMLTaskItem &taskItem = QMLTasks[todoIndex];
|
||||||
taskItem.taskItem->setText(taskItem.taskItem->getText());
|
taskItem.taskItem->setText(taskItem.taskItem->getText());
|
||||||
*(taskItem.taskItem) =
|
*(taskItem.taskItem) =
|
||||||
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString());
|
mirai::TodoMdFormat::stringToTask(text.toStdString(), date.toStdString());
|
||||||
mirai.save();
|
mirai.save();
|
||||||
view.update();
|
view.update();
|
||||||
rebuildQMLTasksList();
|
rebuildQMLTasksList();
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
QString QMLTaskItem::getRawFormat()
|
QString QMLTaskItem::getRawFormat()
|
||||||
{
|
{
|
||||||
return QString::fromStdString(mirai::TodoMdFormat::TaskToString(*taskItem));
|
return QString::fromStdString(mirai::TodoMdFormat::taskToString(*taskItem));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QMLTaskItem::getText()
|
QString QMLTaskItem::getText()
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include "Mirai.h"
|
#include "Mirai.h"
|
||||||
#include "TaskItem.h"
|
#include "TaskItem.h"
|
||||||
#include "core/TasksFile.h"
|
#include "core/TasksFile.h"
|
||||||
|
#include "cpp-utils/debug.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -38,6 +39,7 @@ void Mirai::save()
|
||||||
{
|
{
|
||||||
for (auto &file : files) {
|
for (auto &file : files) {
|
||||||
if (file->isDirty()) {
|
if (file->isDirty()) {
|
||||||
|
file->sortByDate();
|
||||||
TodoMdFormat::writeFile(*file);
|
TodoMdFormat::writeFile(*file);
|
||||||
file->setDirty(false);
|
file->setDirty(false);
|
||||||
}
|
}
|
||||||
|
@ -90,6 +92,7 @@ const std::vector<std::string> &Mirai::getTags()
|
||||||
|
|
||||||
void Mirai::reloadTags()
|
void Mirai::reloadTags()
|
||||||
{
|
{
|
||||||
|
cpputils::debug::Timer reloadingTagsDuration;
|
||||||
tags.clear();
|
tags.clear();
|
||||||
for (auto &file : files) {
|
for (auto &file : files) {
|
||||||
for (auto &task : file->getTasks()) {
|
for (auto &task : file->getTasks()) {
|
||||||
|
@ -100,6 +103,7 @@ void Mirai::reloadTags()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
reloadingTagsDuration.printTimeElapsed("ReloadingTags");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace mirai
|
} // namespace mirai
|
||||||
|
|
|
@ -59,7 +59,6 @@ void TasksFile::addTask(TaskItemData taskItem)
|
||||||
{
|
{
|
||||||
tasks.push_back(std::make_unique<TaskItem>(this, taskItem));
|
tasks.push_back(std::make_unique<TaskItem>(this, taskItem));
|
||||||
setDirty(true);
|
setDirty(true);
|
||||||
sortByDate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TasksFile::addTask(std::string text, std::string date)
|
void TasksFile::addTask(std::string text, std::string date)
|
||||||
|
@ -68,7 +67,6 @@ void TasksFile::addTask(std::string text, std::string date)
|
||||||
TaskItem{this, {.text = text, .state = TODO, .date = date == "" ? "No date" : date}}
|
TaskItem{this, {.text = text, .state = TODO, .date = date == "" ? "No date" : date}}
|
||||||
);
|
);
|
||||||
tasks.push_back(std::move(newTask));
|
tasks.push_back(std::move(newTask));
|
||||||
sortByDate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TasksFile::removeTask(const TaskItem *taskToRemove)
|
void TasksFile::removeTask(const TaskItem *taskToRemove)
|
||||||
|
|
|
@ -57,6 +57,7 @@ void TasksView::update()
|
||||||
} else if (!t1->hasDate() && t2->hasDate()) {
|
} else if (!t1->hasDate() && t2->hasDate()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t1->getDate() < t2->getDate()) {
|
if (t1->getDate() < t2->getDate()) {
|
||||||
return true;
|
return true;
|
||||||
} else if (t1->getDate() > t2->getDate()) {
|
} else if (t1->getDate() > t2->getDate()) {
|
||||||
|
|
|
@ -7,16 +7,19 @@
|
||||||
#include "TodoMd.h"
|
#include "TodoMd.h"
|
||||||
#include "TaskItem.h"
|
#include "TaskItem.h"
|
||||||
#include "core/TasksFile.h"
|
#include "core/TasksFile.h"
|
||||||
|
#include "cpp-utils/debug.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace mirai
|
namespace mirai
|
||||||
{
|
{
|
||||||
|
|
||||||
std::unique_ptr<TasksFile> TodoMdFormat::readFile(const std::string &path)
|
std::unique_ptr<TasksFile> TodoMdFormat::readFile(const std::string &path)
|
||||||
{
|
{
|
||||||
|
cpputils::debug::Timer readMdFormatDuration;
|
||||||
std::ifstream file(path);
|
std::ifstream file(path);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
throw std::runtime_error("todo.txt file not found");
|
throw std::runtime_error("todo.txt file not found");
|
||||||
|
@ -35,17 +38,26 @@ std::unique_ptr<TasksFile> TodoMdFormat::readFile(const std::string &path)
|
||||||
TasksFileConstructor{.name = name, .path = path, .tasks = taskItems}
|
TasksFileConstructor{.name = name, .path = path, .tasks = taskItems}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
cpputils::debug::Timer stringToTaskDuration;
|
||||||
|
stringToTaskDuration.reset();
|
||||||
|
cpputils::debug::Timer gelinesDuration;
|
||||||
while (std::getline(file, line)) {
|
while (std::getline(file, line)) {
|
||||||
if (line.substr(0, 3) == "## ") {
|
if (std::string_view{line.data(), 3} == "## ") {
|
||||||
currentDate = line.substr(3);
|
currentDate = line.substr(3);
|
||||||
} else if (line.substr(0, 5) == "- [ ]" || line.substr(0, 5) == "- [X]") {
|
} else if (std::string_view{line.data(), 5} == "- [ ]" || std::string_view{line.data(), 5} == "- [X]") {
|
||||||
TaskItemData taskItemData = StringToTask(line, currentDate);
|
stringToTaskDuration.start();
|
||||||
|
TaskItemData taskItemData = stringToTask(line, currentDate);
|
||||||
|
stringToTaskDuration.stop();
|
||||||
taskItemData.date = currentDate;
|
taskItemData.date = currentDate;
|
||||||
tasksFile->addTask(taskItemData);
|
tasksFile->addTask(taskItemData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gelinesDuration.printTimeElapsed("getlinesDuration");
|
||||||
|
stringToTaskDuration.printTimeElapsed("stringToTaskDuration");
|
||||||
file.close();
|
file.close();
|
||||||
tasksFile->setDirty(false);
|
tasksFile->setDirty(false);
|
||||||
|
readMdFormatDuration.printTimeElapsed("Reading MD File duration of " + path);
|
||||||
return tasksFile;
|
return tasksFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +91,7 @@ void TodoMdFormat::writeFile(TasksFile &tasks)
|
||||||
currentDate = task->getDate();
|
currentDate = task->getDate();
|
||||||
file << "\n## " << (task->getDate() != "" ? task->getDate() : "No date") << "\n\n";
|
file << "\n## " << (task->getDate() != "" ? task->getDate() : "No date") << "\n\n";
|
||||||
}
|
}
|
||||||
file << TaskToString(*task) << '\n';
|
file << taskToString(*task) << '\n';
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
@ -92,7 +104,7 @@ std::string TodoMdFormat::fieldWithSpace(const std::string &field)
|
||||||
return (field + " ");
|
return (field + " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
TaskItemData TodoMdFormat::StringToTask(const std::string &str, const std::string &date)
|
TaskItemData TodoMdFormat::stringToTask(const std::string &str, const std::string &date)
|
||||||
{
|
{
|
||||||
std::smatch matches;
|
std::smatch matches;
|
||||||
std::regex regex("- \\[(\\s|X)\\] (([0-9]{2}:[0-9]{2})-([0-9]{2}:[0-9]{2}) > )?(.*?)( -- (.*))?"
|
std::regex regex("- \\[(\\s|X)\\] (([0-9]{2}:[0-9]{2})-([0-9]{2}:[0-9]{2}) > )?(.*?)( -- (.*))?"
|
||||||
|
@ -122,7 +134,7 @@ TaskItemData TodoMdFormat::StringToTask(const std::string &str, const std::strin
|
||||||
return taskItem;
|
return taskItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string TodoMdFormat::TaskToString(const TaskItem &task)
|
std::string TodoMdFormat::taskToString(const TaskItem &task)
|
||||||
{
|
{
|
||||||
std::string str = task.getText();
|
std::string str = task.getText();
|
||||||
if (task.getTags().size() > 0) {
|
if (task.getTags().size() > 0) {
|
||||||
|
|
|
@ -20,8 +20,8 @@ class TodoMdFormat
|
||||||
static std::unique_ptr<TasksFile> readFile(const std::string &path);
|
static std::unique_ptr<TasksFile> readFile(const std::string &path);
|
||||||
static void writeFile(TasksFile &tasks);
|
static void writeFile(TasksFile &tasks);
|
||||||
|
|
||||||
static std::string TaskToString(const TaskItem &task);
|
static std::string taskToString(const TaskItem &task);
|
||||||
static TaskItemData StringToTask(const std::string &str, const std::string &date);
|
static TaskItemData stringToTask(const std::string &str, const std::string &date);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::string fieldWithSpace(const std::string &field);
|
static std::string fieldWithSpace(const std::string &field);
|
||||||
|
|
|
@ -5,12 +5,34 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
namespace mirai
|
namespace mirai
|
||||||
{
|
{
|
||||||
bool isDate(const std::string &dateStr)
|
bool isDate(const std::string &dateStr)
|
||||||
{
|
{
|
||||||
std::regex regex("[0-9]{4}-[0-9]{2}-[0-9]{2}");
|
// std regex are really slow
|
||||||
return std::regex_match(dateStr, regex);
|
/*std::regex regex("[0-9]{4}-[0-9]{2}-[0-9]{2}");*/
|
||||||
|
/*return std::regex_match(dateStr, regex);*/
|
||||||
|
|
||||||
|
// Quick hand made check instead of regex until I rework date to use timestamp or std::chrono
|
||||||
|
if (dateStr.length() != 10) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dateStr[4] != '-' || dateStr[7] != '-') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < dateStr.length(); ++i) {
|
||||||
|
if (i == 4 || i == 7) {
|
||||||
|
// These are the '-' characters
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!isdigit(dateStr[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} // namespace mirai
|
} // namespace mirai
|
||||||
|
|
|
@ -15,7 +15,7 @@ TEST_CASE("Creating task from string")
|
||||||
{
|
{
|
||||||
SECTION("All properties")
|
SECTION("All properties")
|
||||||
{
|
{
|
||||||
auto task = mirai::TodoMdFormat::StringToTask(
|
auto task = mirai::TodoMdFormat::stringToTask(
|
||||||
"- [X] 08:00-10:00 > This is a test -- #mirai", "2024-04-19"
|
"- [X] 08:00-10:00 > This is a test -- #mirai", "2024-04-19"
|
||||||
);
|
);
|
||||||
REQUIRE(task.date == "2024-04-19");
|
REQUIRE(task.date == "2024-04-19");
|
||||||
|
@ -29,7 +29,7 @@ TEST_CASE("Creating task from string")
|
||||||
|
|
||||||
SECTION("All properties 2")
|
SECTION("All properties 2")
|
||||||
{
|
{
|
||||||
auto task = mirai::TodoMdFormat::StringToTask(
|
auto task = mirai::TodoMdFormat::stringToTask(
|
||||||
"- [ ] 09:00-17:00 > This is another test -- #mirai #feature", "2024-04-20"
|
"- [ ] 09:00-17:00 > This is another test -- #mirai #feature", "2024-04-20"
|
||||||
);
|
);
|
||||||
REQUIRE(task.date == "2024-04-20");
|
REQUIRE(task.date == "2024-04-20");
|
||||||
|
@ -44,7 +44,7 @@ TEST_CASE("Creating task from string")
|
||||||
|
|
||||||
SECTION("Only text")
|
SECTION("Only text")
|
||||||
{
|
{
|
||||||
auto task = mirai::TodoMdFormat::StringToTask("- [ ] This is another test", "");
|
auto task = mirai::TodoMdFormat::stringToTask("- [ ] This is another test", "");
|
||||||
REQUIRE(task.date == "");
|
REQUIRE(task.date == "");
|
||||||
REQUIRE(task.tags.size() == 0);
|
REQUIRE(task.tags.size() == 0);
|
||||||
REQUIRE(task.text == "This is another test");
|
REQUIRE(task.text == "This is another test");
|
||||||
|
@ -53,7 +53,7 @@ TEST_CASE("Creating task from string")
|
||||||
|
|
||||||
SECTION("Only text with a date")
|
SECTION("Only text with a date")
|
||||||
{
|
{
|
||||||
auto task = mirai::TodoMdFormat::StringToTask("- [X] This is another test", "2025-02-03");
|
auto task = mirai::TodoMdFormat::stringToTask("- [X] This is another test", "2025-02-03");
|
||||||
REQUIRE(task.date == "2025-02-03");
|
REQUIRE(task.date == "2025-02-03");
|
||||||
REQUIRE(task.tags.size() == 0);
|
REQUIRE(task.tags.size() == 0);
|
||||||
REQUIRE(task.text == "This is another test");
|
REQUIRE(task.text == "This is another test");
|
||||||
|
@ -62,7 +62,7 @@ TEST_CASE("Creating task from string")
|
||||||
|
|
||||||
SECTION("Tags before -- are not tags")
|
SECTION("Tags before -- are not tags")
|
||||||
{
|
{
|
||||||
auto task = mirai::TodoMdFormat::StringToTask(
|
auto task = mirai::TodoMdFormat::stringToTask(
|
||||||
"- [ ] This is another test #ImNotATag -- #ImATag", ""
|
"- [ ] This is another test #ImNotATag -- #ImATag", ""
|
||||||
);
|
);
|
||||||
REQUIRE(task.date == "");
|
REQUIRE(task.date == "");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue