Skip to content
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

Make stream buffer size configurable. #1286

Merged
merged 6 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to contribute to ImageSharp
# How to contribute to SixLabors.ImageSharp

#### **Did you find a bug?**

Expand All @@ -12,11 +12,11 @@

* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.

* Before submitting, please ensure that your code matches the existing coding patterns and practise as demonstrated in the repository. These follow strict Stylecop rules :cop:.
* Before submitting, please ensure that your code matches the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules :cop:.

#### **Do you intend to add a new feature or change an existing one?**

* Suggest your change in the [ImageSharp Gitter Chat Room](https://gitter.im/ImageSharp/General) and start writing code.
* Suggest your change in the [Ideas Discussions Channel](https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AIdeas) and start writing code.

* Do not open an issue on GitHub until you have collected positive feedback about the change. GitHub issues are primarily intended for bug reports and fixes.

Expand All @@ -33,14 +33,12 @@

#### **Do you have questions about consuming the library or the source code?**

* Ask any question about how to use ImageSharp over in the [discussions section](https://github.com/SixLabors/ImageSharp/discussions).
* Ask any question about how to use SixLabors.ImageSharp in the [Help Discussions Channel](https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AHelp).

#### Code of Conduct
This project has adopted the code of conduct defined by the [Contributor Covenant](https://contributor-covenant.org/) to clarify expected behavior in our community.
For more information, see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).

And please remember. ImageSharp is the work of a very, very, small number of developers who struggle balancing time to contribute to the project with family time and work commitments. We encourage you to pitch in and help make our vision of simple accessible imageprocessing available to all. Open Source can only exist with your help.
And please remember. SixLabors.ImageSharp is the work of a very, very, small number of developers who struggle balancing time to contribute to the project with family time and work commitments. We encourage you to pitch in and help make our vision of simple accessible image processing available to all. Open Source can only exist with your help.

Thanks for reading!

James Jackson-South :heart:
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
blank_issues_enabled: false
contact_links:
- name: Ask a Question
url: https://github.com/SixLabors/ImageSharp/discussions/new?category_id=6331980
url: https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AHelp
about: Ask a question about this project.
- name: Feature Request
url: https://github.com/SixLabors/ImageSharp/discussions/new?category_id=6331981
url: https://github.com/SixLabors/ImageSharp/discussions?discussions_q=category%3AIdeas
about: Share ideas for new features for this project.
23 changes: 21 additions & 2 deletions src/ImageSharp/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Http;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Gif;
Expand All @@ -26,7 +25,8 @@ public sealed class Configuration
/// A lazily initialized configuration default instance.
/// </summary>
private static readonly Lazy<Configuration> Lazy = new Lazy<Configuration>(CreateDefaultInstance);

private const int DefaultStreamProcessingBufferSize = 8096;
private int streamProcessingBufferSize = DefaultStreamProcessingBufferSize;
private int maxDegreeOfParallelism = Environment.ProcessorCount;

/// <summary>
Expand Down Expand Up @@ -75,6 +75,24 @@ public int MaxDegreeOfParallelism
}
}

