mercredi 11 janvier 2023

Performance consequence of using a single deque for undo / redo in memento implementation vs separate undo / redo stacks/linked lists?

I have implemented a Memento pattern using a deque

class Mementoable {
deque<Memento*> _mementos;
int _currentIndex;
... //Implementation omitted for brevity
}

The currentIndex stores the current position. For example:

  • if I have just saved, the currentIndex will be the last index.
  • if I undo once, the currentIndex is decremented
  • if I redo once, the currentIndex is incremented
  • if I undo, and then do a new save, then all items after the new saves index will be cleared (to redo wouldn't make sense anymore)

Without taking into performance of the deque, this seems like a very fast implementation to me, since I either add or remove mementos from the dequeue (which would have to be done anyway if I had separate stacks for undo and redo), or I simply update the index, which is much faster than having separate stacks, since I would have to move objects from the undo stack to redo stack and vice versa.

If I play devils advocate, having separate undo / redo stacks could be very fast if I just use a linked list, in contrast if I use the std::deque then if it becomes very large there could be memory management issues that reduce performance depending on the implementation of deque.

Is my analysis correct ? Which option is better ? Or is any difference negligible ?

Aucun commentaire:

Enregistrer un commentaire