Skip to content

SkipLocalsInit on methods with stackalloc #32567

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

Merged
5 commits merged into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Microsoft.AspNetCore.DataProtection.IDataProtectionProvider
Microsoft.AspNetCore.DataProtection.IDataProtector</Description>
<TargetFrameworks>$(DefaultNetFxTargetFramework);netstandard2.0;$(DefaultNetCoreTargetFramework)</TargetFrameworks>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;dataprotection</PackageTags>
<Nullable>enable</Nullable>
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Routing/src/Matching/DfaMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing.Patterns;
Expand All @@ -27,6 +28,7 @@ public DfaMatcher(ILogger<DfaMatcher> logger, EndpointSelector selector, DfaStat
_isDefaultEndpointSelector = selector is DefaultEndpointSelector;
}

[SkipLocalsInit]
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, you'll get better performance if you change line 48 to use a const value rather than a variable value. If it's impractical to do so, no change needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I noticed that. I don't want to mess with it in this PR: #32569 😄

public sealed override Task MatchAsync(HttpContext httpContext)
{
if (httpContext == null)
Expand Down
1 change: 1 addition & 0 deletions src/Http/Routing/src/Microsoft.AspNetCore.Routing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Microsoft.AspNetCore.Routing.Route
Microsoft.AspNetCore.Routing.RouteCollection</Description>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;routing</PackageTags>
<IsPackable>false</IsPackable>
Expand Down
3 changes: 2 additions & 1 deletion src/Http/WebUtilities/src/FormPipeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ private void ThrowValueTooLargeException()
throw new InvalidDataException($"Form value length limit {ValueLengthLimit} exceeded.");
}

[SkipLocalsInit]
private string GetDecodedStringFromReadOnlySequence(in ReadOnlySequence<byte> ros)
{
if (ros.IsSingleSegment)
Expand All @@ -341,7 +342,7 @@ private string GetDecodedStringFromReadOnlySequence(in ReadOnlySequence<byte> ro

if (ros.Length < StackAllocThreshold)
{
Span<byte> buffer = stackalloc byte[(int)ros.Length];
Span<byte> buffer = stackalloc byte[StackAllocThreshold].Slice(0, (int)ros.Length);
ros.CopyTo(buffer);
return GetDecodedString(buffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<DefineConstants>$(DefineConstants);WebEncoders_In_WebUtilities</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore</PackageTags>
<IsPackable>false</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private void AppendContentLength(ReadOnlySpan<byte> value)
}

[MethodImpl(MethodImplOptions.NoInlining)]
[SkipLocalsInit]
private void AppendContentLengthCustomEncoding(ReadOnlySpan<byte> value, Encoding? customEncoding)
{
if (_contentLength.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,11 @@ private bool TryValidateAuthorityAndHost(out string hostText)
return true;
}

[SkipLocalsInit]
private bool TryValidatePath(ReadOnlySpan<char> pathSegment)
{
// Must start with a leading slash
if (pathSegment.Length == 0 || pathSegment[0] != '/')
if (pathSegment.IsEmpty || pathSegment[0] != '/')
{
ResetAndAbort(new ConnectionAbortedException(CoreStrings.FormatHttp2StreamErrorPathInvalid(RawTarget)), Http2ErrorCode.PROTOCOL_ERROR);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO.Pipelines;
using System.Net.Http;
using System.Net.Http.QPack;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
Expand Down Expand Up @@ -739,10 +740,11 @@ private bool TryValidateAuthorityAndHost(out string hostText)
return true;
}

[SkipLocalsInit]
private bool TryValidatePath(ReadOnlySpan<char> pathSegment)
{
// Must start with a leading slash
if (pathSegment.Length == 0 || pathSegment[0] != '/')
if (pathSegment.IsEmpty || pathSegment[0] != '/')
{
Abort(new ConnectionAbortedException(CoreStrings.FormatHttp3StreamErrorPathInvalid(RawTarget)), Http3ErrorCode.ProtocolError);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ protected override void OnEventCommand(EventCommandEventArgs command)
}

[NonEvent]
[SkipLocalsInit]
private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, string? arg3, string? arg4, string? arg5)
{
const int EventDataCount = 5;
Expand Down
8 changes: 6 additions & 2 deletions src/Shared/WebEncoders/WebEncoders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endif
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.WebEncoders.Sources;

#if WebEncoders_In_WebUtilities
Expand Down Expand Up @@ -343,8 +344,11 @@ public static int GetArraySizeRequiredToEncode(int count)
/// </summary>
/// <param name="input">The binary input to encode.</param>
/// <returns>The base64url-encoded form of <paramref name="input"/>.</returns>
[SkipLocalsInit]
public static string Base64UrlEncode(ReadOnlySpan<byte> input)
{
const int StackAllocThreshold = 128;

if (input.IsEmpty)
{
return string.Empty;
Expand All @@ -353,8 +357,8 @@ public static string Base64UrlEncode(ReadOnlySpan<byte> input)
int bufferSize = GetArraySizeRequiredToEncode(input.Length);

char[]? bufferToReturnToPool = null;
Span<char> buffer = bufferSize <= 128
? stackalloc char[bufferSize]
Span<char> buffer = bufferSize <= StackAllocThreshold
? stackalloc char[StackAllocThreshold]
: bufferToReturnToPool = ArrayPool<char>.Shared.Rent(bufferSize);

var numBase64Chars = Base64UrlEncode(input, buffer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Components for providing real-time bi-directional communication across the Web.</Description>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<IsAspNetCoreApp>true</IsAspNetCoreApp>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down