Skip to content

Commit

Permalink
Use collections.deque for undo/redo stack
Browse files Browse the repository at this point in the history
Avoids refilling the whole undo stack after every command.
  • Loading branch information
toniwe authored and jplloyd committed May 15, 2020
1 parent dc5deca commit 1d0a185
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions lib/command.py
Expand Up @@ -11,6 +11,7 @@
## Imports

from __future__ import division, print_function
from collections import deque
from warnings import warn
from copy import deepcopy
import weakref
Expand All @@ -23,7 +24,6 @@
import lib.stroke
from lib.pycompat import unicode


logger = getLogger(__name__)


Expand All @@ -37,8 +37,8 @@ class CommandStack (object):

def __init__(self, **kwargs):
super(CommandStack, self).__init__()
self.undo_stack = []
self.redo_stack = []
self.undo_stack = deque()
self.redo_stack = deque()
self.stack_updated()

def __repr__(self):
Expand All @@ -51,10 +51,10 @@ def clear(self):
self.stack_updated()

def _discard_undo(self):
self.undo_stack = []
self.undo_stack = deque()

def _discard_redo(self):
self.redo_stack = []
self.redo_stack = deque()

def do(self, command):
"""Performs a new command
Expand Down Expand Up @@ -121,15 +121,8 @@ def redo(self):

def reduce_undo_history(self):
"""Trims the undo stack"""
stack = self.undo_stack
self.undo_stack = []
steps = 0
for item in reversed(stack):
self.undo_stack.insert(0, item)
if not item.automatic_undo:
steps += 1
if steps == self.MAXLEN: # and memory > ...
break
while len(self.undo_stack) > self.MAXLEN:
self.undo_stack.popleft()

def get_last_command(self):
"""Returns the most recently performed command"""
Expand Down

0 comments on commit 1d0a185

Please sign in to comment.