r/computerscience • • Aug 05 '25
General How does the computer know now to prompt saving a document when I type something, erase it and type it back?
When you have a text file and you change it, it gives you an option to save
If I type "Hello", hit backspace, then I will immediately get a save prompt. The character count has been changed
If I type "Hello", hit backspace and type "h", I will get a save prompt
If I type "Hello", hit backspace and type "o", I will not get a save prompt
I'm sure hashing the entire file is too expensive, and collisions can occur
So how does a computer know when to prompt a save, and when not to
92
Upvotes
59
u/SirClueless Aug 05 '25
Why are you sure about this? A CPU can hash at about ~13 GB/s (source: https://github.com/google/cityhash). A text file is a few kilobytes. A 10 kb file can be hashed in under a microsecond (i.e. you could hash a million of them every second).
With a 64-bit hash, you can type a million random characters every second and not expect to collide for 200,000 years. With a 128-bit hash, you can do this until the heat death of the universe and not expect to collide.
4
u/RajjSinghh Aug 05 '25
You don't need to track the contents of a file, you just need to track changes. Relatively few lines in a file change between saves so you can save a lot of work by just looking at those changes instead of the rest of the file. That's also why git can work quickly, even on large projects.
42
u/postmodest Aug 05 '25
It saves every change you make in a list. When you hit backspace it goes backward and undoes the things in that list. If it gets to the beginning of the list, it knows nothing has changed and it doesn't need to save.
14
u/wolfkeeper Aug 05 '25
The exact algorithms are going to depend on the editor.
Calculating a hash isn't generally too slow. Modern computers can hash at hundreds of megabytes per second, and hash collisions can be made statistically impossible to occur accidentally. So occasionally calculating the hash and comparing with the last save file would be a good way to do it; but I think most editors typically just check to see when the 'undo' list is empty.
13
u/TabAtkins Aug 05 '25
It's not the computer prompting you to save, it's the word processor program you're using. It obviously knows you've entered characters then deleted them, because it updated your display accordingly. No need to verify with the original file on disk.
2
u/solarmist Aug 05 '25
Check out Editor data structures
1
u/stevevdvkpe Aug 05 '25
Hashing file contents isn't generally how editors decide when to prompt for saving or to automatically save a file. It's typically based more on the time since the last save (or when the file was first opened) or on the amount of changes that have happened since the last save. There are much easier ways to track changes than hashing the entire file, especially since an editor is often recording editing changes incrementally so that you can undo them incrementally.
1
u/Revolutionalredstone Aug 05 '25
It has a string of the last save and just checks if they are the same.
most of the time this is just a single size check which is instant.
Otherwise it ends up doing a byte level comparison which is also very much instant on modern computers.
Enjoy
1
u/Menector Aug 05 '25
It's program specific. Some will still prompt to save even if you retype 'o' or hit undo. Two main ways to track changes I'm familiar with are hashes and dirty bits.
Hashes are straightforward, and not really all that expensive. The hash doesn't even have to be complicated, most simple hashes would easily detect a single character change with no collision concerns. The hashing code would be run in a separate thread, so even if it took a few seconds (for a very large file) you wouldn't really notice the delay. Small files are effectively immediate.
For dirty bits, on change you set a flag as True to indicate the change. The flag is removed if you 'undo' all the way to the first change or if all changes can be considered logically circular (returning to original state). Most text editors I've used aren't that complicated and will just assume the file has changed regardless of if it's undone, but tracking it and determining circular logic isn't too complicated.
1
u/pixel293 Aug 05 '25
A simple text editor/word processor would just have a "dirty" flag, any time you change the file, backspace, type a character, whatever, the dirty flag is set and it prompts you to save.
An advanced text editor could either track the changes and if they are simple enough determine that you ultimately haven't changed anything at all. This could also be done by just having a copy of the document in memory as it appears on disk and doing a compare of your modified document.
1
u/high_throughput Aug 05 '25
Huh. I could have sworn I've seen editors do this, but now that I'm looking for one to actually investigate, I'm not able to reproduce it.
I tried VS Code, Sublime Text, Vim, Kakuone, Lite XL, and the online GitHub editor. All of them treated the buffer as dirty when you delete and retype a character.
1
u/Fragrant_Gap7551 Aug 05 '25
It doesn't need to hash the whole file, only keep track of changes.
If you remove an "o" at position 5, then add an "o" at position 5, it can cancel those out against each other.
If no changes are in the list you don't get a save prompt
That same list can be used while changing to only override necessary parts of the file.
1
u/Llotekr Aug 09 '25
If the editor uses a rope dataype (a balanced tree) and a homomorphic monoidal hash function, it only needs to do O(log n) work to update the hash after an edit.
I have developed a rope datatype that I call "yarn" which makes equality comparison of character sequences very efficient (O(1)), so we don't have to just rely on hashes alone, and hence collisions will not lead to false positives. Maybe those editors use something similar? Yarn is only special in that it guarantees O(log n · log* n) update cost in the worst case, but previous algorithms should be good enough to get O(log n) in the average case.
0
u/hoeding Aug 05 '25
It can be as simple as setting a variable from false to true when any action is performed.
-2
u/high_throughput Aug 05 '25
I'm hoping someone less in bed than me will actually take a look at some editor's source code instead of guessing