Skip to content

Optimize ConcurrentLru read throughput #645

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 2 commits into from
Nov 20, 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
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/AfterAccessPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public LongTickCountLruItem<K, V> CreateItem(K key, V value)
public void Touch(LongTickCountLruItem<K, V> item)
{
item.TickCount = this.time.Last;
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/DiscretePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Touch(LongTickCountLruItem<K, V> item)
var currentExpiry = new Duration(item.TickCount - this.time.Last);
var newExpiry = expiry.GetExpireAfterRead(item.Key, item.Value, currentExpiry);
item.TickCount = this.time.Last + newExpiry.raw;
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
15 changes: 13 additions & 2 deletions BitFaster.Caching/Lru/LruItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class LruItem<K, V>
{
private V data;

private volatile bool wasAccessed;
private volatile bool wasRemoved;
private bool wasAccessed;
private bool wasRemoved;

// only used when V is a non-atomic value type to prevent torn reads
private int sequence;
Expand Down Expand Up @@ -85,6 +85,17 @@ public bool WasRemoved
set => this.wasRemoved = value;
}

/// <summary>
/// Marks the item as accessed, if it was not already accessed.
/// </summary>
public void MarkAccessed()
{
if (!this.wasAccessed)
{
this.wasAccessed = true;
}
}

internal V SeqLockRead()
{
var spin = new SpinWait();
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/LruPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public LruItem<K, V> CreateItem(K key, V value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(LruItem<K, V> item)
{
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/TLruLongTicksPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public LongTickCountLruItem<K, V> CreateItem(K key, V value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(LongTickCountLruItem<K, V> item)
{
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/TlruDateTimePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public TimeStampedLruItem<K, V> CreateItem(K key, V value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(TimeStampedLruItem<K, V> item)
{
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/TlruTicksPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public TickCountLruItem<K, V> CreateItem(K key, V value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Touch(TickCountLruItem<K, V> item)
{
item.WasAccessed = true;
item.MarkAccessed();
}

///<inheritdoc/>
Expand Down
Loading