Skip to content

M.E.C.M. Add TryGetValue(ReadOnlySpan<char>) API #112695

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

Merged
merged 7 commits into from
Feb 20, 2025

Conversation

mgravell
Copy link
Member

Implement MemoryCache.TryGetValue APIs taking ReadOnlySpan<char>, as approved in #110504

fix #110504

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

1 similar comment
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


#if NET9_0_OR_GREATER
[OverloadResolutionPriority(1)]
public bool TryGetValue(ReadOnlySpan<char> key, out object? result) { throw null; }
Copy link
Preview

Copilot AI Feb 19, 2025

Choose a reason for hiding this comment

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

Add proper XML documentation comments to describe the purpose and parameters of this method.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Documentation is in src - let me know if I'm wrong (humans., I mean)

[OverloadResolutionPriority(1)]
public bool TryGetValue(ReadOnlySpan<char> key, out object? result) { throw null; }
[OverloadResolutionPriority(1)]
public bool TryGetValue<TItem>(ReadOnlySpan<char> key, out TItem? value) { throw null; }
Copy link
Preview

Copilot AI Feb 19, 2025

Choose a reason for hiding this comment

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

Add proper XML documentation comments to describe the purpose and parameters of this method.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Documentation is in src - let me know if I'm wrong (humans., I mean)

- demand GetAlternateLookup works on the target runtimes
- rely on documented "out set to default on failure" lookup behavior
@mgravell
Copy link
Member Author

@stephentoub made less defensive in 4f60149, per feedback

public bool TryGetValue(System.ReadOnlySpan<char> key, out object? value) { throw null; }
[System.Runtime.CompilerServices.OverloadResolutionPriority(1)]
public bool TryGetValue<TItem>(System.ReadOnlySpan<char> key, out TItem? value) { throw null; }
#endif
Copy link
Member

Choose a reason for hiding this comment

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

@@ -211,9 +211,68 @@ public bool TryGetValue(object key, out object? result)
DateTime utcNow = UtcNow;

CoherentState coherentState = _coherentState; // Clear() can update the reference in the meantime
if (coherentState.TryGetValue(key, out CacheEntry? tmp))
coherentState.TryGetValue(key, out CacheEntry? entry); // note we rely on documented "default when fails" contract re the out
Copy link
Member

Choose a reason for hiding this comment

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

We could formalize the comment with an assert, e.g.

Suggested change
coherentState.TryGetValue(key, out CacheEntry? entry); // note we rely on documented "default when fails" contract re the out
bool success = coherentState.TryGetValue(key, out CacheEntry? entry);
Debug.Assert(success || entry is null, "We rely on documented 'default when fails' contract.");

/// <param name="value">The located value or null.</param>
/// <returns>True if the key was found.</returns>
/// <remarks>This method allows values with <see cref="string"/> keys to be queried by content without allocating a new <see cref="string"/> instance.</remarks>
[OverloadResolutionPriority(1)]
Copy link
Member

Choose a reason for hiding this comment

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

@333fred, @jaredpar, just FYI on more use of ORPA.

@mgravell mgravell merged commit 7e9343b into dotnet:main Feb 20, 2025
3 checks passed
@stephentoub
Copy link
Member

This didn't actually run CI

@stephentoub
Copy link
Member

It also merged with https://github.com/dotnet/runtime/pull/112695/files#r1963779247 unaddressed

@mgravell
Copy link
Member Author

mgravell commented Feb 20, 2025 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: M.E.C.M. MemoryCache - add ReadOnlySpan<char> get API
3 participants