What undo/redo approach works best for a real-time text editor?
#1
I'm trying to implement a simple undo/redo feature for a text editor I'm building, but my naive approach of saving the entire document state on every keystroke is causing major performance issues. I've read about using a command pattern or a memento pattern, but I'm not sure which approach is more appropriate for a real-time application or how to handle merging fine-grained actions efficiently.
Reply
#2
I cut back on full snapshots by moving to a small command log and storing diffs instead of the whole text. On each keystroke I just record the edit as a patch and keep a stack of patches. Undo pops the patch and replays the patch set. Redo replays forward. It felt faster once the patches were small and I avoided duplicating the whole string. The tricky part was making sure you can reapply patches after edits in between. I also capped history and dropped old edits to avoid memory creep.
Reply
#3
I think you might be chasing the wrong problem I would try to measure before you rewrite the whole thing If you are seeing lag during typing the undo stack may not be the main culprit Could it be the rendering path that repaints a lot?
Reply
#4
Yesterday I chased a memory spike on a mobile build and ended up thinning the history window I set for the editor It helped a bit but users asked for more undo steps and I felt stuck I kept wondering if a simpler change would have been enough.
Reply
#5
I looked at the memento pattern as a theory once but in practice it felt brittle for fine grained edits and I ended up back to the patch log approach.
Reply


[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Forum Jump: