Repository navigation
Don't re-save chunks that were only loaded - #14498
Open
InstantlyMoist wants to merge 1 commit into
Open
InstantlyMoist wants to merge 1 commit into
InstantlyMoist wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
LastUpdatechanged.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.saveChunkwrites the chunk ifchunk.isUnsaved(). Every chunk read from disk already was:SerializableChunkData.readrestores the chunk through setters that callmarkUnsaved():setLightCorrect,setAllStarts,setAllReferences.readalso doespersistentDataContainer.putAll(ChunkBukkitValues), andDirtyCraftPersistentDataContainer.putAllsets the dirty flag, so any chunk with PDC data was unsaved throughisUnsaved() = unsaved || pdc.dirty().ServerChunkCache.onLightUpdate > ChunkHolder.sectionLightChanged > ImposterProtoChunk.markUnsavedfor 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 byreaditself are cleared, unless the load changed something that would be written:Proto chunks are not touched; they are marked unsaved by
setPersistedStatusas before.DataConverter (
ChunkLoadTask): the DataVersion is checked beforeupgradeChunkTag, 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, buttryMarkSavedonly cleared the PDC dirty flag ifunsavedwas 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.checkNullSectioncallsinitNibble(..., initRemovedNibbles = true), which puts a newSWMRNibbleArrayinto the engine's nibble cache in place of a null nibble thatrewriteNibbleCacheForSkylightremoved from the cache, so light can propagate through it. That nibble only exists in the cache and is dropped indestroyCaches: the only places nibbles are written back to a chunk (light,relight) use their own arrays, andgetNibblesForChunkFromCacheis unused. The chunk's real nibble for that section is out of the cache, so nothing can modify it during the operation. YetupdateVisiblereported the cache-only nibble as changed and firedonLightUpdatefor 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.updateVisiblereturned 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 byStarLightEngine.updateVisibleto decide whether to fireonLightUpdate.Nothing changes in the save format, autosave or unload behaviour otherwise.
When a loaded chunk is still saved
markUnsaved()after the load (block changes, block entities, light changes, structures, etc.), as beforeTesting
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
Heightmapsremoved, one withChunkBukkitValuesadded. Restart,/forceload addthe area, break a block in one chunk,/forceload remove,save-all flush, compare timestamps. Then another add/remove cycle with no changes.randomTickSpeedwas set to 0 so untouched chunks really are untouched.The one chunk still rewritten has block light at a chunk edge that is lowered by
checkChunkEdgeswhile 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):
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 inSerializableChunkData.read(different local variable names), but I have not tried applying it there.