Skip to content

Add IScopedAlternateLookup for scoped caches#771

Draft
Copilot wants to merge 10 commits intomainfrom
copilot/add-iscopedalternatelookup
Draft

Add IScopedAlternateLookup for scoped caches#771
Copilot wants to merge 10 commits intomainfrom
copilot/add-iscopedalternatelookup

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 15, 2026

Summary

  • add a new net9-only IScopedAlternateLookup API and expose it from IScopedCache
  • implement scoped alternate lookup wrappers for ScopedCache and AtomicFactoryScopedCache
  • add scoped alternate lookup unit coverage and default interface method tests

Validation

  • dotnet format BitFaster.Caching/BitFaster.Caching.csproj
  • dotnet build BitFaster.Caching/BitFaster.Caching.csproj -f net9.0
  • dotnet test BitFaster.Caching.UnitTests/BitFaster.Caching.UnitTests.csproj -f net9.0
  • parallel_validation

Copilot AI and others added 2 commits April 15, 2026 07:07
Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/dccf8c03-48f8-4e76-bf28-f77f9a51d303

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/dccf8c03-48f8-4e76-bf28-f77f9a51d303

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Comment thread BitFaster.Caching/ScopedCache.cs
@coveralls
Copy link
Copy Markdown

coveralls commented Apr 15, 2026

Coverage Status

coverage: 98.969% (-0.1%) from 99.11% — copilot/add-iscopedalternatelookup into main

Copilot AI requested a review from bitfaster April 15, 2026 17:46
}
#pragma warning restore CA2000 // Dispose objects before losing scope

public Lifetime<V> ScopedGetOrAdd(TAlternateKey key, Func<K, Scoped<V>> valueFactory)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add a soak test to verify this works as expected with concurrent callers. Soak test will cover all branches in the private ScopedGetOrAdd method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a concurrent soak test in ScopedCacheSoakTests that races alternate lookup ScopedGetOrAdd against TryRemove and asserts the returned lifetime stays alive under contention, covering the retry path in the private helper. Addressed in a3558be.

Copilot AI and others added 2 commits April 15, 2026 19:03
Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/72ba7d13-f135-44e5-ab2e-e3421636eee4

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/72ba7d13-f135-44e5-ab2e-e3421636eee4

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Copilot AI requested a review from bitfaster April 15, 2026 19:08
[Fact]
public async Task ScopedGetOrAdd_ConcurrentWithRemove_ReturnedLifetimeIsAlive()
{
var scopedCache = new ScopedCache<string, Disposable>(new ConcurrentLru<string, Scoped<Disposable>>(1, 1, StringComparer.Ordinal));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot initialize the cache with the same size parameter as the other tests, 1 is an invalid size. Do not remove values as part of the test. Instead, make the test match WhenSoakScopedGetOrAddValueIsAlwaysAlive but use the alternate lookup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the soak test to use the standard cache capacity and to mirror the existing scoped get-or-add soak pattern via alternate lookup, without removing values. Addressed in 99d95c4.

Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/53059f04-e895-4186-a464-9cd3159d01ec

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Copilot AI requested a review from bitfaster April 16, 2026 06:44
{
for (int j = 0; j < 100000; j++)
{
string key = j.ToString();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot declare a char[] buffer variable and format j into the buffer to avoid allocating a string in the loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the soak test to reuse one char[] buffer per worker thread via Threaded.Run(threadCount, r => ...), so the key formatting no longer allocates in the hot loop. Addressed in 32705c1.

Copilot AI and others added 3 commits April 16, 2026 06:53
Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/7b7e1bc4-582b-4e81-b6a3-d21883ba1a98

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/7b7e1bc4-582b-4e81-b6a3-d21883ba1a98

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/7b7e1bc4-582b-4e81-b6a3-d21883ba1a98

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Copilot AI requested a review from bitfaster April 16, 2026 06:59
{
await Threaded.Run(threadCount, r =>
{
var key = keys[r];
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot declare the key on this line and delete the array of keys. This will avoid capturing the keys array in the lambda.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated it to declare the per-thread char[] buffer inside the worker lambda and removed the outer keys array, so the lambda no longer captures that array. Addressed in 330d9bf.

Agent-Logs-Url: https://github.com/bitfaster/BitFaster.Caching/sessions/430078a5-1591-4951-9db4-852f58455984

Co-authored-by: bitfaster <12851828+bitfaster@users.noreply.github.com>
Copilot AI requested a review from bitfaster April 16, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants