Skip to content

Commit

Permalink
Reduce allocation overhead of KeyCombination constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Jan 11, 2024
1 parent d16d899 commit 7641f70
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions osu.Framework/Input/Bindings/KeyCombination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ namespace osu.Framework.Input.Bindings
/// Construct a new instance.
/// </summary>
/// <param name="keys">The keys.</param>
/// <remarks>This constructor is not optimized. Hot paths are assumed to use <see cref="FromInputState(InputState, Vector2?)"/>.</remarks>
public KeyCombination(IEnumerable<InputKey>? keys)
{
Keys = keys?.Any() == true ? keys.Distinct().OrderBy(k => (int)k).ToImmutableArray() : none;
if (keys == null || !keys.Any())
{
Keys = ImmutableArray<InputKey>.Empty;
return;
}

var keyBuilder = ImmutableArray.CreateBuilder<InputKey>(keys.Count());

keyBuilder.AddRange(keys);
keyBuilder.Sort();

Keys = keyBuilder.MoveToImmutable();
}

/// <summary>
Expand Down Expand Up @@ -643,8 +653,7 @@ public static KeyCombination FromInputState(InputState state, Vector2? scrollDel
}

Debug.Assert(!keys.Contains(InputKey.None)); // Having None in pressed keys will break IsPressed
keys.Sort();
return new KeyCombination(keys.ToImmutable());
return new KeyCombination(keys);
}
}

Expand Down

0 comments on commit 7641f70

Please sign in to comment.