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

Some performance improvements in Azure.Core for .Net 6.0 #32481

Merged
merged 7 commits into from
Nov 15, 2022
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
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Microsoft.Build.Traversal": "3.0.23"
},
"sdk": {
"version": "6.0.100",
"version": "6.0.403",
AlexanderSher marked this conversation as resolved.
Show resolved Hide resolved
"rollForward": "feature"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@
<OutputPath>bin\$(Configuration)\</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
</ItemGroup>
<ItemGroup>
<!-- This is needed for discovering tests in test explorer -->
<PackageReference Include="System.Runtime.InteropServices" />
</ItemGroup>

<PropertyGroup>
<AssemblyTitle>Microsoft.Azure.Batch.FileConventions.Integration.Tests</AssemblyTitle>
<Description>Azure Batch File conventions IntegrationTests tests class library</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
<ItemGroup>
<PackageReference Include="FsCheck.Xunit" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
</ItemGroup>
<ItemGroup>
<!-- This is needed for discovering tests in test explorer -->
<PackageReference Include="System.Runtime.InteropServices" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
</ItemGroup>
<ItemGroup>
<!-- This is needed for discovering tests in test explorer -->
<PackageReference Include="System.Runtime.InteropServices" />
</ItemGroup>

<PropertyGroup>
<AssemblyTitle>Microsoft.Azure.Batch.FileStaging.Tests</AssemblyTitle>
<Description>Azure Batch File Staging integration tests</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<PropertyGroup>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
<VersionPrefix>1.0.0</VersionPrefix>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<PropertyGroup>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
</PropertyGroup>
Expand Down
66 changes: 64 additions & 2 deletions sdk/core/Azure.Core/src/Pipeline/HttpClientTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,61 @@ private static HttpRequestMessage BuildRequestMessage(HttpMessage message)

internal static bool TryGetHeader(HttpHeaders headers, HttpContent? content, string name, [NotNullWhen(true)] out string? value)
{
#if NET6_0_OR_GREATER
if (headers.NonValidated.TryGetValues(name, out HeaderStringValues values) ||
content is not null && content.Headers.NonValidated.TryGetValues(name, out values))
{
value = JoinHeaderValues(values);
return true;
}
#else
if (TryGetHeader(headers, content, name, out IEnumerable<string>? values))
{
value = JoinHeaderValues(values);
return true;
}

#endif
value = null;
return false;
}

internal static bool TryGetHeader(HttpHeaders headers, HttpContent? content, string name, [NotNullWhen(true)] out IEnumerable<string>? values)
{
#if NET6_0_OR_GREATER
if (headers.NonValidated.TryGetValues(name, out HeaderStringValues headerStringValues) ||
content != null &&
content.Headers.NonValidated.TryGetValues(name, out headerStringValues))
{
values = headerStringValues;
return true;
}

values = null;
return false;
#else
return headers.TryGetValues(name, out values) ||
content != null &&
content.Headers.TryGetValues(name, out values);
#endif

}

internal static IEnumerable<HttpHeader> GetHeaders(HttpHeaders headers, HttpContent? content)
{
#if NET6_0_OR_GREATER
foreach (var (key, value) in headers.NonValidated)
{
yield return new HttpHeader(key, JoinHeaderValues(value));
}

if (content is not null)
{
foreach (var (key, value) in content.Headers.NonValidated)
{
yield return new HttpHeader(key, JoinHeaderValues(value));
}
}
#else
foreach (KeyValuePair<string, IEnumerable<string>> header in headers)
{
yield return new HttpHeader(header.Key, JoinHeaderValues(header.Value));
Expand All @@ -238,28 +274,43 @@ internal static IEnumerable<HttpHeader> GetHeaders(HttpHeaders headers, HttpCont
yield return new HttpHeader(header.Key, JoinHeaderValues(header.Value));
}
}
#endif

}

internal static bool RemoveHeader(HttpHeaders headers, HttpContent? content, string name)
{
// .Remove throws on invalid header name so use TryGet here to check
#if NET6_0_OR_GREATER
if (headers.NonValidated.Contains(name) && headers.Remove(name))
{
return true;
}

return content is not null && content.Headers.NonValidated.Contains(name) && content.Headers.Remove(name);
AlexanderSher marked this conversation as resolved.
Show resolved Hide resolved
#else
if (headers.TryGetValues(name, out _) && headers.Remove(name))
{
return true;
}

return content?.Headers.TryGetValues(name, out _) == true && content.Headers.Remove(name);
#endif
}

internal static bool ContainsHeader(HttpHeaders headers, HttpContent? content, string name)
{
// .Contains throws on invalid header name so use TryGet here
#if NET6_0_OR_GREATER
return headers.NonValidated.Contains(name) || content is not null && content.Headers.NonValidated.Contains(name);
#else
if (headers.TryGetValues(name, out _))
{
return true;
}

return content?.Headers.TryGetValues(name, out _) == true;
#endif
}

internal static void CopyHeaders(HttpHeaders from, HttpHeaders to)
Expand All @@ -272,11 +323,22 @@ internal static void CopyHeaders(HttpHeaders from, HttpHeaders to)
}
}
}

#if NET6_0_OR_GREATER
private static string JoinHeaderValues(HeaderStringValues values)
{
return values.Count switch
{
0 => string.Empty,
1 => values.ToString(),
_ => string.Join(",", values)
};
}
#else
private static string JoinHeaderValues(IEnumerable<string> values)
{
return string.Join(",", values);
}
#endif

private sealed class PipelineRequest : Request
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePol
/// <inheritdoc />
public override ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
async ValueTask ProcessAsyncInner(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
OnSendingRequest(message);
await ProcessNextAsync(message, pipeline).ConfigureAwait(false);
OnReceivedResponse(message);
}

if (!_hasOnReceivedResponse)
{
// If OnReceivedResponse was not overridden we can avoid creating a state machine and return the task directly
OnSendingRequest(message);
return ProcessNextAsync(message, pipeline);
}

return ProcessAsyncInner(message, pipeline);
return InnerProcessAsync(message, pipeline);
}

private async ValueTask InnerProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
OnSendingRequest(message);
await ProcessNextAsync(message, pipeline).ConfigureAwait(false);
OnReceivedResponse(message);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/tests/TransportFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public async Task CanGetAndSetResponseHeaders(string headerName, string headerVa

Response response = await ExecuteRequest(request, transport);

Assert.True(response.Headers.Contains(headerName));
Assert.True(response.Headers.Contains(headerName), $"response.Headers contains the following headers: {string.Join(", ", response.Headers.Select(h => $"\"{h.Name}\": \"{h.Value}\""))}");

Assert.True(response.Headers.TryGetValue(headerName, out var value));
Assert.AreEqual(headerValue, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" VersionOverride="2.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" VersionOverride="3.0.14" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" VersionOverride="3.0.14" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="System.Memory" VersionOverride="4.5.4" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Azure.ResourceManager" />
</ItemGroup>
<ItemGroup>
<Folder Include="SessionRecords\DiskPoolTests\" />
Expand Down