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;
}
}