0

This question already has an answer here:

Long time ago I learned that text files are not like Random access Files, i. e., adding or updating info at the beginning of a text file involves moving all the rest of the file "forward" (or backwards if the size of the new info is smaller than the former info) in storage to let some space. This takes time and space in memory, even using buffers.

Moreover modern IDEs seem to flush all the buffers when saving files in case a failure occurs.

My point is: Am I right presuming that editing a huge text file with autosave enabled is slower when done at the beginning of the file or are there some tricks involved I'm not aware of?

Maybe the lack of really fast secondary storage (like SSD) until a few years ago made this autosave not so common? Can anybody perhaps enlighten me?

I've read https://softwareengineering.stackexchange.com/a/273524/336500 but I'd prefer an answer from the point of view of a programmer, like

What type of File Decorator would you use to implement autosave feature in Java?, for example.

marked as duplicate by Robert Harvey May 16 at 16:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Are you entirely sure that Autosave will work with a huge text file? Saving a 10MB file will take just a second, but I doubt IDEs will really attempt autosaves on 1G files – Ordous May 16 at 15:47
  • 1
    What do you consider huge? The largest single source code file I've ever seen was about 20,000 lines long, which might be pushing a megabyte. You only have to autosave files that have been changed or added, and programmers can only type so fast. Seriously, it's not a heavy lift. – Robert Harvey May 16 at 15:52
  • So, autosave was not an option 20 years ago due to the slow hardware we had which is not a problem nowadays, I'm I right? – Mdot May 16 at 15:57
  • 1
    I seem to remember having auto-save in some tools 20 years ago. "Slow" is a relative term, the machines I was using to write code 20 years ago were way faster than the machines I used to write code 30 years ago. – cdkMoose May 16 at 16:11
  • So, autosave was not an option 20 years ago due to the slow hardware we had which is not a problem nowadays, I'm I right? -- Hard to say. Maybe we just hadn't thought of it yet. – Robert Harvey May 16 at 16:12
0

adding or updating info at the beginning of a text file involves moving all the rest of the file "forward"

Only if you have a the simplest possible in-memory implementation. It would, for example, be easy to store a text file as a linked list of lines. Now, adding text at the start of the file is just a matter of prepending a new line to the linked list.

  • [shrug] See my comment above. – Robert Harvey May 16 at 15:53
  • But if you have to "flush" to secondary memory the list, It really doen't matter your in-memory implementation, at last things will work as I thought. Thank you. – Mdot May 16 at 15:55
  • 1
    @Mdot And who's saying that your on-disk representation isn't also a linked list-style? Add a line in the middle? Navigate to exactly that row on the disk, modify the "next" data, append new line at the end of storage with "next" pointing to the line that should come next. – Delioth May 16 at 19:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.