Skip to content

Commit 2aba476

Browse files
authored
Switched from LRU cache algorithm to clock cache algorithm (#8253)
1 parent ed5ac28 commit 2aba476

File tree

13 files changed

+273
-350
lines changed

13 files changed

+273
-350
lines changed

src/HotChocolate/Core/src/Authorization/AuthorizationCache.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,19 @@
33

44
namespace HotChocolate.Authorization;
55

6-
internal sealed class AuthorizationCache
6+
internal sealed class AuthorizationCache(int capacity = 256)
77
{
8-
private readonly Cache<AuthorizeDirective[]> _cache;
9-
10-
public AuthorizationCache(int capacity = 100)
11-
{
12-
_cache = new Cache<AuthorizeDirective[]>(capacity);
13-
}
8+
private readonly Cache<AuthorizeDirective[]> _cache = new(capacity);
149

1510
public int Capacity => _cache.Capacity;
1611

17-
public int Count => _cache.Usage;
12+
public int Count => _cache.Count;
1813

19-
public bool TryGetDirectives(
20-
string documentId,
21-
[NotNullWhen(true)] out AuthorizeDirective[]? directives)
14+
public bool TryGetDirectives(string documentId, [NotNullWhen(true)] out AuthorizeDirective[]? directives)
2215
=> _cache.TryGet(documentId, out directives);
2316

2417
public void TryAddDirectives(string documentId, AuthorizeDirective[] directives)
25-
=> _cache.GetOrCreate(documentId, () => directives);
18+
=> _cache.GetOrCreate(documentId, static (_, d) => d, directives);
2619

2720
public void Clear()
2821
=> _cache.Clear();

src/HotChocolate/Core/src/Execution/Caching/DefaultDocumentCache.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
namespace HotChocolate.Execution.Caching;
66

7-
internal sealed class DefaultDocumentCache(int capacity = 100) : IDocumentCache
7+
internal sealed class DefaultDocumentCache(int capacity = 256) : IDocumentCache
88
{
99
private readonly Cache<CachedDocument> _cache = new(capacity);
1010

1111
public int Capacity => _cache.Capacity;
1212

13-
public int Count => _cache.Usage;
13+
public int Count => _cache.Count;
1414

1515
public void TryAddDocument(string documentId, CachedDocument document)
16-
=> _cache.GetOrCreate(documentId, () => document);
16+
=> _cache.GetOrCreate(documentId, static (_, d) => d, document);
1717

1818
public bool TryGetDocument(string documentId, [NotNullWhen(true)] out CachedDocument? document)
19-
=> _cache.TryGet(documentId, out document!);
19+
=> _cache.TryGet(documentId, out document);
2020

2121
public void Clear() => _cache.Clear();
2222
}

src/HotChocolate/Core/src/Execution/Caching/DefaultPreparedOperationCache.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@
44

55
namespace HotChocolate.Execution.Caching;
66

7-
internal sealed class DefaultPreparedOperationCache(int capacity = 100) : IPreparedOperationCache
7+
internal sealed class DefaultPreparedOperationCache(int capacity = 256) : IPreparedOperationCache
88
{
99
private readonly Cache<IOperation> _cache = new(capacity);
1010

1111
public int Capacity => _cache.Capacity;
1212

13-
public int Count => _cache.Usage;
13+
public int Count => _cache.Count;
1414

15-
public void TryAddOperation(
16-
string operationId,
17-
IOperation operation) =>
18-
_cache.GetOrCreate(operationId, () => operation);
15+
public void TryAddOperation(string operationId, IOperation operation)
16+
=> _cache.GetOrCreate(operationId, static (_, op) => op, operation);
1917

20-
public bool TryGetOperation(
21-
string operationId,
22-
[NotNullWhen(true)] out IOperation? operation) =>
23-
_cache.TryGet(operationId, out operation!);
18+
public bool TryGetOperation(string operationId, [NotNullWhen(true)] out IOperation? operation)
19+
=> _cache.TryGet(operationId, out operation);
2420

2521
public void Clear() => _cache.Clear();
2622
}

src/HotChocolate/Core/src/Execution/Caching/IPreparedOperationCache.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ namespace HotChocolate.Execution.Caching;
99
public interface IPreparedOperationCache
1010
{
1111
/// <summary>
12-
/// Gets maximum amount of compiled operations that can be cached. The default
12+
/// Gets the maximum number of compiled operations that can be cached. The default
1313
/// value is <c>100</c>. The minimum allowed value is <c>10</c>.
1414
/// </summary>
1515
int Capacity { get; }
1616

1717
/// <summary>
18-
/// Gets the amount of compiled queries residing in the cache.
18+
/// Gets the maximum number of compiled queries residing in the cache.
1919
/// </summary>
2020
int Count { get; }
2121

2222
/// <summary>
23-
/// Try get a compiled operation by it <paramref name="operationId" />.
23+
/// Try to get a compiled operation by it <paramref name="operationId" />.
2424
/// </summary>
2525
/// <param name="operationId">
2626
/// The internal operation id.
@@ -33,9 +33,7 @@ public interface IPreparedOperationCache
3333
/// <c>true</c> if an operation was found that matches the specified
3434
/// <paramref name="operationId"/>, otherwise <c>false</c>.
3535
/// </returns>
36-
bool TryGetOperation(
37-
string operationId,
38-
[NotNullWhen(true)] out IOperation? operation);
36+
bool TryGetOperation(string operationId, [NotNullWhen(true)] out IOperation? operation);
3937

4038
/// <summary>
4139
/// Tries to add a new compiled operation to the cache.
@@ -46,9 +44,7 @@ bool TryGetOperation(
4644
/// <param name="operation">
4745
/// The operation that shall be cached.
4846
/// </param>
49-
void TryAddOperation(
50-
string operationId,
51-
IOperation operation);
47+
void TryAddOperation(string operationId, IOperation operation);
5248

5349
/// <summary>
5450
/// Clears all items from the cache.

src/HotChocolate/Core/src/Execution/Caching/PreparedOperationCacheOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ namespace HotChocolate.Execution.Caching;
22

33
internal sealed class PreparedOperationCacheOptions
44
{
5-
public int Capacity { get; set; } = 100;
5+
public int Capacity { get; set; } = 256;
66
}

src/HotChocolate/Core/src/Execution/DependencyInjection/InternalServiceCollectionExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ internal static IServiceCollection TryAddRequestExecutorResolver(
162162
internal static IServiceCollection TryAddDefaultCaches(
163163
this IServiceCollection services)
164164
{
165-
services.TryAddSingleton<IDocumentCache>(
166-
_ => new DefaultDocumentCache());
167165
services.TryAddSingleton<PreparedOperationCacheOptions>(
168-
_ => new PreparedOperationCacheOptions { Capacity = 100 });
166+
_ => new PreparedOperationCacheOptions { Capacity = 256 });
167+
services.TryAddSingleton<IDocumentCache>(
168+
sp => new DefaultDocumentCache(
169+
sp.GetRequiredService<PreparedOperationCacheOptions>().Capacity));
169170
return services;
170171
}
171172

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private static IRequestExecutorBuilder CreateBuilder(
172172

173173
public static IServiceCollection AddDocumentCache(
174174
this IServiceCollection services,
175-
int capacity = 100)
175+
int capacity = 256)
176176
{
177177
services.RemoveAll<IDocumentCache>();
178178
services.AddSingleton<IDocumentCache>(
@@ -182,7 +182,7 @@ public static IServiceCollection AddDocumentCache(
182182

183183
public static IServiceCollection AddOperationCache(
184184
this IServiceCollection services,
185-
int capacity = 100)
185+
int capacity = 256)
186186
{
187187
services.RemoveAll<PreparedOperationCacheOptions>();
188188
services.AddSingleton<PreparedOperationCacheOptions>(

src/HotChocolate/CostAnalysis/src/CostAnalysis/Caching/DefaultCostMetricsCache.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@
33

44
namespace HotChocolate.CostAnalysis.Caching;
55

6-
internal sealed class DefaultCostMetricsCache(int capacity = 100) : ICostMetricsCache
6+
internal sealed class DefaultCostMetricsCache(int capacity = 256) : ICostMetricsCache
77
{
88
private readonly Cache<CostMetrics> _cache = new(capacity);
99

1010
public int Capacity => _cache.Capacity;
1111

12-
public int Count => _cache.Usage;
12+
public int Count => _cache.Count;
1313

1414
public bool TryGetCostMetrics(
1515
string operationId,
1616
[NotNullWhen(true)] out CostMetrics? costMetrics)
1717
=> _cache.TryGet(operationId, out costMetrics);
1818

19-
public void TryAddCostMetrics(
20-
string operationId,
21-
CostMetrics costMetrics)
22-
=> _cache.GetOrCreate(operationId, () => costMetrics);
19+
public void TryAddCostMetrics(string operationId, CostMetrics costMetrics)
20+
=> _cache.GetOrCreate(operationId, static (_, m) => m, costMetrics);
2321

2422
public void Clear() => _cache.Clear();
2523
}

src/HotChocolate/CostAnalysis/test/CostAnalysis.Tests/Doubles/FakeCostMetricsCache.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
44

55
namespace HotChocolate.CostAnalysis.Doubles;
66

7-
internal class FakeCostMetricsCache(int capacity = 100) : ICostMetricsCache
7+
internal class FakeCostMetricsCache(int capacity = 256) : ICostMetricsCache
88
{
99
private readonly Cache<CostMetrics> _cache = new(capacity);
1010

1111
public int Capacity => _cache.Capacity;
1212

13-
public int Count => _cache.Usage;
13+
public int Count => _cache.Count;
1414

1515
public int Hits { get; private set; }
1616

1717
public int Misses { get; private set; }
1818

1919
public int Additions { get; private set; }
2020

21-
public bool TryGetCostMetrics(
22-
string operationId,
23-
[NotNullWhen(true)] out CostMetrics? costMetrics)
21+
public bool TryGetCostMetrics(string operationId, [NotNullWhen(true)] out CostMetrics? costMetrics)
2422
{
2523
var result = _cache.TryGet(operationId, out costMetrics);
2624

@@ -36,11 +34,9 @@ public bool TryGetCostMetrics(
3634
return result;
3735
}
3836

39-
public void TryAddCostMetrics(
40-
string operationId,
41-
CostMetrics costMetrics)
37+
public void TryAddCostMetrics(string operationId, CostMetrics costMetrics)
4238
{
43-
_cache.GetOrCreate(operationId, () => costMetrics);
39+
_cache.GetOrCreate(operationId, static (_, m) => m, costMetrics);
4440
Additions++;
4541
}
4642

0 commit comments

Comments
 (0)