-
Notifications
You must be signed in to change notification settings - Fork 510
[WIP] Improve I/O operations while reading big solid archives #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ namespace SharpCompress.Archives; | |
|
|
||
| public static class IArchiveEntryExtensions | ||
| { | ||
| private const int BufferSize = 81920; | ||
| private const int BufferSize = 1048576; // 1MB buffer for better disk I/O performance | ||
|
|
||
| /// <param name="archiveEntry">The archive entry to extract.</param> | ||
| extension(IArchiveEntry archiveEntry) | ||
|
|
@@ -135,7 +135,14 @@ public void WriteToFile(string destinationFileName, ExtractionOptions? options = | |
| options, | ||
| (x, fm) => | ||
| { | ||
| using var fs = File.Open(destinationFileName, fm); | ||
| // Use larger buffer for better disk I/O performance | ||
| using var fs = new FileStream( | ||
| destinationFileName, | ||
| fm, | ||
| FileAccess.Write, | ||
| FileShare.None, | ||
| bufferSize: 1048576 | ||
| ); // 1MB buffer | ||
|
Comment on lines
+139
to
+145
|
||
| entry.WriteTo(fs); | ||
| } | ||
| ); | ||
|
|
@@ -155,7 +162,15 @@ await ExtractionMethods | |
| options, | ||
| async (x, fm, ct) => | ||
| { | ||
| using var fs = File.Open(destinationFileName, fm); | ||
| // Use async I/O with large buffer for better performance | ||
| using var fs = new FileStream( | ||
| destinationFileName, | ||
| fm, | ||
| FileAccess.Write, | ||
| FileShare.None, | ||
| bufferSize: 1048576, | ||
| useAsync: true | ||
| ); // 1MB buffer | ||
| await entry.WriteToAsync(fs, null, ct).ConfigureAwait(false); | ||
| }, | ||
| cancellationToken | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,6 +143,70 @@ public override int Read(byte[] buffer, int offset, int count) | |
| return count; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Fast skip operation that minimizes cache refills and uses larger reads. | ||
| /// Used by StreamExtensions.Skip to efficiently skip large amounts of data. | ||
| /// For non-seekable streams like LZMA, we must still read (decompress) the data, | ||
| /// but we can do it in very large chunks to minimize overhead. | ||
| /// </summary> | ||
| internal void SkipInternal(long advanceAmount) | ||
| { | ||
| if (advanceAmount <= 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // First, skip what's already in cache (free) | ||
| var inCache = _cacheLength - _cacheOffset; | ||
| if (inCache > 0) | ||
| { | ||
| var skipFromCache = (int)Math.Min(advanceAmount, inCache); | ||
| _cacheOffset += skipFromCache; | ||
| advanceAmount -= skipFromCache; | ||
| } | ||
|
|
||
| if (advanceAmount == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // For remaining data, we must actually read it from the underlying stream | ||
| // Use very large reads to minimize LZMA decompression call overhead | ||
| var skipBuffer = ArrayPool<byte>.Shared.Rent(1048576); // 1MB for skipping | ||
| try | ||
| { | ||
| while (advanceAmount > 0 && BytesLeftToRead > 0) | ||
| { | ||
| var toRead = (int) | ||
| Math.Min(Math.Min(advanceAmount, BytesLeftToRead), skipBuffer.Length); | ||
|
Comment on lines
+173
to
+181
|
||
|
|
||
| // Only seek if we're not already at the correct position | ||
| if (Stream.CanSeek && Stream.Position != origin) | ||
| { | ||
| Stream.Position = origin; | ||
| } | ||
|
|
||
| var read = Stream.Read(skipBuffer, 0, toRead); | ||
| if (read == 0) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| origin += read; | ||
| BytesLeftToRead -= read; | ||
| advanceAmount -= read; | ||
| } | ||
|
|
||
| // Invalidate cache since we skipped past it | ||
| _cacheOffset = 0; | ||
| _cacheLength = 0; | ||
| } | ||
| finally | ||
| { | ||
| ArrayPool<byte>.Shared.Return(skipBuffer); | ||
| } | ||
| } | ||
|
|
||
| public override int ReadByte() | ||
| { | ||
| if (_cacheOffset == _cacheLength) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting BufferSize to 1MB affects CopyTo/CopyToAsync below, which will allocate a new 1MB byte[] per call (LOH) in the framework implementation. This can significantly increase GC pressure for archives with many (especially small) entries. Consider keeping the CopyTo buffer below the LOH threshold or using a pooled-buffer copy implementation so the larger buffer doesn’t allocate per extraction call.