Skip to content

Commit 7dfea16

Browse files
authored
Use a compiled pattern instead of parsing it every time (#6107)
* Use a compiled pattern instead of parsing it every time * Add changelog
1 parent f74b2ee commit 7dfea16

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"contributor": "",
4+
"type": "feature",
5+
"description": "Small optimization for endpoint rules. Lazily compile the region pattern instead of parsing it every time. This will pay the penalty of parsing it just once at the cost of using a bit more of memory to keep the parsed pattern."
6+
}

codegen/src/main/resources/software/amazon/awssdk/codegen/rules/Partition.java.resource

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java.util.HashMap;
22
import java.util.Map;
3+
import java.util.regex.Pattern;
34
import software.amazon.awssdk.annotations.SdkInternalApi;
45
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
56
import software.amazon.awssdk.utils.ToString;
@@ -15,6 +16,7 @@ public class Partition {
1516
private final String regionRegex;
1617
private final Map<String, RegionOverride> regions;
1718
private final Outputs outputs;
19+
private Pattern regionPattern;
1820

1921
private Partition(Builder builder) {
2022
this.id = builder.id;
@@ -31,6 +33,13 @@ public class Partition {
3133
return regionRegex;
3234
}
3335

36+
public boolean regionMatches(String region) {
37+
if (regionPattern == null) {
38+
regionPattern = Pattern.compile(regionRegex);
39+
}
40+
return regionPattern.matcher(region).matches();
41+
}
42+
3443
public Map<String, RegionOverride> regions() {
3544
return regions;
3645
}

codegen/src/main/resources/software/amazon/awssdk/codegen/rules2/RulesFunctions.java.resource

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ public class RulesFunctions {
9090
if (matchedPartition == null) {
9191
// try matching on region name pattern
9292
for (Partition p : data.partitions) {
93-
Pattern regex = Pattern.compile(p.regionRegex());
94-
if (regex.matcher(regionName).matches()) {
93+
if (p.regionMatches(regionName)) {
9594
matchedPartition = p;
9695
break;
9796
}

0 commit comments

Comments
 (0)