From 9a88ebf83049d2e06ac1fba109c6af41a19020b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Sat, 7 Nov 2026 22:08:28 +0900 Subject: [PATCH] perf(apigateway): skip the key copy for a host that is no custom domain (#7727) Every request of every service looked its Host up among the custom domains. The lookup copied all stored domain keys and cut a substring per key, and almost no host is a custom domain. 100 domains added about 19 KB to each request. A walk over the stored keys now answers a miss first. It copies nothing. A hit still takes the old path, so the owner, region and conflict rules stay the same. --- .../apigateway/ApiGatewayService.java | 7 +++++- ...piGatewayCustomDomainOwnerServiceTest.java | 24 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java index 07cb82aa4e..ea24354e64 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayService.java @@ -3284,8 +3284,13 @@ public class ApiGatewayService implements ResourceProvider { * @return the domain's routing scope if found, or null if no domain matches */ public CustomDomainRoute findDomainByName(String domainName) { + String keySuffix = "::" + domainName; + // Every request of every service gets here, and scanAllAccountEntries copies every key. + if (domainStore.scanRaw(rawKey -> rawKey.endsWith(keySuffix)).isEmpty()) { + return null; + } List> matches = - domainStore.scanAllAccountEntries(key -> key.endsWith("::" + domainName)); + domainStore.scanAllAccountEntries(key -> key.endsWith(keySuffix)); if (matches.size() > 1) { throw new AwsException("ConflictException", "Custom domain '" + domainName + "' is ambiguous across accounts or regions", 409); diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayCustomDomainOwnerServiceTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayCustomDomainOwnerServiceTest.java index f675587181..b437227b0b 100644 --- a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayCustomDomainOwnerServiceTest.java +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayCustomDomainOwnerServiceTest.java @@ -22,6 +22,9 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class ApiGatewayCustomDomainOwnerServiceTest { @@ -41,7 +44,7 @@ class ApiGatewayCustomDomainOwnerServiceTest { StorageFactory storageFactory = mock(StorageFactory.class); when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(invocation -> stores.computeIfAbsent(invocation.getArgument(1, String.class), - ignored -> AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT))); + ignored -> spy(AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT)))); service = new ApiGatewayService(storageFactory, mock(EmulatorConfig.class, RETURNS_DEEP_STUBS), mock(TlsCertificateManager.class), new RegionResolver(REGION, DEFAULT_ACCOUNT)); @@ -112,13 +115,24 @@ class ApiGatewayCustomDomainOwnerServiceTest { assertNull(service.findDomainByRegionalHostname(DOMAIN)); } - @SuppressWarnings("unchecked") + @Test + void anUnknownHostDoesNotCopyTheStoredKeys() { + putDomain(OWNER, REGION); + + assertNull(service.findDomainByName("other.example.test")); + + verify(domains(), never()).scanAllAccountEntries(any()); + } + private void putDomain(String account, String region) { - AccountAwareStorageBackend domains = - (AccountAwareStorageBackend) stores.get("apigateway-domains.json"); CustomDomain domain = new CustomDomain(); domain.setDomainName(DOMAIN); - domains.putForAccount(account, region + "::" + DOMAIN, domain); + domains().putForAccount(account, region + "::" + DOMAIN, domain); + } + + @SuppressWarnings("unchecked") + private AccountAwareStorageBackend domains() { + return (AccountAwareStorageBackend) stores.get("apigateway-domains.json"); } @SuppressWarnings("unchecked")