Skip to content

Commit e5ab7d9

Browse files
Remove weird memoryallocator injection
1 parent 17d65a7 commit e5ab7d9

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

src/ImageSharp.Web/Caching/CacheHash.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Buffers;
56
using System.Runtime.CompilerServices;
67
using System.Security.Cryptography;
78
using System.Text;
@@ -18,19 +19,14 @@ namespace SixLabors.ImageSharp.Web.Caching
1819
/// </summary>
1920
public sealed class CacheHash : ICacheHash
2021
{
21-
private readonly MemoryAllocator memoryAllocator;
22-
2322
/// <summary>
2423
/// Initializes a new instance of the <see cref="CacheHash"/> class.
2524
/// </summary>
2625
/// <param name="options">The middleware configuration options.</param>
27-
/// <param name="memoryAllocator">The memory allocator.</param>
28-
public CacheHash(IOptions<ImageSharpMiddlewareOptions> options, MemoryAllocator memoryAllocator)
26+
public CacheHash(IOptions<ImageSharpMiddlewareOptions> options)
2927
{
3028
Guard.NotNull(options, nameof(options));
3129
Guard.MustBeBetweenOrEqualTo<uint>(options.Value.CachedNameLength, 2, 64, nameof(options.Value.CachedNameLength));
32-
33-
this.memoryAllocator = memoryAllocator;
3430
}
3531

3632
/// <inheritdoc/>
@@ -46,8 +42,19 @@ public string Create(string value, uint length)
4642
return HashValue(value, length, stackalloc byte[byteCount]);
4743
}
4844

49-
using IManagedByteBuffer buffer = this.memoryAllocator.AllocateManagedByteBuffer(byteCount);
50-
return HashValue(value, length, buffer.Memory.Span);
45+
byte[] buffer = null;
46+
try
47+
{
48+
buffer = ArrayPool<byte>.Shared.Rent(byteCount);
49+
return HashValue(value, length, buffer.AsSpan(0, byteCount));
50+
}
51+
finally
52+
{
53+
if (buffer != null)
54+
{
55+
ArrayPool<byte>.Shared.Return(buffer);
56+
}
57+
}
5158
}
5259

5360
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/ImageSharp.Web/DependencyInjection/ImageSharpBuilderExtensions.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -282,22 +282,6 @@ public static IImageSharpBuilder Configure<TOptions>(this IImageSharpBuilder bui
282282
return builder;
283283
}
284284

285-
/// <summary>
286-
/// Sets the memory allocator configured in <see cref="Configuration.MemoryAllocator"/> of <see cref="ImageSharpMiddlewareOptions.Configuration"/>.
287-
/// </summary>
288-
/// <param name="builder">The core builder.</param>
289-
/// <returns>The <see cref="IImageSharpBuilder"/>.</returns>
290-
internal static IImageSharpBuilder SetMemoryAllocatorFromMiddlewareOptions(this IImageSharpBuilder builder)
291-
{
292-
static MemoryAllocator AllocatorFactory(IServiceProvider s)
293-
{
294-
return s.GetRequiredService<IOptions<ImageSharpMiddlewareOptions>>().Value.Configuration.MemoryAllocator;
295-
}
296-
297-
builder.SetMemoryAllocator(AllocatorFactory);
298-
return builder;
299-
}
300-
301285
/// <summary>
302286
/// Sets the <see cref="FormatUtilities"/> configured by <see cref="ImageSharpMiddlewareOptions.Configuration"/>.
303287
/// </summary>

src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ private static void AddDefaultServices(
5454
{
5555
builder.Services.Configure(setupAction);
5656

57-
builder.SetMemoryAllocatorFromMiddlewareOptions();
5857
builder.SetFormatUtilitesFromMiddlewareOptions();
5958

6059
builder.SetRequestParser<QueryCollectionRequestParser>();

tests/ImageSharp.Web.Benchmarks/Caching/CacheHashBenchmarks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CacheHashBenchmarks
1313
{
1414
private const string URL = "http://testwebsite.com/image-12345.jpeg?width=400";
1515
private static readonly IOptions<ImageSharpMiddlewareOptions> MWOptions = Options.Create(new ImageSharpMiddlewareOptions());
16-
private static readonly CacheHash Sha256Hasher = new CacheHash(MWOptions, MWOptions.Value.Configuration.MemoryAllocator);
16+
private static readonly CacheHash Sha256Hasher = new CacheHash(MWOptions);
1717
private static readonly CacheHashBaseline NaiveSha256Hasher = new CacheHashBaseline();
1818

1919
[Benchmark(Baseline = true, Description = "Baseline Sha256Hasher")]

tests/ImageSharp.Web.Tests/Caching/CacheHashTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System.Text;
45
using Microsoft.Extensions.Options;
56
using SixLabors.ImageSharp.Web.Caching;
67
using SixLabors.ImageSharp.Web.Middleware;
@@ -13,7 +14,7 @@ namespace SixLabors.ImageSharp.Web.Tests.Caching
1314
public class CacheHashTests
1415
{
1516
private static readonly IOptions<ImageSharpMiddlewareOptions> Options = MSOptions.Create(new ImageSharpMiddlewareOptions());
16-
private static readonly ICacheHash CacheHash = new CacheHash(Options, Options.Value.Configuration.MemoryAllocator);
17+
private static readonly ICacheHash CacheHash = new CacheHash(Options);
1718

1819
[Fact]
1920
public void CachHashProducesIdenticalResults()
@@ -42,11 +43,28 @@ public void CachHashLengthIsIdentical()
4243
const int Length = 12;
4344
const string Input = "http://testwebsite.com/image-12345.jpeg?width=400";
4445
const string Input2 = "http://testwebsite.com/image-12345.jpeg";
46+
string input3 = CreateLongString();
47+
4548
int expected = CacheHash.Create(Input, Length).Length;
4649
int actual = CacheHash.Create(Input2, Length).Length;
50+
int actual2 = CacheHash.Create(input3, Length).Length;
4751

4852
Assert.Equal(expected, actual);
4953
Assert.Equal(Length, actual);
54+
Assert.Equal(Length, actual2);
55+
}
56+
57+
private static string CreateLongString()
58+
{
59+
const int Length = 2048;
60+
var sb = new StringBuilder(Length);
61+
62+
for (int i = 0; i < Length; i++)
63+
{
64+
sb.Append(i.ToString());
65+
}
66+
67+
return sb.ToString();
5068
}
5169
}
5270
}

0 commit comments

Comments
 (0)