Cache compiled Regex instances in Partition and RegionEndpoint for en…#4396
Merged
muhammad-othman merged 1 commit intodevelopmentfrom Apr 23, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Improves endpoint/partition resolution performance in the AWS .NET SDK Core by caching compiled Regex instances used to map regions to partitions and to match partition region patterns.
Changes:
- Cache compiled partition-region regexes in
RegionEndpointand useIsMatch()to avoidMatchallocations. - Change
Partition’s_partitionsByRegexto key on compiledRegexinstances (and update generated partition initialization accordingly). - Update the partitions T4 template so regenerated code continues to emit compiled
Regexusage; add a dev config entry for a Core patch release.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| sdk/src/Core/RegionEndpoint/RegionEndpoint.cs | Replaces pattern set with a pattern→compiled-regex map and uses cached Regex.IsMatch() during unknown-region partition inference. |
| sdk/src/Core/Amazon.Runtime/Internal/Endpoints/StandardLibrary/Partition.cs | Stores compiled Regex instances for partition matching and iterates compiled regexes during partition lookup. |
| sdk/src/Core/Amazon.Runtime/Internal/Endpoints/StandardLibrary/Partition.generated.cs | Updates generated partition initialization to add compiled Regex objects as keys. |
| generator/ServiceClientGeneratorLib/Generators/Endpoints/PartitionsTemplate.tt | Adjusts template to generate compiled Regex keys going forward. |
| generator/ServiceClientGeneratorLib/Generators/Endpoints/PartitionsTemplate.cs | Regenerated T4 output reflecting the template change. |
| generator/.DevConfigs/1b22ae51-b2c9-40ca-bb2b-d16a341993fe.json | Adds Core dev config entry marking the change as a patch. |
…dpoint resolution performance
0721c42 to
e6eb48e
Compare
GarrettBeatty
approved these changes
Apr 23, 2026
dscpinheiro
approved these changes
Apr 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Changed
_partitionsByRegexdictionary key fromstringtoRegex. The compiledRegexinstances are now the keys themselves — no separate cache needed. This is thread-safe because the dictionary is already guarded byReaderWriterLockSlim.Updated
PartitionsTemplate.ttto generatenew Regex(@"pattern", RegexOptions.Compiled)keys so future regeneration produces the correct code.Merged
_allPartitionRegionRegex(HashSet) and added a compiled Regex cache into a single_partitionRegionRegexMap(Dictionary<string, Regex>). Also changed from.Match().Successto.IsMatch()to avoid unnecessaryMatchobject allocation.Motivation and Context
DOTNET-8584Testing
Created a BenchmarkDotNet tests and got these results for Fn.Partition()
RegionEndpoint.GetBySystemName: Shows no benchmark difference because it self-caches resolved regions in
_hashBySystemName— subsequent calls hit the dictionary directly. The regex caching improvement applies only to the first resolution of an unknown region.Dry-runs
Breaking Changes Assessment
Screenshots (if appropriate)
Types of changes
Checklist
License