feat: lazily allocate FileContent chunks, sparse until first write - #164
Merged
Merged
Conversation
Growing a file (create, resize, extend) used to eagerly new byte[] every chunk it now spans, even though the whole point of growth is to expose more zero bytes. Represent an untouched chunk as null instead and let read paths (ReadTo/CopyTo/HashInto/ToArray/ChunkReadStream) satisfy it from a shared static zero buffer without allocating; a chunk only materializes the first time WriteFrom/WriteManaged/FillFromStream actually writes into it. Clone() preserves sparseness for chunks the source never wrote to. This only affects in-memory representation - persisted .mdr images and snapshot blobs still serialize full byte ranges, so a reloaded file re-materializes fully; the memory saving applies within a single mount session for freshly created/extended/truncated regions.
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.
Summary
FileContent's chunk list (List<byte[]>) becomesList<byte[]?>. Growth (CreateZeroed/Resize) no longer eagerlynew byte[]s every chunk it now spans — an untouched chunk staysnull(sparse), since the whole point of growth is exposing more zero bytes and a null entry already reads as zero.ReadTo,CopyTo,HashInto,ToArray,ChunkReadStream.Read) satisfy a sparse chunk from a shared static all-zero buffer without allocating.WriteFrom,WriteManaged,FillFromStream) materialize a chunk (via a newAllocateChunkhelper, right-sized for the terminal chunk) only the first time something is actually written into it.Clone()preserves sparseness: a chunk the source never wrote to stays sparse in the clone too, instead of eagerly copying/materializing it.FileNodeMap.GetTotalAllocated, the low-memory guard from feat: reject RAM disk growth when system memory runs low #163) is unaffected — both already key off configured/logical allocation size, not materialized chunk bytes, so nothing needed to change there..mdrimage save/load and snapshot blobs still serialize full byte ranges (no on-disk hole/sparse encoding), so a reloaded file re-materializes fully — the memory saving applies within a single mount session, mainly to freshly created, extended, or truncated-then-regrown regions that are never actually written.Test plan
dotnet build -c Release— 0 warnings, 0 errorsdotnet test tests/ManagedDrive.Tests -c Release --filter "FullyQualifiedName~FileContentTests"— 22/22 passed (3 existing tests updated since they previously asserted a freshly created file is already fully materialized; ~6 new tests added covering sparse creation, lazy materialization on write, sparse reads, growth-across-chunk-boundary, and clone sparseness)dotnet test tests/ManagedDrive.Tests -c Release— 380/380 passeddotnet format --verify-no-changes --no-restore— clean