Skip to content

Cache compiled Regex instances in Partition and RegionEndpoint for en…#4396

Merged
muhammad-othman merged 1 commit intodevelopmentfrom
muhamoth/DOTNET-8584-perf-optimize-regex
Apr 23, 2026
Merged

Cache compiled Regex instances in Partition and RegionEndpoint for en…#4396
muhammad-othman merged 1 commit intodevelopmentfrom
muhamoth/DOTNET-8584-perf-optimize-regex

Conversation

@muhammad-othman
Copy link
Copy Markdown
Member

@muhammad-othman muhammad-othman commented Apr 22, 2026

Description

  • Changed _partitionsByRegex dictionary key from string to Regex. The compiled Regex instances are now the keys themselves — no separate cache needed. This is thread-safe because the dictionary is already guarded by ReaderWriterLockSlim.

  • Updated PartitionsTemplate.tt to generate new 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().Success to .IsMatch() to avoid unnecessary Match object allocation.

Motivation and Context

DOTNET-8584

Testing

Created a BenchmarkDotNet tests and got these results for Fn.Partition()

Method Before After
Known region (us-east-1) 241 ns 208 ns
Regex match (us-west-99) 308 ns 297 ns
Regex match (cn-south-1) 537 ns 260 ns
Regex match (us-gov-east-99) 1,282 ns 534 ns
No match (unknown-1) 759 ns 412 ns

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

  • DotNet Dry-run ID: 88936fb2-0de8-4dac-baf1-9d358809e919
    • Pending
    • Completed successfully
    • Failed
  • PowerShell Dry-run ID: d9576ae0-8a36-40cb-ad18-90074dfdef94
    • Pending
    • Completed successfully
    • Failed

Breaking Changes Assessment

  1. Identify all breaking changes including the following details:
    • What functionality was changed?
    • How will this impact customers?
    • Why does this need to be a breaking change and what are the most notable non-breaking alternatives?
    • Are best practices being followed?
    • How have you tested this breaking change?
  2. Has a senior/+ engineer been assigned to review this PR?

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 RegionEndpoint and use IsMatch() to avoid Match allocations.
  • Change Partition’s _partitionsByRegex to key on compiled Regex instances (and update generated partition initialization accordingly).
  • Update the partitions T4 template so regenerated code continues to emit compiled Regex usage; 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.

@dscpinheiro dscpinheiro self-requested a review April 23, 2026 15:04
@muhammad-othman muhammad-othman force-pushed the muhamoth/DOTNET-8584-perf-optimize-regex branch from 0721c42 to e6eb48e Compare April 23, 2026 15:05
@muhammad-othman muhammad-othman merged commit dae57fb into development Apr 23, 2026
10 checks passed
@muhammad-othman muhammad-othman deleted the muhamoth/DOTNET-8584-perf-optimize-regex branch April 23, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants