Registry Mirror
Skip to content

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Writing through a mock FileStream copied the data three times: into the MemoryStream, into a temporary array on flush, and into the new container content. Disposing copied the stream buffer once more, although nothing writes to it afterwards.

The flush now passes the stream buffer with index and count to IStorageContainer.WriteRange, so the temporary copy is gone, and on dispose the container takes over the buffer when it holds exactly the new file content. BytesChangedEventArgs carries the new container content with offset and count, so other streams on the same file copy only the changed range. All touched types are internal.

This also fixes a bug: after opening a 5-byte file, writing 10 bytes and calling SetLength(5), the mock kept a length of 10 and persisted the cut-off part of the pending write, while the real file system keeps 5 bytes. The new test SetLength_Truncate_AfterPendingWriteBeyondNewLength_ShouldPersistNewLength passes on all file system rows, including the real file system.

Scenario Before After System.IO.Abstractions
Create and write 4 KB 17.4 KB 9.1 KB 14.7 KB
Create and write 64 KB 202 KB / 28.6 μs 71 KB / 13.5 μs 138 KB / ~50 μs
Create and write 1 MB 3.15 MB / 472 μs 1.05 MB / 126 μs 2.10 MB / ~750 μs
Create and 16 writes of 4 KB 272 KB 141 KB ~200 KB

Each flush copied the pending range into a temporary array before the container copied it again, and disposing copied the stream buffer even though nothing writes to it afterwards. Requiring the pending writes to end within the stream length also stops a SetLength truncation from persisting the cut-off part of a pending write.
@vbreuss vbreuss self-assigned this Nov 28, 2026
@mergify

mergify Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

This pull request is merged.

@greptile-apps

greptile-apps Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; the previous buffer-sharing issue is fixed.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous buffer-sharing issue is fixed.

Summary

The PR reduces copies when mock file streams flush and fixes truncation after pending writes.

  • Mock file streams send buffer ranges to storage and reuse the buffer on close.
  • Truncating a mock stream saves only bytes within its new length.

Reviews (2) · Last reviewed commit: "fix: close the stream before the file ta..." · Reviewed by Greptile

@vbreuss
vbreuss enabled auto-merge (squash) November 28, 2026 14:45
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss merged commit 08688d5 into main Nov 28, 2026
20 checks passed
@vbreuss
vbreuss deleted the perf/filestream-copies branch November 28, 2026 17:30
@github-actions

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main, which ran on the same runner. Times that change by more than 10% are marked.

Memory

Benchmark main PR Δ
Complete 114,697 B 114,609 B 🟢 -88 B (±0%)
Enumerate (N=1000&Pattern=*) 326,950 B 326,951 B 🔴 +1 B (±0%)
CompleteTestBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 62.32 μs 1.187 μs 0.991 μs 1.00 111.92 KB 1.00
SystemIOAbstractions 87.95 μs 0.936 μs 0.875 μs 1.41 166.7 KB 1.49
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 3.08GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

MockCreationBenchmarks
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 3.732 μs 0.0261 μs 0.0232 μs 1.00 15.96 KB 1.00
SystemIOAbstractions 3.930 μs 0.0287 μs 0.0255 μs 1.05 8.15 KB 0.51
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 2.60GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

EnumerationBenchmarks
Enumerate N Pattern Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 10 ***** 4.182 μs 0.0349 μs 0.0327 μs 1.00 5.9 KB 1.00
SystemIOAbstractions 10 * 3.052 μs 0.0205 μs 0.0171 μs 0.73 3.93 KB 0.67
Testably 10 *.txt 4.341 μs 0.0536 μs 0.0502 μs 1.00 5.59 KB 1.00
SystemIOAbstractions 10 *.txt 4.853 μs 0.0651 μs 0.0578 μs 1.12 4.24 KB 0.76
Testably 1000 ***** 469.506 μs 3.9190 μs 3.4740 μs 1.00 319.29 KB 1.00
SystemIOAbstractions 1000 * 186.491 μs 1.6811 μs 1.4038 μs 0.40 70.45 KB 0.22
Testably 1000 *.txt 485.100 μs 3.4365 μs 3.0464 μs 1.00 284.52 KB 1.00
SystemIOAbstractions 1000 *.txt 353.493 μs 1.7164 μs 1.6055 μs 0.73 66.89 KB 0.24
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.51GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

github-actions Bot added a commit that referenced this pull request Nov 29, 2026
…en flushing a mock file stream (#1242) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Nov 29, 2026
…en flushing a mock file stream (#1242) by Valentin Breuß
@github-actions

Copy link
Copy Markdown

This is addressed in release v8.1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant