Save system: better to have one big file or several small ones?

Godot Version

4.7

Question

I am making my very first save system and wondering what is the best solution to handle saving data between one big file with all the data of the game, or several dozens smaller files that only contain a fraction of the data each.

Some context: I’m making a puzzle game where the player has to create interconnected buildings to solve the puzzle (like factorio or shapez). Each puzzle is in a room, and the player can see all the directly adjacent rooms, so at any given time there is between 1 and 9 rooms loaded and visible, each with their collection of buildings created by the player.

Whenever the player changes room, I save it, unload the rooms that gets out of sight and load a few new ones that come into view.

I expect the game to have a few dozen rooms in total, probably less than a hundred, and I would expect between 100 and 200 buildings per room.

So would it be more efficient (in terms of RAM usage and speed) to have a single large file that is opened at each room change, with all the writing and reading done once, or to have up to a dozen files opened and either written or red in quick succession?

If you have to open all of the rooms to save data then it would be significantly better to store all your rooms in one file. If you can save one room at a time, then separate files may be better. Minecraft for instance saves chunks in individual files (at least for PC), because the game cannot typically store entire worlds in-memory. Though more advanced games would use a sql database, Valheim for instance stores it’s world data in such a file, though it’s operation is much more complex.

On memory usage: if you can and do have all the rooms open in memory, then you should use one file. This is also much better for speed as opening a file, writing to it, and closing it is three system calls, and system calls are one of the slowest single operations a computer program can do. Generally speaking I’d say you should be happy to use more memory if it means less files and thus less system calls.

Thank you for your answer!

So if the real bottleneck is the system calls, having a single file does sound like the better option. Especially since my game is 2D and I can probably shave down the rooms state to a collection of [building type, position, rotation], load that in memory at the start, and rebuild the rooms as they come based on it. That would decouple the save system from the room spawning, and let me do the file writing in the background when needed.

So I could only have one read at the beginning, and a few writing here and there like when the player quit the game or every X minutes with a “something has changed” flag.

I’m gonna try that, thank you!

Since you already save on every room change and only up to nine rooms are loaded at once, one file per room seems to match how the game streams them, and you would only rewrite the rooms that actually changed. With 100 to 200 buildings per room each file should stay fairly small. Going by what gertkeno said, the deciding part is probably whether the rooms that are out of view stay in memory or get read from disk again when they come back. Which way is it set up right now?

Right now the rooms are fully unloaded when out of sight, destroying any building in them. They are re instantiated empty. I have not made any system to save the information or recreate a room with its buildings yet. I’m still very early in development.

2cents another issue is corruption of data for whatever reason, but in case when a single corrupt room file can still break the whole level it doesnt make any difference. About performance, not like ur writing into files all the time, just writing on transition should be fine, and you are not writing 100s of rooms just a bunch, so again little difference. Where i see the main consideration is simplicity. One file that is one line and just one object with whatever is referenced in it is just simpler to handle. Its so simple that you can dedicate the time you would have spent for multi file code to make simple automatic save backup just in case.