/// <summary>
/// Gets or sets the size of the buffer to use when working with streams.
/// Intitialized with <see cref="DefaultStreamProcessingBufferSize"/> by default.
/// </summary>
public int StreamProcessingBufferSize
{
get => this.streamProcessingBufferSize;
set
{
if (value <= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer size 1 and 2 will not work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll have a look at your tests and fix up

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're missing some guard sanitation on the Position property but the problem is really with the tests. They're expecting a minimum length.

Copy link
Member

@antonfirsov antonfirsov Jul 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main question: would Image.Load[Async] work with buffer sizes like 1, 2, 3, 503, 719 ? If not, the setter should throw.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll work with any length. Was just bad tests.

{
throw new ArgumentOutOfRangeException(nameof(this.StreamProcessingBufferSize));
}

this.streamProcessingBufferSize = value;
}
}

/// <summary>
/// Gets a set of properties for the Congiguration.
/// </summary>
Expand Down Expand Up @@ -145,6 +163,7 @@ public Configuration Clone()
return new Configuration
{
MaxDegreeOfParallelism = this.MaxDegreeOfParallelism,
StreamProcessingBufferSize = this.StreamProcessingBufferSize,
ImageFormatsManager = this.ImageFormatsManager,
MemoryAllocator = this.MemoryAllocator,
ImageOperationsProvider = this.ImageOperationsProvider,
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Bmp/BmpDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)

try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Decode<TPixel>(bufferedStream);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -64,7 +64,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration

try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -84,7 +84,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
{
Guard.NotNull(stream, nameof(stream));

using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return new BmpDecoderCore(configuration, this).Identify(bufferedStream);
}

Expand All @@ -93,7 +93,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream
{
Guard.NotNull(stream, nameof(stream));

using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return new BmpDecoderCore(configuration, this).IdentifyAsync(bufferedStream);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Gif/GifDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)

try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Decode<TPixel>(bufferedStream);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -59,7 +59,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration

try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -84,7 +84,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)

var decoder = new GifDecoderCore(configuration, this);

using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Identify(bufferedStream);
}

Expand All @@ -95,7 +95,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream

var decoder = new GifDecoderCore(configuration, this);

using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.IdentifyAsync(bufferedStream);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
using var decoder = new JpegDecoderCore(configuration, this);
try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Decode<TPixel>(bufferedStream);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -53,7 +53,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration
using var decoder = new JpegDecoderCore(configuration, this);
try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -77,7 +77,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
Guard.NotNull(stream, nameof(stream));

using var decoder = new JpegDecoderCore(configuration, this);
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);

return decoder.Identify(bufferedStream);
}
Expand All @@ -88,7 +88,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream
Guard.NotNull(stream, nameof(stream));

using var decoder = new JpegDecoderCore(configuration, this);
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);

return decoder.IdentifyAsync(bufferedStream);
}
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Png/PngDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)

try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Decode<TPixel>(bufferedStream);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -50,7 +50,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration

try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -71,15 +71,15 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration
public IImageInfo Identify(Configuration configuration, Stream stream)
{
var decoder = new PngDecoderCore(configuration, this);
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Identify(bufferedStream);
}

/// <inheritdoc/>
public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream)
{
var decoder = new PngDecoderCore(configuration, this);
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.IdentifyAsync(bufferedStream);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/ImageSharp/Formats/Png/PngDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Formats.Png.Chunks;
using SixLabors.ImageSharp.Formats.Png.Filters;
using SixLabors.ImageSharp.Formats.Png.Zlib;
Expand Down Expand Up @@ -1027,7 +1026,7 @@ private void ReadInternationalTextChunk(PngMetadata metadata, ReadOnlySpan<byte>
private bool TryUncompressTextData(ReadOnlySpan<byte> compressedData, Encoding encoding, out string value)
{
using (var memoryStream = new MemoryStream(compressedData.ToArray()))
using (var bufferedStream = new BufferedReadStream(memoryStream))
using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStream))
using (var inflateStream = new ZlibInflateStream(bufferedStream))
{
if (!inflateStream.AllocateNewBytes(compressedData.Length, false))
Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Tga/TgaDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)

try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return decoder.Decode<TPixel>(bufferedStream);
}
catch (InvalidMemoryOperationException ex)
Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration

try
{
using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return await decoder.DecodeAsync<TPixel>(bufferedStream).ConfigureAwait(false);
}
catch (InvalidMemoryOperationException ex)
Expand All @@ -75,7 +75,7 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
{
Guard.NotNull(stream, nameof(stream));

using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return new TgaDecoderCore(configuration, this).Identify(bufferedStream);
}

Expand All @@ -84,7 +84,7 @@ public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream
{
Guard.NotNull(stream, nameof(stream));

using var bufferedStream = new BufferedReadStream(stream);
using var bufferedStream = new BufferedReadStream(configuration, stream);
return new TgaDecoderCore(configuration, this).IdentifyAsync(bufferedStream);
}
}
Expand Down
Loading