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

Add a change list to optimize datacache commit #1619

Merged
merged 14 commits into from
May 7, 2020
20 changes: 18 additions & 2 deletions src/neo/IO/Caching/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Trackable
}

private readonly Dictionary<TKey, Trackable> dictionary = new Dictionary<TKey, Trackable>();
private readonly HashSet<TKey> changeSet = new HashSet<TKey>();

public TValue this[TKey key]
{
Expand Down Expand Up @@ -65,6 +66,7 @@ public void Add(TKey key, TValue value)
Item = value,
State = trackable == null ? TrackState.Added : TrackState.Changed
};
changeSet.Add(key);
}
}

Expand Down Expand Up @@ -96,6 +98,7 @@ public void Commit()
{
dictionary.Remove(key);
}
changeSet.Clear();
}

public DataCache<TKey, TValue> CreateSnapshot()
Expand All @@ -114,9 +117,15 @@ public void Delete(TKey key)
if (dictionary.TryGetValue(key, out Trackable trackable))
{
if (trackable.State == TrackState.Added)
{
dictionary.Remove(key);
changeSet.Remove(key);
}
else
{
trackable.State = TrackState.Deleted;
changeSet.Add(key);
}
}
else
{
Expand All @@ -128,6 +137,7 @@ public void Delete(TKey key)
Item = item,
State = TrackState.Deleted
});
changeSet.Add(key);
}
}
}
Expand All @@ -139,7 +149,10 @@ public void DeleteWhere(Func<TKey, TValue, bool> predicate)
lock (dictionary)
{
foreach (Trackable trackable in dictionary.Where(p => p.Value.State != TrackState.Deleted && predicate(p.Key, p.Value.Item)).Select(p => p.Value))
{
changeSet.Add(trackable.Key);
trackable.State = TrackState.Deleted;
}
}
}

Expand Down Expand Up @@ -204,8 +217,8 @@ public IEnumerable<Trackable> GetChangeSet()
{
lock (dictionary)
{
foreach (Trackable trackable in dictionary.Values.Where(p => p.State != TrackState.None))
yield return trackable;
foreach (TKey key in changeSet)
yield return dictionary[key];
}
}

Expand Down Expand Up @@ -234,6 +247,7 @@ public TValue GetAndChange(TKey key, Func<TValue> factory = null)
else if (trackable.State == TrackState.None)
{
trackable.State = TrackState.Changed;
changeSet.Add(key);
}
}
else
Expand All @@ -254,6 +268,7 @@ public TValue GetAndChange(TKey key, Func<TValue> factory = null)
trackable.State = TrackState.Changed;
}
dictionary.Add(key, trackable);
changeSet.Add(key);
}
return trackable.Item;
}
Expand Down Expand Up @@ -282,6 +297,7 @@ public TValue GetOrAdd(TKey key, Func<TValue> factory)
{
trackable.Item = factory();
trackable.State = TrackState.Added;
changeSet.Add(key);
}
else
{
Expand Down