From e47ccbd08aa707d02b7b532bdbc5c0d99f366902 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 15 Oct 2021 13:50:25 +0900 Subject: [PATCH] Fix realm transactional overhead when rapidly changing `RulesetConfigManager` values --- .../Configuration/RulesetConfigManager.cs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs b/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs index a0ec8e3e0e..901efc3155 100644 --- a/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs +++ b/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs @@ -47,9 +47,28 @@ namespace osu.Game.Rulesets.Configuration } } + private readonly HashSet pendingWrites = new HashSet(); + protected override bool PerformSave() { - // do nothing, realm saves immediately + TLookup[] changed; + + lock (pendingWrites) + { + changed = pendingWrites.ToArray(); + pendingWrites.Clear(); + } + + realmFactory?.CreateContext().Write(realm => + { + foreach (var c in changed) + { + var setting = realm.All().First(s => s.RulesetID == rulesetId && s.Variant == variant && s.Key == c.ToString()); + + setting.Value = ConfigStore[c].ToString(); + } + }); + return true; } @@ -80,7 +99,8 @@ namespace osu.Game.Rulesets.Configuration bindable.ValueChanged += b => { - realmFactory?.Context.Write(() => setting.Value = b.NewValue.ToString()); + lock (pendingWrites) + pendingWrites.Add(lookup); }; } }