Skip to content
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

Reduce contention in ClockCache #7338

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions src/Nethermind/Nethermind.Core/Caching/ClockCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using Nethermind.Core.Threading;
Expand Down Expand Up @@ -74,7 +75,8 @@ private bool SetSlow(TKey key, TValue val)
return false;
}

int offset = _cacheMap.Count;
int offset = _count;
Debug.Assert(_cacheMap.Count == _count);
if (FreeOffsets.Count > 0)
{
offset = FreeOffsets.Dequeue();
Expand All @@ -86,14 +88,16 @@ private bool SetSlow(TKey key, TValue val)

_cacheMap[key] = new LruCacheItem(offset, val);
KeyToOffset[offset] = key;
_count++;
Debug.Assert(_cacheMap.Count == _count);

return true;
}

private int Replace(TKey key)
{
int position = Clock;
int max = _cacheMap.Count;
int max = _count;
while (true)
{
if (position >= max)
Expand All @@ -108,6 +112,7 @@ private int Replace(TKey key)
{
ThrowInvalidOperationException();
}
_count--;
break;
}

Expand All @@ -132,6 +137,7 @@ public bool Delete(TKey key)

if (_cacheMap.Remove(key, out LruCacheItem? ov))
{
_count--;
KeyToOffset[ov.Offset] = default;
ClearAccessed(ov.Offset);
FreeOffsets.Enqueue(ov.Offset);
Expand All @@ -157,7 +163,7 @@ public bool Contains(TKey key)
return _cacheMap.ContainsKey(key);
}

public int Count => _cacheMap.Count;
public int Count => _count;

private class LruCacheItem(int offset, TValue v)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Core/Caching/ClockCacheBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public abstract class ClockCacheBase<TKey>
protected Queue<int> FreeOffsets { get; } = new();

protected int Clock { get; set; } = 0;
// Use local count to avoid lock contention with reads on ConcurrentDictionary.Count
protected int _count = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why introduce _count here while it's touched only by descendant classes?


protected ClockCacheBase(int maxCapacity)
{
Expand All @@ -35,6 +37,7 @@ protected void Clear()
{
if (MaxCapacity == 0) return;

_count = 0;
Clock = 0;
FreeOffsets.Clear();
KeyToOffset.AsSpan().Clear();
Expand Down
12 changes: 9 additions & 3 deletions src/Nethermind/Nethermind.Core/Caching/ClockKeyCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using Nethermind.Core.Threading;
Expand Down Expand Up @@ -51,7 +52,8 @@ private bool SetSlow(TKey key)
return false;
}

offset = _cacheMap.Count;
offset = _count;
Debug.Assert(_cacheMap.Count == _count);
if (FreeOffsets.Count > 0)
{
offset = FreeOffsets.Dequeue();
Expand All @@ -63,14 +65,16 @@ private bool SetSlow(TKey key)

_cacheMap[key] = offset;
KeyToOffset[offset] = key;
_count++;
Debug.Assert(_cacheMap.Count == _count);

return true;
}

private int Replace(TKey key)
{
int position = Clock;
int max = _cacheMap.Count;
int max = _count;
while (true)
{
if (position >= max)
Expand All @@ -85,6 +89,7 @@ private int Replace(TKey key)
{
ThrowInvalidOperationException();
}
_count--;
break;
}

Expand All @@ -108,6 +113,7 @@ public bool Delete(TKey key)

if (_cacheMap.Remove(key, out int offset))
{
_count--;
ClearAccessed(offset);
FreeOffsets.Enqueue(offset);
return true;
Expand All @@ -131,5 +137,5 @@ public bool Contains(TKey key)
return _cacheMap.ContainsKey(key);
}

public int Count => _cacheMap.Count;
public int Count => _count;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public int Count => _count;
public int Count => Volatile.Read(ref _count);

}