-
Notifications
You must be signed in to change notification settings - Fork 41
Extract net9 alternate-key cache support for ConcurrentLru #739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
9
commits into
main
Choose a base branch
from
copilot/add-alternate-cache-from-concurrentlru
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+302
−22
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
554fe2f
Implement alternate cache API
Copilot e088f6f
Document alternate cache API
Copilot 776481e
Refine alternate cache get-or-add
Copilot fa5b42c
Extract alternate cache interface to top-level namespace
Copilot 51324ab
Use ReadOnlySpan alternate keys in positive tests
Copilot b303547
Rename alternate cache API to alternate lookup
Copilot 75d5105
Cache alternate lookup adapter in ConcurrentLru
Copilot b9ea646
Extract alternate lookup collection helpers
Copilot b3577b4
Remove redundant alternate lookup check
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
BitFaster.Caching.UnitTests/Lru/ConcurrentLruAlternateLookupTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #if NET9_0_OR_GREATER | ||
| using System; | ||
| using BitFaster.Caching.Lru; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace BitFaster.Caching.UnitTests.Lru | ||
| { | ||
| public class ConcurrentLruAlternateLookupTests | ||
| { | ||
| [Fact] | ||
| public void TryGetAlternateLookupReturnsLookupForCompatibleComparer() | ||
| { | ||
| var cache = new ConcurrentLru<string, string>(1, 3, StringComparer.Ordinal); | ||
| cache.GetOrAdd("42", _ => "value"); | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| cache.TryGetAlternateLookup<ReadOnlySpan<char>>(out var alternate).Should().BeTrue(); | ||
| alternate.TryGet(key, out var value).Should().BeTrue(); | ||
| value.Should().Be("value"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetAlternateLookupThrowsForIncompatibleComparer() | ||
| { | ||
| var cache = new ConcurrentLru<string, string>(1, 3, StringComparer.Ordinal); | ||
|
|
||
| Action act = () => cache.GetAlternateLookup<int>(); | ||
|
|
||
| act.Should().Throw<InvalidOperationException>().WithMessage("Incompatible comparer"); | ||
| cache.TryGetAlternateLookup<int>(out var alternate).Should().BeFalse(); | ||
| alternate.Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AlternateLookupTryRemoveReturnsActualKeyAndValue() | ||
| { | ||
| var cache = new ConcurrentLru<string, string>(1, 3, StringComparer.Ordinal); | ||
| cache.GetOrAdd("42", _ => "value"); | ||
| var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>(); | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| alternate.TryRemove(key, out var actualKey, out var value).Should().BeTrue(); | ||
|
|
||
| actualKey.Should().Be("42"); | ||
| value.Should().Be("value"); | ||
| cache.TryGet("42", out _).Should().BeFalse(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AlternateLookupGetOrAddUsesAlternateKeyOnMissAndHit() | ||
| { | ||
| var cache = new ConcurrentLru<string, string>(1, 3, StringComparer.Ordinal); | ||
| var alternate = cache.GetAlternateLookup<ReadOnlySpan<char>>(); | ||
| var factoryCalls = 0; | ||
| ReadOnlySpan<char> key = "42"; | ||
|
|
||
| alternate.GetOrAdd(key, key => | ||
| { | ||
| factoryCalls++; | ||
| return $"value-{key.ToString()}"; | ||
| }).Should().Be("value-42"); | ||
|
|
||
| alternate.GetOrAdd(key, (_, prefix) => | ||
| { | ||
| factoryCalls++; | ||
| return prefix; | ||
| }, "unused").Should().Be("value-42"); | ||
|
|
||
| factoryCalls.Should().Be(1); | ||
| cache.TryGet("42", out var value).Should().BeTrue(); | ||
| value.Should().Be("value-42"); | ||
| } | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace BitFaster.Caching | ||
| { | ||
| internal static class CollectionExtensions | ||
| { | ||
| #if NET9_0_OR_GREATER | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| internal static bool IsCompatibleKey<TAlternateKey, TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary) | ||
| where TAlternateKey : notnull, allows ref struct | ||
| where TKey : notnull | ||
| { | ||
| return dictionary.Comparer is IAlternateEqualityComparer<TAlternateKey, TKey>; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| internal static IAlternateEqualityComparer<TAlternateKey, TKey> GetAlternateComparer<TAlternateKey, TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary) | ||
| where TAlternateKey : notnull, allows ref struct | ||
| where TKey : notnull | ||
| { | ||
| Debug.Assert(dictionary.IsCompatibleKey<TAlternateKey, TKey, TValue>()); | ||
| return Unsafe.As<IAlternateEqualityComparer<TAlternateKey, TKey>>(dictionary.Comparer!); | ||
| } | ||
| #endif | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #if NET9_0_OR_GREATER | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace BitFaster.Caching | ||
| { | ||
| /// <summary> | ||
| /// Provides an alternate-key lookup over a cache. | ||
| /// </summary> | ||
| /// <typeparam name="TAlternateKey">The alternate key type.</typeparam> | ||
| /// <typeparam name="TKey">The cache key type.</typeparam> | ||
| /// <typeparam name="TValue">The cache value type.</typeparam> | ||
| public interface IAlternateLookup<TAlternateKey, TKey, TValue> | ||
| where TAlternateKey : notnull, allows ref struct | ||
| where TKey : notnull | ||
| { | ||
| /// <summary> | ||
| /// Attempts to get a value using an alternate key. | ||
| /// </summary> | ||
| /// <param name="key">The alternate key.</param> | ||
| /// <param name="value">The cached value when found.</param> | ||
| /// <returns><see langword="true" /> when the key is found; otherwise, <see langword="false" />.</returns> | ||
| bool TryGet(TAlternateKey key, [MaybeNullWhen(false)] out TValue value); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to remove a value using an alternate key. | ||
| /// </summary> | ||
| /// <param name="key">The alternate key.</param> | ||
| /// <param name="actualKey">The removed cache key.</param> | ||
| /// <param name="value">The removed value.</param> | ||
| /// <returns><see langword="true" /> when the key is found; otherwise, <see langword="false" />.</returns> | ||
| bool TryRemove(TAlternateKey key, [MaybeNullWhen(false)] out TKey actualKey, [MaybeNullWhen(false)] out TValue value); | ||
|
|
||
| /// <summary> | ||
| /// Gets an existing value or adds a new value using an alternate key. | ||
| /// </summary> | ||
| /// <param name="key">The alternate key.</param> | ||
| /// <param name="valueFactory">The value factory.</param> | ||
| /// <returns>The cached value.</returns> | ||
| TValue GetOrAdd(TAlternateKey key, Func<TAlternateKey, TValue> valueFactory); | ||
|
|
||
| /// <summary> | ||
| /// Gets an existing value or adds a new value using an alternate key and factory argument. | ||
| /// </summary> | ||
| /// <typeparam name="TArg">The factory argument type.</typeparam> | ||
| /// <param name="key">The alternate key.</param> | ||
| /// <param name="valueFactory">The value factory.</param> | ||
| /// <param name="factoryArgument">The factory argument.</param> | ||
| /// <returns>The cached value.</returns> | ||
| TValue GetOrAdd<TArg>(TAlternateKey key, Func<TAlternateKey, TArg, TValue> valueFactory, TArg factoryArgument); | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetOrAddimplies there isIAsyncAlternativeLookupwithGetOrAddAsync.It may be simpler to only have
TryGetandTryRemove, thus the async variants are not needed.ConcurrentDictionarysupports whole API surface including indexer, so precedent was set to be complete.