perf(cloudfront): stop copying strings for every distribution in the host lookup (#7742)

Every request of every service looked its Host up among the distributions
once one existed. For each distribution the lookup built the local delivery
host names and cut a substring out of the stored key. 10 distributions
added about 4 KB to each request.

The lookup now compares the id and the suffix in place and walks the raw
keys of the same account.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-08 05:04:44 -04:00
1 parent 30db535f2e
commit 9822ad5f05
2 files changed
+31 -3

No files matched your search

@@ -578,7 +578,9 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
return null;
}
String hostname = stripPort(host);
List<Distribution> distributions = distStore.scan(k -> true);
// Every request gets here once a distribution exists, so this lookup avoids copying strings.
String rawPrefix = AccountAwareStorageBackend.rawKey(distStore.accountId(), "");
List<Distribution> distributions = distStore.scanRaw(rawKey -> rawKey.startsWith(rawPrefix));
for (Distribution dist : distributions) {
if (hostname.equalsIgnoreCase(dist.getDomainName())) {
return dist;
@@ -640,8 +642,11 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
/** True when {@code hostname} is the local delivery host of the distribution with this id. */
private boolean matchesLocalDeliveryHost(String hostname, String id) {
int idLength = id.length();
for (String suffix : localDeliverySuffixes) {
if (hostname.equalsIgnoreCase(id + suffix)) {
if (hostname.length() == idLength + suffix.length()
&& hostname.regionMatches(true, 0, id, 0, idLength)
&& hostname.regionMatches(true, idLength, suffix, 0, suffix.length())) {
return true;
}
}
@@ -29,6 +29,7 @@ import java.security.KeyPairGenerator;
import java.security.spec.ECGenParameterSpec;
import java.time.Instant;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
@@ -125,6 +126,8 @@ class CloudFrontServiceTest {
+ "<Items><Location>US</Location><Location>CA</Location></Items>"
+ "</GeoRestriction></Restrictions>";
private final Map<String, AccountAwareStorageBackend<?>> stores = new HashMap<>();
private CloudFrontService serviceWithDomainSuffix(String domainSuffix) {
return serviceWithDomainSuffix(domainSuffix, "us-east-1");
}
@@ -135,9 +138,11 @@ class CloudFrontServiceTest {
}
private CloudFrontService serviceWithDomainSuffix(String domainSuffix, String region, String baseUrl) {
stores.clear();
StorageFactory storageFactory = Mockito.mock(StorageFactory.class);
when(storageFactory.create(Mockito.anyString(), Mockito.anyString(), Mockito.any()))
.thenAnswer(invocation -> AccountAwareStorageBackend.inMemory("000000000000"));
.thenAnswer(invocation -> stores.computeIfAbsent(invocation.getArgument(1, String.class),
ignored -> AccountAwareStorageBackend.inMemory("000000000000")));
EmulatorConfig config = Mockito.mock(EmulatorConfig.class);
EmulatorConfig.DnsConfig dnsConfig = Mockito.mock(EmulatorConfig.DnsConfig.class);
@@ -473,6 +478,24 @@ class CloudFrontServiceTest {
assertNull(service.findByHost("a." + id + ".cloudfront.localhost.floci.io"));
assertNull(service.findByHost("EABCDEFGHIJKLM.cloudfront.localhost.floci.io"));
assertNull(service.findByHost(".cloudfront.localhost"));
// The id and the suffix are compared apart, ignoring case.
assertEquals(id, service.findByHost(id + ".CLOUDFRONT.LOCALHOST").getId());
assertNull(service.findByHost(id + ".cloudfront.localhosx"));
assertNull(service.findByHost("X" + id.substring(1) + ".cloudfront.localhost"));
}
@Test
@SuppressWarnings("unchecked")
void findByHostIgnoresADistributionOfAnotherAccount() {
CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
Distribution dist = service.createDistribution(distribution(true, List.of()), Map.of());
AccountAwareStorageBackend<Distribution> distributions =
(AccountAwareStorageBackend<Distribution>) stores.get("cloudfront-distributions.json");
distributions.deleteForAccount("000000000000", dist.getId());
distributions.putForAccount("111122223333", dist.getId(), dist);
assertNull(service.findByHost(dist.getDomainName()));
}
@Test