HTTP headers enumerator move directly to next (#37538)

This commit is contained in:
James Newton-King 2021-10-15 09:50:44 +13:00 коммит произвёл GitHub
Родитель 3239228949
Коммит 0e5df998b5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 473 добавлений и 803 удалений

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -6,6 +6,7 @@ using System.Buffers.Text;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
@ -222,7 +223,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
public partial struct Enumerator : IEnumerator<KeyValuePair<string, StringValues>>
{
private readonly HttpRequestHeaders _collection;
private readonly long _bits;
private long _currentBits;
private int _next;
private KeyValuePair<string, StringValues> _current;
private readonly bool _hasUnknown;
@ -231,8 +232,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
internal Enumerator(HttpRequestHeaders collection)
{
_collection = collection;
_bits = collection._bits;
_next = 0;
_currentBits = collection._bits;
_next = _currentBits != 0 ? BitOperations.TrailingZeroCount(_currentBits) : -1;
_current = default;
_hasUnknown = collection.MaybeUnknown != null;
_unknownEnumerator = _hasUnknown
@ -250,7 +251,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
public void Reset()
{
_next = 0;
_currentBits = _collection._bits;
_next = _currentBits != 0 ? BitOperations.TrailingZeroCount(_currentBits) : -1;
}
}
}

Просмотреть файл

@ -7,6 +7,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Collections;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.Primitives;
@ -142,7 +143,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
public partial struct Enumerator : IEnumerator<KeyValuePair<string, StringValues>>
{
private readonly HttpResponseHeaders _collection;
private readonly long _bits;
private long _currentBits;
private int _next;
private KeyValuePair<string, StringValues> _current;
private KnownHeaderType _currentKnownType;
@ -152,8 +153,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
internal Enumerator(HttpResponseHeaders collection)
{
_collection = collection;
_bits = collection._bits;
_next = 0;
_currentBits = collection._bits;
_next = _currentBits != 0 ? BitOperations.TrailingZeroCount(_currentBits) : -1;
_current = default;
_currentKnownType = default;
_hasUnknown = collection.MaybeUnknown != null;
@ -174,7 +175,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
public void Reset()
{
_next = 0;
_currentBits = _collection._bits;
_next = _currentBits != 0 ? BitOperations.TrailingZeroCount(_currentBits) : -1;
}
}

Просмотреть файл

@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.Primitives;
@ -50,7 +51,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
public partial struct Enumerator : IEnumerator<KeyValuePair<string, StringValues>>
{
private readonly HttpResponseTrailers _collection;
private readonly long _bits;
private long _currentBits;
private int _next;
private KeyValuePair<string, StringValues> _current;
private KnownHeaderType _currentKnownType;
@ -60,8 +61,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
internal Enumerator(HttpResponseTrailers collection)
{
_collection = collection;
_bits = collection._bits;
_next = 0;
_currentBits = collection._bits;
_next = _currentBits != 0 ? BitOperations.TrailingZeroCount(_currentBits) : -1;
_current = default;
_currentKnownType = default;
_hasUnknown = collection.MaybeUnknown != null;
@ -82,7 +83,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
public void Reset()
{
_next = 0;
_currentBits = _collection._bits;
_next = _currentBits != 0 ? BitOperations.TrailingZeroCount(_currentBits) : -1;
}
}
}

Просмотреть файл

@ -428,8 +428,9 @@ namespace CodeGenerator
public bool EnhancedSetter { get; set; }
public bool PrimaryHeader { get; set; }
public string FlagBit() => $"{"0x" + (1L << Index).ToString("x", CultureInfo.InvariantCulture)}L";
public string TestBit() => $"(_bits & {"0x" + (1L << Index).ToString("x", CultureInfo.InvariantCulture)}L) != 0";
public string TestTempBit() => $"(tempBits & {"0x" + (1L << Index).ToString("x", CultureInfo.InvariantCulture)}L) != 0";
public string TestBitCore(string name) => $"({name} & {"0x" + (1L << Index).ToString("x", CultureInfo.InvariantCulture)}L) != 0";
public string TestBit() => TestBitCore("_bits");
public string TestTempBit() => TestBitCore("tempBits");
public string TestNotTempBit() => $"(tempBits & ~{"0x" + (1L << Index).ToString("x", CultureInfo.InvariantCulture)}L) == 0";
public string TestNotBit() => $"(_bits & {"0x" + (1L << Index).ToString("x", CultureInfo.InvariantCulture)}L) == 0";
public string SetBit() => $"_bits |= {"0x" + (1L << Index).ToString("x", CultureInfo.InvariantCulture)}L";
@ -1321,40 +1322,40 @@ $@" private void Clear(long bitsToClear)
{{
switch (_next)
{{{Each(loop.Headers.Where(header => header.Identifier != "ContentLength"), header => $@"
case {header.Index}:
goto Header{header.Identifier};")}
{(!loop.ClassName.Contains("Trailers") ? $@"case {loop.Headers.Length - 1}:
goto HeaderContentLength;" : "")}
default:
goto ExtraHeaders;
}}
{Each(loop.Headers.Where(header => header.Identifier != "ContentLength"), header => $@"
Header{header.Identifier}: // case {header.Index}
if ({header.TestBit()})
{{
case {header.Index}: // Header: ""{header.Name}""
Debug.Assert({header.TestBitCore("_currentBits")});
_current = new KeyValuePair<string, StringValues>(HeaderNames.{header.Identifier}, _collection._headers._{header.Identifier});
{(loop.ClassName.Contains("Request") ? "" : @$"_currentKnownType = KnownHeaderType.{header.Identifier};
")}_next = {header.Index + 1};
return true;
}}")}
{(!loop.ClassName.Contains("Trailers") ? $@"HeaderContentLength: // case {loop.Headers.Length - 1}
if (_collection._contentLength.HasValue)
{{
_current = new KeyValuePair<string, StringValues>(HeaderNames.ContentLength, HeaderUtilities.FormatNonNegativeInt64(_collection._contentLength.Value));
")}_currentBits ^= {"0x" + (1L << header.Index).ToString("x", CultureInfo.InvariantCulture)}L;
break;")}
{(!loop.ClassName.Contains("Trailers") ? $@"case {loop.Headers.Length - 1}: // Header: ""Content-Length""
Debug.Assert(_currentBits == 0);
_current = new KeyValuePair<string, StringValues>(HeaderNames.ContentLength, HeaderUtilities.FormatNonNegativeInt64(_collection._contentLength.GetValueOrDefault()));
{(loop.ClassName.Contains("Request") ? "" : @"_currentKnownType = KnownHeaderType.ContentLength;
")}_next = {loop.Headers.Length};
return true;
}}" : "")}
ExtraHeaders:
if (!_hasUnknown || !_unknownEnumerator.MoveNext())
{{
_current = default(KeyValuePair<string, StringValues>);
{(loop.ClassName.Contains("Request") ? "" : @"_currentKnownType = default;
")}return false;
}}
_current = _unknownEnumerator.Current;
{(loop.ClassName.Contains("Request") ? "" : @"_currentKnownType = KnownHeaderType.Unknown;
")}return true;
")}_next = -1;
return true;" : "")}
default:
if (!_hasUnknown || !_unknownEnumerator.MoveNext())
{{
_current = default(KeyValuePair<string, StringValues>);
{(loop.ClassName.Contains("Request") ? "" : @"_currentKnownType = default;
")}return false;
}}
_current = _unknownEnumerator.Current;
{(loop.ClassName.Contains("Request") ? "" : @"_currentKnownType = KnownHeaderType.Unknown;
")}return true;
}}
if (_currentBits != 0)
{{
_next = BitOperations.TrailingZeroCount(_currentBits);
return true;
}}
else
{{
{(!loop.ClassName.Contains("Trailers") ? $@"_next = _collection._contentLength.HasValue ? {loop.Headers.Length - 1} : -1;" : "_next = -1;")}
return true;
}}
}}
}}
}}