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

Improved graph rendering performance by reducing locks in settingscache #5540

Merged
Merged
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
58 changes: 29 additions & 29 deletions GitCommands/Settings/SettingsCache.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace GitCommands
{
public abstract class SettingsCache : IDisposable
{
private readonly Dictionary<string, object> _byNameMap = new Dictionary<string, object>();
private readonly ConcurrentDictionary<string, object> _byNameMap = new ConcurrentDictionary<string, object>();

public void Dispose()
{
Expand Down Expand Up @@ -149,51 +150,50 @@ public void SetValue<T>(string name, T value, Func<T, string> encode)
{
SetValue(name, s);

_byNameMap[name] = s == null ? (object)null : value;
_byNameMap.AddOrUpdate(name, value, (key, oldValue) => value);
});
}

// This method will attempt to get the value from cache first. If the setting is not cached, it will call GetValue.
// GetValue will not look in the cache. This method doesn't require a lock. A lock is only required when GetValue is
// called. GetValue will set the lock.
public bool TryGetValue<T>([NotNull] string name, T defaultValue, [NotNull] Func<string, T> decode, out T value)
{
if (decode == null)
{
throw new ArgumentNullException(nameof(decode), $"The decode parameter for setting {name} is null.");
}

T val = defaultValue;
value = defaultValue;

bool result = LockedAction(() =>
{
EnsureSettingsAreUpToDate();
EnsureSettingsAreUpToDate();

if (_byNameMap.TryGetValue(name, out object o))
if (_byNameMap.TryGetValue(name, out object o))
{
switch (o)
{
switch (o)
{
case null:
return false;
case T t:
val = t;
return true;
default:
throw new Exception("Incompatible class for settings: " + name + ". Expected: " + typeof(T).FullName + ", found: " + o.GetType().FullName);
}
case null:
return false;
case T t:
value = t;
return true;
default:
throw new Exception("Incompatible class for settings: " + name + ". Expected: " + typeof(T).FullName + ", found: " + o.GetType().FullName);
}
}

string s = GetValue(name);
string s = GetValue(name);

if (s == null)
{
val = defaultValue;
return false;
}
if (s == null)
{
value = defaultValue;
return false;
}

val = decode(s);
_byNameMap[name] = val;
return true;
});
value = val;
return result;
T decodedValue = decode(s);
value = decodedValue;
_byNameMap.AddOrUpdate(name, decodedValue, (key, oldValue) => decodedValue);
return true;
}
}
}