Skip to content

Commit

Permalink
Merge pull request #185 from adamhathcock/ppmd_allocation_zipwriter
Browse files Browse the repository at this point in the history
Make PpmdProperties lazy to avoid unnecessary allocations.
  • Loading branch information
adamhathcock authored Oct 3, 2016
2 parents e5ee399 + 671f9cd commit c73ac20
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The major feature is support for non-seekable streams so large files can be proc
## Need Help?
Post Issues on Github!

Check the [Supported Formats](FORMATS.md) and [basic usage.](USAGE.md)
Check the [Supported Formats](FORMATS.md) and [Basic Usage.](USAGE.md)

## A Simple Request

Expand Down
7 changes: 0 additions & 7 deletions src/SharpCompress/Compressors/PPMd/PpmdProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@

namespace SharpCompress.Compressors.PPMd
{
public enum PpmdVersion
{
H,
H7z,
I1
}

public class PpmdProperties
{
public PpmdVersion Version = PpmdVersion.I1;
Expand Down
9 changes: 9 additions & 0 deletions src/SharpCompress/Compressors/PPMd/PpmdVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SharpCompress.Compressors.PPMd
{
public enum PpmdVersion
{
H,
H7z,
I1
}
}
18 changes: 15 additions & 3 deletions src/SharpCompress/Writers/Zip/ZipWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class ZipWriter : AbstractWriter
{
private readonly CompressionType compressionType;
private readonly CompressionLevel compressionLevel;
private readonly PpmdProperties ppmdProperties = new PpmdProperties(); // Caching properties to speed up PPMd
private readonly List<ZipCentralDirectoryEntry> entries = new List<ZipCentralDirectoryEntry>();
private readonly string zipComment;
private long streamPosition;
private PpmdProperties ppmdProps;

public ZipWriter(Stream destination, ZipWriterOptions zipWriterOptions)
: base(ArchiveType.Zip)
Expand All @@ -34,6 +34,18 @@ public ZipWriter(Stream destination, ZipWriterOptions zipWriterOptions)
InitalizeStream(destination, !zipWriterOptions.LeaveStreamOpen);
}

private PpmdProperties PpmdProperties
{
get
{
if (ppmdProps == null)
{
ppmdProps = new PpmdProperties();
}
return ppmdProps;
}
}

protected override void Dispose(bool isDisposing)
{
if (isDisposing)
Expand Down Expand Up @@ -252,8 +264,8 @@ private Stream GetWriteStream(Stream writeStream)
}
case ZipCompressionMethod.PPMd:
{
counting.Write(writer.ppmdProperties.Properties, 0, 2);
return new PpmdStream(writer.ppmdProperties, counting, true);
counting.Write(writer.PpmdProperties.Properties, 0, 2);
return new PpmdStream(writer.PpmdProperties, counting, true);
}
default:
{
Expand Down

0 comments on commit c73ac20

Please sign in to comment.