Skip to content

Commit

Permalink
Reduce SharpCompress allocs some more
Browse files Browse the repository at this point in the history
  • Loading branch information
FenPhoenix committed Sep 4, 2023
1 parent a27af01 commit 6823237
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 2 additions & 0 deletions SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace SharpCompress.Archives.SevenZip;

Expand Down Expand Up @@ -38,6 +39,7 @@ public DateTime? LastModifiedTime

public bool IsDirectory;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Reset()
{
HasStream = true;
Expand Down
7 changes: 4 additions & 3 deletions SharpCompress/Common/SevenZip/ArchiveDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using SharpCompress.Archives.SevenZip;
using static AL_Common.Common;

Expand All @@ -13,17 +13,18 @@ internal sealed class ArchiveDatabase
internal byte _minorVersion;
internal long _startPositionAfterHeader;

internal readonly List<CFolder> _folders = new();
internal readonly ListFast<CFolder> _folders = new(0);
internal readonly ListFast<int> _numUnpackStreamsVector = new(0);
internal readonly ListFast<SevenZipArchiveEntry> _files = new(0);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Clear()
{
_majorVersion = 0;
_minorVersion = 0;
_startPositionAfterHeader = 0;

_folders.Clear();
_folders.ClearFast();
_numUnpackStreamsVector.ClearFast();
_files.ClearFast();
}
Expand Down
25 changes: 18 additions & 7 deletions SharpCompress/Common/SevenZip/ArchiveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private void ReadAttributeVector(
private void GetNextFolderItem(CFolder folder)
{
int numCoders = ReadNum();
folder._coders = new List<CCoderInfo>(numCoders);
folder._coders.ClearAndEnsureCapacity(numCoders);
int numInStreams = 0;
int numOutStreams = 0;
for (int i = 0; i < numCoders; i++)
Expand Down Expand Up @@ -319,7 +319,7 @@ private void GetNextFolderItem(CFolder folder)
}

int numBindPairs = numOutStreams - 1;
folder._bindPairs = new List<CBindPair>(numBindPairs);
folder._bindPairs.ClearAndEnsureCapacity(numBindPairs);
for (int i = 0; i < numBindPairs; i++)
{
var bp = new CBindPair
Expand Down Expand Up @@ -419,12 +419,21 @@ private void ReadUnpackInfo(List<byte[]> dataVector)
{
streamSwitch.Set(this, dataVector);

db._folders.ClearAndEnsureCapacity(numFolders);
db._folders.SetRecycleState(numFolders, 25_000);
for (int i = 0; i < numFolders; i++)
{
var f = new CFolder();
db._folders.Add(f);
GetNextFolderItem(f);
CFolder folder = db._folders[i];
if (folder != null)
{
folder.Reset();
}
else
{
folder = new CFolder();
db._folders[i] = folder;
}

GetNextFolderItem(folder);
}
}

Expand Down Expand Up @@ -609,8 +618,10 @@ out _

var dataVector = new List<byte[]>(db._folders.Count);
const int packIndex = 0;
foreach (CFolder folder in db._folders)
for (int folderIndex = 0; folderIndex < db._folders.Count; folderIndex++)
{
CFolder folder = db._folders[folderIndex];

long oldDataStartPos = dataStartPos;
int myPackSizesLength = folder._packStreams.Count;
long[] myPackSizes = _context.LongArrayPool.Rent(myPackSizesLength);
Expand Down
15 changes: 13 additions & 2 deletions SharpCompress/Common/SevenZip/CFolder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using AL_Common;
using SharpCompress.Archives.SevenZip;
using SharpCompress.Compressors.LZMA;
Expand All @@ -8,14 +9,24 @@ namespace SharpCompress.Common.SevenZip;

internal sealed class CFolder
{
internal List<CCoderInfo> _coders = new();
internal List<CBindPair> _bindPairs = new();
internal readonly List<CCoderInfo> _coders = new();
internal readonly List<CBindPair> _bindPairs = new();
internal readonly List<int> _packStreams = new();
internal readonly List<long> _unpackSizes = new();
internal uint? _unpackCrc;

internal bool UnpackCrcDefined => _unpackCrc != null;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Reset()
{
_coders.Clear();
_bindPairs.Clear();
_packStreams.Clear();
_unpackSizes.Clear();
_unpackCrc = null;
}

public long GetUnpackSize()
{
if (_unpackSizes.Count == 0)
Expand Down

0 comments on commit 6823237

Please sign in to comment.