Skip to content

Don't re-save chunks that were only loaded - #14498

Open
InstantlyMoist wants to merge 1 commit into
PaperMC:mainfrom
InstantlyMoist:fix/skip-resave-unchanged-chunks
Open

InstantlyMoist wants to merge 1 commit into
PaperMC:mainfrom
InstantlyMoist:fix/skip-resave-unchanged-chunks

Conversation

@InstantlyMoist

Copy link
Copy Markdown

i was looking around and noticed chunks were always saving no matter what, so i told claude to investigate. it found this out, not sure if it's of any use but it seems to stop the unnecesary saving. i did my best claude did the rest


Chunks that are only loaded and unloaded again, without anything in them changing, are currently written back to their region file every time. On a busy server that means every chunk players pass through gets serialized and rewritten on unload. An NBT diff of such a chunk before and after shows only LastUpdate changed.

This makes a loaded chunk only count as unsaved when the data that would be written actually differs from what is on disk.

Why chunks were marked unsaved

On unload NewChunkHolder.saveChunk writes the chunk if chunk.isUnsaved(). Every chunk read from disk already was:

  • SerializableChunkData.read restores the chunk through setters that call markUnsaved(): setLightCorrect, setAllStarts, setAllReferences.
  • read also does persistentDataContainer.putAll(ChunkBukkitValues), and DirtyCraftPersistentDataContainer.putAll sets the dirty flag, so any chunk with PDC data was unsaved through isUnsaved() = unsaved || pdc.dirty().
  • After load, Starlight fires ServerChunkCache.onLightUpdate > ChunkHolder.sectionLightChanged > ImposterProtoChunk.markUnsaved for sections whose light did not change (details below).

Changes

Load (SerializableChunkData.read): after restoring a full chunk, the unsaved flag and PDC dirty flag set by read itself are cleared, unless the load changed something that would be written:

  • the chunk was not light-correct on disk,
  • heightmaps were missing or had the wrong size and had to be primed,
  • unknown/invalid structure starts or references were dropped (or empty reference sets, which are not written back),
  • the chunk has blending data.

Proto chunks are not touched; they are marked unsaved by setPersistedStatus as before.

DataConverter (ChunkLoadTask): the DataVersion is checked before upgradeChunkTag, and an upgraded chunk is marked unsaved so it is written back in the current format.

Post-processing (LevelChunk.postProcessGeneration): post-processing lists, pending block entities and upgrade data stay in the saved data until they are consumed here, which only happens once the chunk is ticking. The chunk is marked unsaved here if any of them was non-empty. Marking at load time would keep re-saving chunks at the edge of the loaded area that hold post-processing data but never tick.

PDC (ChunkAccess.tryMarkSaved): a chunk whose only change was its PDC is saved, but tryMarkSaved only cleared the PDC dirty flag if unsaved was also set, so such a chunk stayed unsaved and was rewritten on every save. It now clears it in both cases.

Light (Starlight): two guards, both only skipping updates that cannot be observed:

  • SkyStarLightEngine.checkNullSection calls initNibble(..., initRemovedNibbles = true), which puts a new SWMRNibbleArray into the engine's nibble cache in place of a null nibble that rewriteNibbleCacheForSkylight removed from the cache, so light can propagate through it. That nibble only exists in the cache and is dropped in destroyCaches: the only places nibbles are written back to a chunk (light, relight) use their own arrays, and getNibblesForChunkFromCache is unused. The chunk's real nibble for that section is out of the cache, so nothing can modify it during the operation. Yet updateVisible reported the cache-only nibble as changed and fired onLightUpdate for the section. These nibbles are now flagged and don't fire it. This was the main cause of re-saves after the load fix.
  • SWMRNibbleArray.updateVisible returned true for any dirty nibble. It now returns true only if the visible state or data actually differs afterwards (a light update can lower a value and propagate it back up to where it was). Its return value is only used by StarLightEngine.updateVisible to decide whether to fire onLightUpdate.

Nothing changes in the save format, autosave or unload behaviour otherwise.

When a loaded chunk is still saved

  • anything that called markUnsaved() after the load (block changes, block entities, light changes, structures, etc.), as before
  • scheduled block/fluid ticks are pending and game time has passed since the load (they are saved with relative delays)
  • the PDC was modified
  • the chunk was upgraded from an older DataVersion
  • heightmaps were missing or the wrong size
  • structure starts or references were dropped on load
  • the chunk is not light-correct on disk, or has blending data
  • post-processing, pending block entities or upgrade data were consumed
  • proto chunks: unchanged behaviour

Testing

Test server built from this branch and from main (26.3), normal world, no plugins, seed 20261118. I used region file header timestamps (the second 4 KiB table) to see which chunks were rewritten.

Reproduction: generate a 5x5 forceloaded area (chunks 198..202) plus its neighbours, stop, then edit on disk: one chunk saved with DataVersion - 1, one with Heightmaps removed, one with ChunkBukkitValues added. Restart, /forceload add the area, break a block in one chunk, /forceload remove, save-all flush, compare timestamps. Then another add/remove cycle with no changes. randomTickSpeed was set to 0 so untouched chunks really are untouched.

main this PR
chunk with older DataVersion rewritten rewritten (and DataVersion updated)
chunk with missing heightmaps rewritten rewritten (heightmaps present after)
chunk with a broken block rewritten rewritten
chunk with PDC, no changes rewritten every load not rewritten after a restart
load/unload with nothing changed, after restart (81 chunks: 5x5 + 2 ring) 81/81 rewritten 1/81

The one chunk still rewritten has block light at a chunk edge that is lowered by checkChunkEdges while a neighbour is not loaded yet and raised again once it is. Its light really changes while loaded and only ends up identical at save time, so it is saved, which is correct.

Simulated flight (no client: a 13x13 forceload window, i.e. view distance 6, moved 1 chunk every 0.5 s in a straight line for 100 chunks; writes counted by sampling region headers every second):

main this PR
already generated terrain, chunk writes during the flight 1813 (2121/min) 730 (855/min)
already generated terrain, incl. final unload 1989 790
new terrain, chunk writes during the flight 4475 (5090/min) 4445 (5048/min)
new terrain, incl. final unload 5421 5421

Random ticks were on (default) for this one. I did not break down the writes that remain over generated terrain; likely causes are random ticks, pending fluid ticks, and the chunk rings just outside the pre-generated strip being promoted from proto chunks to full chunks, which are all real saves. New terrain has to be saved either way, so that is unchanged.

I did not add a test, as there is no harness for chunk save behaviour that I could find.

The same code exists on ver/1.21.11; the patch should carry over with minor context changes in SerializableChunkData.read (different local variable names), but I have not tried applying it there.

@InstantlyMoist
InstantlyMoist requested a review from a team as a code owner November 18, 2026 09:14
@github-project-automation github-project-automation Bot moved this to Awaiting review in Paper PR Queue Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Awaiting review

Development

Successfully merging this pull request may close these issues.

1 participant