> I'm failing to see the problem with loading everything into memory. If the next step is "write that to a temp file" there is a big problem. There's nothing wrong with the present tempfile approach. It's reasonably fast, it's completely portable (so no crazy getrlimit or /proc reading). Strace shows that that's how 'sed -i' works.
If on any system other than linux, I would consider loading into ram, but because of memory overcommit, malloc never fails, the whole system crawls to a halt, and the oom killer takes 20 minutes to put everything back together. No thanks.