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 DetermineService #3365

Merged
merged 2 commits into from
Jul 4, 2024
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
39 changes: 19 additions & 20 deletions sdk/src/Core/Amazon.Util/AWSSDKUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,33 +668,32 @@ public static string DetermineRegion(string url)
/// </returns>
public static string DetermineService(string url)
{
int delimIndex = url.IndexOf("//", StringComparison.Ordinal);
if (delimIndex >= 0)
url = url.Substring(delimIndex + 2);
var urlSpan = url.AsSpan();

string[] urlParts = url.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
if (urlParts == null || urlParts.Length == 0)
return string.Empty;
var doubleSlashIndex = urlSpan.IndexOf(DoubleSlash, StringComparison.Ordinal);
if (doubleSlashIndex >= 0)
urlSpan = urlSpan.Slice(doubleSlashIndex + 2);

string servicePart = urlParts[0];
int hyphenated = servicePart.IndexOf('-');
string service;
if (hyphenated < 0)
{ service = servicePart; }
else
{ service = servicePart.Substring(0, hyphenated); }
var dotIndex = urlSpan.IndexOf('.');

// Check for SQS : return "sqs" incase service is determined to be "queue" as per the URL.
if (service.Equals("queue"))
{
return "sqs";
}
else
if (dotIndex < 0)
return string.Empty;

var servicePartSpan = urlSpan.Slice(0, dotIndex);
var hyphenIndex = servicePartSpan.IndexOf('-');
if (hyphenIndex > 0)
{
return service;
servicePartSpan = servicePartSpan.Slice(0, hyphenIndex);
}

// Check for SQS : return "sqs" in case service is determined to be "queue" as per the URL.
return servicePartSpan.Equals(Queue, StringComparison.OrdinalIgnoreCase) ? "sqs" : servicePartSpan.ToString();
}

// Compiler trick to directly refer to static data in the assembly
private static ReadOnlySpan<char> DoubleSlash => new[] { '/', '/' };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stevenaw you might enjoy this ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a neat trick, thanks! I'll have to look out for opportunities to use this

private static ReadOnlySpan<char> Queue => new[] { 'q', 'u', 'e', 'u', 'e' };

/// <summary>
/// Utility method for converting Unix epoch seconds to DateTime structure.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions sdk/test/NetStandard/UnitTests/Core/AWSSDKUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,18 @@ public void CompressSpaces()
var compressed = AWSSDKUtils.CompressSpaces(data);
Assert.Equal("Hello, World!", compressed);
}

[Theory]
[InlineData("https://s3.amazonaws.com", "s3")]
[InlineData("sqs.us-west-2.amazonaws.com", "sqs")]
[InlineData("queue.amazonaws.com", "sqs")]
[InlineData("https://sns.us-west-2.amazonaws.com", "sns")]
[InlineData("https://s3-external-1.amazonaws.com", "s3")]
public void DetermineService(string url, string expectedService)
{
var service = AWSSDKUtils.DetermineService(url);

Assert.Equal(expectedService, service);
}
}
}