8000 Remove double lookups on SizeLimitedCache<K, V>, improve performance by h3xds1nz · Pull Request #9949 · dotnet/wpf · GitHub
[go: up one dir, main page]

Skip to content

Remove double lookups on SizeLimitedCache<K, V>, improve performance #9949

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 8 commits into from
Mar 6, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix double lookup on Add
  • Loading branch information
h3xds1nz committed Feb 28, 2025
commit 6bb5f47b6266eea8ec1db417db182e56058144ad
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,13 @@ public int MaximumItems
/// </param>
public void Add(K key, V resource, bool isPermanent)
{

if ( (object)key == null)
{
throw new ArgumentNullException(nameof(key));
}
if ( (object)resource == null)
{
throw new ArgumentNullException(nameof(resource));
}
ArgumentNullException.ThrowIfNull(key, nameof(key));
ArgumentNullException.ThrowIfNull(resource, nameof(resource));

// note: [] throws, thus we should check if its in the dictionary first.
if (!_nodeLookup.ContainsKey(key))
if (!_nodeLookup.TryGetValue(key, out Node node))
{
Node node = new Node(key, resource, isPermanent);
node = new Node(key, resource, isPermanent);
if (!isPermanent)
{
if (IsFull())
Expand All @@ -111,11 +104,11 @@ public void Add(K key, V resource, bool isPermanent)
{
_permanentCount++;
}

_nodeLookup[key] = node;
}
else
{
Node node = _nodeLookup[key];
if (!node.IsPermanent)
{
RemoveFromList(node);
Expand Down Expand Up @@ -151,27 +144,17 @@ public void Add(K key, V resource, bool isPermanent)
/// </param>
public void Remove(K key)
{
if ( (object)key == null)
{
throw new ArgumentNullException(nameof(key));
}
ArgumentNullException.ThrowIfNull(key, nameof(key));

// note: [] throws, thus we should check if its in the dictionary first.
if (!_nodeLookup.ContainsKey(key))
{
if (!_nodeLookup.TryGetValue(key, out Node node))
return;
}
Node node = _nodeLookup[key];

_nodeLookup.Remove(key);

if (!node.IsPermanent)
{
RemoveFromList(node);
}
else
{
_permanentCount--;
}
}

/// <summary>
Expand Down
0