Skip to content

Commit 0d20eac

Browse files
committed
Switch to using record struct for better cache locality
1 parent 6f7c9a7 commit 0d20eac

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

src/Kubernetes.Controller/Certificates/ImmutableCertificateCache.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.Globalization;
8-
using System.Linq;
7+
98
#nullable enable
109
namespace Yarp.Kubernetes.Controller.Certificates;
1110

@@ -56,26 +55,17 @@ public ImmutableCertificateCache(IEnumerable<TCert> certificates, Func<TCert, IE
5655

5756
protected IReadOnlyDictionary<string, TCert> Certificates => _certificates;
5857

59-
protected record WildCardDomain(string Domain, TCert? Certificate);
58+
protected record struct WildCardDomain(string Domain, TCert? Certificate);
6059

6160
private bool TryGetCertificateExact(string domain, [NotNullWhen(true)] out TCert? certificate) =>
6261
_certificates.TryGetValue(domain, out certificate);
6362

6463
private bool TryGetWildcardCertificate(string domain, [NotNullWhen(true)] out TCert? certificate)
6564
{
66-
if (_wildCardDomains.BinarySearch(new WildCardDomain(domain, null!), DomainNameComparer.Instance) is { } index)
65+
if (_wildCardDomains.BinarySearch(new WildCardDomain(domain, null!), DomainNameComparer.Instance) is { } index and > -1)
6766
{
68-
if (index > -1)
69-
{
70-
certificate = _wildCardDomains[index].Certificate!;
71-
return true;
72-
}
73-
// var candidate = _wildCardDomains[~index];
74-
// if (domain.EndsWith(candidate.Domain, true, CultureInfo.InvariantCulture))
75-
// {
76-
// certificate = candidate.Certificate!;
77-
// return true;
78-
// }
67+
certificate = _wildCardDomains[index].Certificate!;
68+
return true;
7969
}
8070

8171
certificate = null;
@@ -91,23 +81,20 @@ private class DomainNameComparer : IComparer<WildCardDomain>
9181
{
9282
public static readonly DomainNameComparer Instance = new();
9383

94-
public int Compare(WildCardDomain? x, WildCardDomain? y)
84+
public int Compare(WildCardDomain x, WildCardDomain y)
9585
{
96-
var ret = Compare(x!.Domain.AsSpan(), y!.Domain.AsSpan());
86+
var ret = Compare(x.Domain.AsSpan(), y.Domain.AsSpan());
9787
if (ret != 0)
9888
{
9989
return ret;
10090
}
10191

102-
switch (x!.Certificate, y!.Certificate)
92+
return (x.Certificate, y.Certificate) switch
10393
{
104-
case (null, {}) when x.Domain.Length > y.Domain.Length:
105-
return 0;
106-
case ({}, null) when x.Domain.Length < y.Domain.Length:
107-
return 0;
108-
default:
109-
return x.Domain.Length - y.Domain.Length;
110-
}
94+
(null, not null) when x.Domain.Length > y.Domain.Length => 0,
95+
(not null, null) when x.Domain.Length < y.Domain.Length => 0,
96+
_ => x.Domain.Length - y.Domain.Length
97+
};
11198
}
11299

113100
private static int Compare(ReadOnlySpan<char> x, ReadOnlySpan<char> y)

0 commit comments

Comments
 (0)