Skip to content

Commit 7641f70

Browse files
committed
Reduce allocation overhead of KeyCombination constructor
1 parent d16d899 commit 7641f70

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

osu.Framework/Input/Bindings/KeyCombination.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,20 @@ namespace osu.Framework.Input.Bindings
2929
/// Construct a new instance.
3030
/// </summary>
3131
/// <param name="keys">The keys.</param>
32-
/// <remarks>This constructor is not optimized. Hot paths are assumed to use <see cref="FromInputState(InputState, Vector2?)"/>.</remarks>
3332
public KeyCombination(IEnumerable<InputKey>? keys)
3433
{
35-
Keys = keys?.Any() == true ? keys.Distinct().OrderBy(k => (int)k).ToImmutableArray() : none;
34+
if (keys == null || !keys.Any())
35+
{
36+
Keys = ImmutableArray<InputKey>.Empty;
37+
return;
38+
}
39+
40+
var keyBuilder = ImmutableArray.CreateBuilder<InputKey>(keys.Count());
41+
42+
keyBuilder.AddRange(keys);
43+
keyBuilder.Sort();
44+
45+
Keys = keyBuilder.MoveToImmutable();
3646
}
3747

3848
/// <summary>
@@ -643,8 +653,7 @@ public static KeyCombination FromInputState(InputState state, Vector2? scrollDel
643653
}
644654

645655
Debug.Assert(!keys.Contains(InputKey.None)); // Having None in pressed keys will break IsPressed
646-
keys.Sort();
647-
return new KeyCombination(keys.ToImmutable());
656+
return new KeyCombination(keys);
648657
}
649658
}
650659

0 commit comments

Comments
 (0)