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

Optimize UrlEncode in Utils #3307

Draft
wants to merge 11 commits into
base: v4-development
Choose a base branch
from

Conversation

danielmarbach
Copy link
Contributor

@danielmarbach danielmarbach commented Apr 28, 2024

TBD

Description

TBD

Motivation and Context


BenchmarkDotNet v0.13.12, macOS Sonoma 14.4.1 (23E224) [Darwin 23.4.0]
Apple M3 Max, 1 CPU, 14 logical and 14 physical cores
.NET SDK 8.0.202
  [Host]     : .NET 8.0.3 (8.0.324.11423), Arm64 RyuJIT AdvSIMD
  DefaultJob : .NET 8.0.3 (8.0.324.11423), Arm64 RyuJIT AdvSIMD


image

https://github.com/danielmarbach/aws-sdk-net-benchmarks/blob/main/UrlEncode.cs

Testing

Added tests to verify the code still does the same as before.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed

License

  • I confirm that this pull request can be released under the Apache 2 license

@@ -23,6 +23,8 @@
<NoWarn>$(NoWarn);CS1591;CA1822</NoWarn>

<SignAssembly>True</SignAssembly>

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like discussed before this is required for some of the shim APIs.

/// Encoding extensions used to provide a compatibility shim for encoding methods that are missing in .NET Standard
/// 2.0.
/// </summary>
internal static class EncodingExtensions
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know if you prefer another place to put it

{
/// <summary>Indicates to the compiler that the .locals init flag should not be set in nested method headers when emitting to metadata.</summary>
[AttributeUsage(AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Interface, Inherited = false)]
internal sealed class SkipLocalsInitAttribute : Attribute
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know if you prefer another place to put it

@@ -75,12 +77,6 @@ public static partial class AWSSDKUtils
// Default value of progress update interval for streaming is 100KB.
public const long DefaultProgressUpdateInterval = 102400;

internal static Dictionary<int, string> RFCEncodingSchemes = new Dictionary<int, string>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did check the entire SDK and couldn't find another reference so I got rid of it. Is it possible to entirely get rid of the different RFC implementations? If that is possible we could potentially even get rid of the custom code and replace it with the System.Net implementation?

https://learn.microsoft.com/en-us/dotnet/api/system.net.webutility.urlencode?view=net-8.0

@@ -6,7 +6,7 @@ This project file should not be used as part of a release pipeline.
-->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net8.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net8.0</TargetFrameworks>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Netstandard is not a valid target for unit tests


[Theory]
[InlineData("value, with special chars!", "value%2C%20with%20special%20chars!")]
[InlineData("value, with special chars and path {/+:}", "value%2C%20with%20special%20chars%20and%20path%20%7B/%2B:%7D")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I add more data from the valid path chars?

if (!TryGetRFCEncodingSchemes(rfcNumber, out var validUrlCharacters))
validUrlCharacters = ValidUrlCharacters;

var unreservedChars = string.Concat(validUrlCharacters, path ? ValidPathCharacters : "");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given the encoding schemes are only used here the string concat could also go by already have the concatenated string available as costs and returning them by TryGetRFCEncodingSchemes

@dscpinheiro dscpinheiro added the v4 label Apr 29, 2024
@danielmarbach
Copy link
Contributor Author

@normj @dscpinheiro I know this is still draft but would you mind having a look at it at some point? I have added a bunch of inline comments that might influence this PR.

@danielmarbach
Copy link
Contributor Author

Ping @normj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants