Skip to content

Commit 67599ae

Browse files
Merge remote-tracking branch 'upstream/main' into feature/cleanup-di-configuration
2 parents 42e3c73 + 669b403 commit 67599ae

20 files changed

Lines changed: 513 additions & 133 deletions

src/ImageSharp.Web/Caching/SHA256CacheHash.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,16 @@ public SHA256CacheHash(IOptions<ImageSharpMiddlewareOptions> options)
3333
public string Create(string value, uint length)
3434
{
3535
int byteCount = Encoding.ASCII.GetByteCount(value);
36-
37-
// Allocating a buffer from the pool is ~27% slower than stackalloc so use that for short strings
38-
if (byteCount < 257)
39-
{
40-
return HashValue(value, length, stackalloc byte[byteCount]);
41-
}
42-
4336
byte[] buffer = null;
37+
4438
try
4539
{
46-
buffer = ArrayPool<byte>.Shared.Rent(byteCount);
47-
return HashValue(value, length, buffer.AsSpan(0, byteCount));
40+
// Allocating a buffer from the pool is ~27% slower than stackalloc so use that for short strings
41+
Span<byte> bytes = byteCount <= 128
42+
? stackalloc byte[byteCount]
43+
: (buffer = ArrayPool<byte>.Shared.Rent(byteCount)).AsSpan(0, byteCount);
44+
45+
return HashValue(value, length, bytes);
4846
}
4947
finally
5048
{

src/ImageSharp.Web/Caching/UriAbsoluteCacheKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class UriAbsoluteCacheKey : ICacheKey
1313
{
1414
/// <inheritdoc/>
1515
public string Create(HttpContext context, CommandCollection commands)
16-
=> CacheKeyHelper.BuildAbsoluteKey(CacheKeyHelper.CaseHandling.None, context.Request.Host, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
16+
=> CaseHandlingUriBuilder.BuildAbsolute(CaseHandlingUriBuilder.CaseHandling.None, context.Request.Host, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
1717
}
1818
}

src/ImageSharp.Web/Caching/UriAbsoluteLowerInvariantCacheKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class UriAbsoluteLowerInvariantCacheKey : ICacheKey
1313
{
1414
/// <inheritdoc/>
1515
public string Create(HttpContext context, CommandCollection commands)
16-
=> CacheKeyHelper.BuildAbsoluteKey(CacheKeyHelper.CaseHandling.LowerInvariant, context.Request.Host, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
16+
=> CaseHandlingUriBuilder.BuildAbsolute(CaseHandlingUriBuilder.CaseHandling.LowerInvariant, context.Request.Host, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
1717
}
1818
}

src/ImageSharp.Web/Caching/UriRelativeCacheKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class UriRelativeCacheKey : ICacheKey
1313
{
1414
/// <inheritdoc/>
1515
public string Create(HttpContext context, CommandCollection commands)
16-
=> CacheKeyHelper.BuildRelativeKey(CacheKeyHelper.CaseHandling.None, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
16+
=> CaseHandlingUriBuilder.BuildRelative(CaseHandlingUriBuilder.CaseHandling.None, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
1717
}
1818
}

src/ImageSharp.Web/Caching/UriRelativeLowerInvariantCacheKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class UriRelativeLowerInvariantCacheKey : ICacheKey
1313
{
1414
/// <inheritdoc/>
1515
public string Create(HttpContext context, CommandCollection commands)
16-
=> CacheKeyHelper.BuildRelativeKey(CacheKeyHelper.CaseHandling.LowerInvariant, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
16+
=> CaseHandlingUriBuilder.BuildRelative(CaseHandlingUriBuilder.CaseHandling.LowerInvariant, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
1717
}
1818
}

src/ImageSharp.Web/Caching/CacheKeyHelper.cs renamed to src/ImageSharp.Web/CaseHandlingUriBuilder.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@
66
using System.Runtime.CompilerServices;
77
using Microsoft.AspNetCore.Http;
88

9-
namespace SixLabors.ImageSharp.Web.Caching
9+
namespace SixLabors.ImageSharp.Web
1010
{
1111
/// <summary>
12-
/// Optimized helper methods for generating cache keys from URI components. Much of this code has been adapted from the MIT licensed .NET runtime.
12+
/// Optimized helper methods for generating encoded Uris from URI components.
13+
/// Much of this code has been adapted from the MIT licensed .NET runtime.
1314
/// </summary>
14-
internal static class CacheKeyHelper
15+
public static class CaseHandlingUriBuilder
1516
{
1617
private static readonly SpanAction<char, (bool LowerInvariant, string Host, string PathBase, string Path, string Query)> InitializeAbsoluteUriStringSpanAction = new(InitializeAbsoluteUriString);
1718

19+
/// <summary>
20+
/// Provides Uri case handling options.
21+
/// </summary>
1822
public enum CaseHandling
1923
{
24+
/// <summary>
25+
/// No adjustments to casing are made.
26+
/// </summary>
2027
None,
28+
29+
/// <summary>
30+
/// All URI components are converted to lower case using the invariant culture before combining.
31+
/// </summary>
2132
LowerInvariant
2233
}
2334

@@ -29,14 +40,14 @@ public enum CaseHandling
2940
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
3041
/// <param name="query">The query, if any.</param>
3142
/// <returns>The combined URI components, properly encoded for use in HTTP headers.</returns>
32-
public static string BuildRelativeKey(
43+
public static string BuildRelative(
3344
CaseHandling handling,
3445
PathString pathBase = default,
3546
PathString path = default,
3647
QueryString query = default)
3748

3849
// Take any potential performance hit vs concatination for code reading sanity.
39-
=> BuildAbsoluteKey(handling, default, pathBase, path, query);
50+
=> BuildAbsolute(handling, default, pathBase, path, query);
4051

4152
/// <summary>
4253
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
@@ -48,7 +59,7 @@ public static string BuildRelativeKey(
4859
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
4960
/// <param name="query">The query, if any.</param>
5061
/// <returns>The combined URI components, properly encoded for use in HTTP headers.</returns>
51-
public static string BuildAbsoluteKey(
62+
public static string BuildAbsolute(
5263
CaseHandling handling,
5364
HostString host,
5465
PathString pathBase = default,
@@ -113,7 +124,7 @@ private static int CopyTextToBufferLowerInvariant(Span<char> buffer, int index,
113124
=> index + text.ToLowerInvariant(buffer.Slice(index, text.Length));
114125

115126
/// <summary>
116-
/// Initializes the URI <see cref="string"/> for <see cref="BuildAbsoluteKey(CaseHandling, HostString, PathString, PathString, QueryString)"/>.
127+
/// Initializes the URI <see cref="string"/> for <see cref="BuildAbsolute(CaseHandling, HostString, PathString, PathString, QueryString)"/>.
117128
/// </summary>
118129
/// <param name="buffer">The URI <see cref="string"/>'s <see cref="char"/> buffer.</param>
119130
/// <param name="uriParts">The URI parts.</param>

src/ImageSharp.Web/Commands/Converters/SimpleCommandConverter{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Web.Commands.Converters
1111
/// The generic converter for simple types that implement <see cref="IConvertible"/>.
1212
/// </summary>
1313
/// <typeparam name="T">The type of object to convert to.</typeparam>
14-
internal sealed class SimpleCommandConverter<T> : ICommandConverter<T>
14+
public sealed class SimpleCommandConverter<T> : ICommandConverter<T>
1515
where T : IConvertible
1616
{
1717
/// <inheritdoc/>

0 commit comments

Comments
 (0)