From 7d9ef6dcd21561f008092a961f183f87e68dce3a Mon Sep 17 00:00:00 2001 From: Evan Teran Date: Sun, 14 Jan 2018 20:49:23 -0500 Subject: [PATCH] removed the memory based limit of undo/redo as it was actually causing some integer underflow bugs this resolves issue #23 and also makes undo/redo slightly more efficient to track --- source/DocumentWidget.cpp | 21 +-------------------- source/DocumentWidget.h | 1 - source/UndoInfo.h | 9 +-------- 3 files changed, 2 insertions(+), 29 deletions(-) diff --git a/source/DocumentWidget.cpp b/source/DocumentWidget.cpp index 13e9604f..e579a69c 100644 --- a/source/DocumentWidget.cpp +++ b/source/DocumentWidget.cpp @@ -1546,22 +1546,11 @@ void DocumentWidget::addUndoItem(UndoInfo &&undo) { // Add the item to the beginning of the list undo_.emplace_front(std::move(undo)); - // Increment the operation and memory counts - undoMemUsed_ += undo.oldText.size(); - // Trim the list if it exceeds any of the limits if (undo_.size() > UNDO_OP_LIMIT) { trimUndoList(UNDO_OP_TRIMTO); } - if (undoMemUsed_ > UNDO_WORRY_LIMIT) { - trimUndoList(UNDO_WORRY_TRIMTO); - } - - if (undoMemUsed_ > UNDO_PURGE_LIMIT) { - trimUndoList(UNDO_PURGE_TRIMTO); - } - if(MainWindow *window = MainWindow::fromDocument(this)) { window->undoAvailable(undo_.size() != 0); } @@ -1589,11 +1578,6 @@ void DocumentWidget::removeUndoItem() { return; } - const UndoInfo &undo = undo_.front(); - - // Decrement the operation and memory counts - undoMemUsed_ -= undo.oldText.size(); - // Remove and free the item undo_.pop_front(); @@ -1636,10 +1620,7 @@ void DocumentWidget::trimUndoList(size_t maxLength) { } // Trim off all subsequent entries - while(it != undo_.end()) { - undoMemUsed_ -= it->oldText.size(); - it = undo_.erase(it); - } + undo_.erase(it, undo_.end()); } void DocumentWidget::Undo() { diff --git a/source/DocumentWidget.h b/source/DocumentWidget.h index be776ef4..0a545dd7 100644 --- a/source/DocumentWidget.h +++ b/source/DocumentWidget.h @@ -307,7 +307,6 @@ class DocumentWidget : public QWidget { int autoSaveCharCount_ = 0; // count of single characters typed since last backup file generated int autoSaveOpCount_ = 0; // count of editing operations "" size_t nMarks_ = 0; // number of active bookmarks - size_t undoMemUsed_ = 0; // amount of memory (in bytes) dedicated to the undo list mode_t mode_ = 0; // permissions of file being edited time_t lastModTime_ = 0; // time of last modification to file uid_t uid_ = 0; // last recorded user id of the file diff --git a/source/UndoInfo.h b/source/UndoInfo.h index 56e47e85..0315a596 100644 --- a/source/UndoInfo.h +++ b/source/UndoInfo.h @@ -8,15 +8,8 @@ amounts of memory. These tuning parameters determine how much undo infor- mation is retained. Normally, the list is kept between UNDO_OP_LIMIT and UNDO_OP_TRIMTO in length (when the list reaches UNDO_OP_LIMIT, it is - trimmed to UNDO_OP_TRIMTO then allowed to grow back to UNDO_OP_LIMIT). - When there are very large amounts of saved text held in the list, - UNDO_WORRY_LIMIT and UNDO_PURGE_LIMIT take over and cause the list to - be trimmed back further to keep its size down. */ + trimmed to UNDO_OP_TRIMTO then allowed to grow back to UNDO_OP_LIMIT). */ -constexpr auto UNDO_PURGE_LIMIT = 15000000u; // If undo list gets this large (in bytes), trim it to length of UNDO_PURGE_TRIMTO -constexpr auto UNDO_PURGE_TRIMTO = 1u; // Amount to trim the undo list in a purge -constexpr auto UNDO_WORRY_LIMIT = 2000000u; // If undo list gets this large (in bytes), trim it to length of UNDO_WORRY_TRIMTO -constexpr auto UNDO_WORRY_TRIMTO = 5u; // Amount to trim the undo list when memory use begins to get serious constexpr auto UNDO_OP_LIMIT = 400u; // normal limit for length of undo list constexpr auto UNDO_OP_TRIMTO = 200u; // size undo list is normally trimmed to when it exceeds UNDO_OP_TRIMTO in length