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

string.Split to Span.Split #46098

Merged
merged 2 commits into from
Jan 21, 2023
Merged

string.Split to Span.Split #46098

merged 2 commits into from
Jan 21, 2023

Conversation

BrennanConroy
Copy link
Member

New ReadOnlySpan<char>.Split methods introduced in dotnet/runtime#76186 can avoid the string+array allocations from string.Split in some cases. Went through our code base and found where we can use the new methods.

@@ -62,16 +63,35 @@ public static async Task RetrieveClaimsAsync(LdapSettings settings, ClaimsIdenti
{
// Example distinguished name: CN=TestGroup,DC=KERB,DC=local
var groupDN = $"{Encoding.UTF8.GetString((byte[])group)}";
var groupCN = groupDN.Split(',')[0].Substring("CN=".Length);

if (!TryGetGroupCN(groupDN, out var groupCN))
Copy link
Member Author

Choose a reason for hiding this comment

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

There aren't any unit tests for this area. So this change could be reverted if we aren't confident.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

This will conflict with https://github.com/dotnet/aspnetcore/pull/45649/files. Revert changes in this file.

@@ -3,9 +3,9 @@

namespace Microsoft.AspNetCore.Rewrite.ApacheModRewrite;

internal sealed class FlagParser
internal static class FlagParser
Copy link
Member Author

Choose a reason for hiding this comment

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

This was just silly, both the class and the _ruleFlagLookup dictionary were allocated every time this type was used.

Comment on lines +923 to +936
// non-allocating version of mergedCodes.Split(';').Length
var count = 1;
var index = 0;
while (index < mergedCodes.Length)
{
var semiColonIndex = mergedCodes.IndexOf(';', index);
if (semiColonIndex < 0)
{
break;
}
count++;
index = semiColonIndex + 1;
}
return count;
Copy link
Member

Choose a reason for hiding this comment

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

This one makes me a little sad. Feels like something missing API?

cc @stephentoub

Copy link
Member

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

finally got around to do it. PR here: dotnet/runtime#80662

@BrennanConroy BrennanConroy marked this pull request as ready for review January 18, 2023 17:47
@@ -154,25 +154,27 @@ private static EdgeKey CreateEdgeKey(string host)
return EdgeKey.WildcardEdgeKey;
}

var hostParts = host.Split(':');
if (hostParts.Length == 1)
Span<Range> hostParts = stackalloc Range[3];
Copy link
Member

Choose a reason for hiding this comment

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

Why Range 3 when we only support lengths 1 and 2?

Copy link
Member Author

Choose a reason for hiding this comment

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

We need to know if there were more than 2 for the error case

@@ -62,16 +63,35 @@ public static async Task RetrieveClaimsAsync(LdapSettings settings, ClaimsIdenti
{
// Example distinguished name: CN=TestGroup,DC=KERB,DC=local
var groupDN = $"{Encoding.UTF8.GetString((byte[])group)}";
var groupCN = groupDN.Split(',')[0].Substring("CN=".Length);

if (!TryGetGroupCN(groupDN, out var groupCN))
Copy link
Member

Choose a reason for hiding this comment

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

This will conflict with https://github.com/dotnet/aspnetcore/pull/45649/files. Revert changes in this file.

@BrennanConroy BrennanConroy merged commit ebbf5eb into main Jan 21, 2023
@BrennanConroy BrennanConroy deleted the brecon/split branch January 21, 2023 00:12
@ghost ghost added this to the 8.0-preview1 milestone Jan 21, 2023
@davidfowl
Copy link
Member

Very nice! Are you planning to tackle routing @BrennanConroy ?

@BrennanConroy
Copy link
Member Author

BrennanConroy commented Jan 23, 2023

Maybe in the future. I don't see it being worth the effort right now. Routing already does its own custom span-like string splitting via PathSegment and it flows everywhere (20ish files including benchmarks and tests).

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.

6 participants