This is a raw patch that was made more for evaluating the idea.
Analyzing T88010: Memory consumption beyond defined limit when entering-exiting Edit mode, I realized that the logic of "compact array" used in edit-mode undo could be implemented for Mesh-type IDs and thus save memory usage.
But the current Undo System brings many obstacles to this implementation.
This is because "Global Undo" is very intertwined with the file writing/reading system.
Using file read/write functions for Undo is also misleading as, as far as I've seen, no files are written in undo steps.
This patch proposes to use specific functions for Undo thus separating what is for read/write files.
One downside is that a lot of code had to be duplicated, but there's probably still a lot of room for simplification.