Replace All on a 10 GB file without loading it
Drafted with Claude (Anthropic) from BigText's code, notes and benchmark reports. I checked every number and code excerpt against the repository before publishing. In an ordinary text editor, Replace All is a small loop. The file is in memory, you search it, you build the new text, and you push the old one onto the undo stack. For a 10 GB log the same loop needs at least the file, the result and…
For editors handling massive text files, the "Replace All" feature can prove to be a bottleneck. In a standard text editor, this operation typically involves loading the entire file into memory, searching for matches, building the new text, and then replacing the old file—a process that requires tens of gigabytes of RAM for a 10 GB log file. This has limited the capabilities of text editors when dealing with such large files.
BigText, a free and open-source editor designed for handling huge text and log files across Windows, Linux, and macOS, takes a different approach. Instead of loading the entire file into memory, it employs a streaming path for Replace All operations on files exceeding a threshold of 50,000 matches. This approach enables BigText to avoid the memory constraints associated with larger files.
The streaming path begins by collecting all matches within the file and ceasing the operation once the match count surpasses the threshold. Once the matches are collected, the editor can then process them using a streaming algorithm, which processes the file in chunks rather than loading the entire file into memory. This allows BigText to perform Replace All operations on large files without consuming excessive memory.
The undo ability of the streaming path is also maintained, ensuring that users can reverse any "Replace All" operation. This is achieved by storing the changes made during the streaming operation in a separate buffer, which can be undone using the standard undo command (Ctrl+Z). By employing this streaming path, BigText can perform Replace All operations on large files efficiently, without the need to load the entire file into memory.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to