diff --git a/RELEASE.md b/RELEASE.md index 4277ba5..a0f4836 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -35,3 +35,4 @@ It does help this project on being supported. - Fix crash on startup when Steam is missing - Fix crash on resume when X360 Controller is in use - Provide currated list of mapping keys in settings (might be extended in the future) +- Improve performance on critical loop (code is lock-less) diff --git a/SteamController/Context.cs b/SteamController/Context.cs index 2adbb1b..ebad5f1 100644 --- a/SteamController/Context.cs +++ b/SteamController/Context.cs @@ -128,63 +128,62 @@ namespace SteamController } } - public Profiles.Profile? FindProfile(String name) + private bool SelectProfile(Profiles.Profile profile) { - return Profiles.Find((profile) => profile.Name == name); + lock (this) + { + var list = OrderedProfiles.ToList(); + if (!list.Remove(profile)) + return false; + list.Insert(0, profile); + orderedProfiles = list; + RequestDesktopMode = profile.IsDesktop; + + if (profile.Selected(this)) + { + ProfileChanged(profile); + return true; + } + return false; + } } - public bool SelectProfile(String name, bool enable = true) + public bool SelectProfile(String name) { - var profile = FindProfile(name); + var profile = Profiles.Find((profile) => profile.Name == name); if (profile is null) return false; - var list = OrderedProfiles; - list.Remove(profile); - list.Insert(0, profile); - RequestDesktopMode = profile.IsDesktop; - - if (profile.Selected(this)) - ProfileChanged(profile); - return true; + return SelectProfile(profile); } public bool SelectNext() { - bool firstSelected = false; + var profile = OrderedProfiles. + Where((profile) => profile.Selected(this)). + Skip(1). + FirstOrDefault(); - var list = OrderedProfiles; - foreach (var profile in list) - { - if (!profile.Selected(this)) - continue; - - if (!firstSelected) - { - firstSelected = true; - continue; - } - - list.Remove(profile); - list.Insert(0, profile); - ProfileChanged(profile); - return true; - } + if (profile is not null) + return SelectProfile(profile); return false; } public void ToggleDesktopMode(bool? forceState = null) { - var oldProfile = GetCurrentProfile(); - if (forceState is null) - RequestDesktopMode = !RequestDesktopMode; - else - RequestDesktopMode = forceState.Value; + lock (this) + { + var oldProfile = GetCurrentProfile(); + if (forceState is null) + RequestDesktopMode = !RequestDesktopMode; + else + RequestDesktopMode = forceState.Value; - var newProfile = GetCurrentProfile(); - if (oldProfile != newProfile && newProfile is not null) - ProfileChanged(newProfile); + var newProfile = GetCurrentProfile(); + if (oldProfile != newProfile && newProfile is not null) + ProfileChanged(newProfile); + } } } } diff --git a/SteamController/Controller.cs b/SteamController/Controller.cs index 75328a1..bc91100 100644 --- a/SteamController/Controller.cs +++ b/SteamController/Controller.cs @@ -59,7 +59,7 @@ namespace SteamController continue; var profileItem = new ToolStripMenuItem(profile.Name); - profileItem.Click += delegate { lock (context) { context.SelectProfile(profile.Name); } }; + profileItem.Click += delegate { context.SelectProfile(profile.Name); }; contextMenu.Opening += delegate { profileItem.Checked = context.GetCurrentProfile() == profile; }; contextMenu.Items.Add(profileItem); } @@ -149,12 +149,8 @@ namespace SteamController } updatesReceived++; - - lock (context) - { - context.Update(); - context.Debug(); - } + context.Update(); + context.Debug(); if (!context.Enabled) { @@ -179,11 +175,8 @@ namespace SteamController private void ContextStateUpdate_Tick(object? sender, EventArgs e) { - lock (context) - { - context.Tick(); - SharedData_Update(); - } + context.Tick(); + SharedData_Update(); if (!context.Mouse.Valid) {