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

Widget, avoid copying child list when reverse iterating. #20151

Merged
merged 1 commit into from Jul 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions OpenRA.Game/Widgets/Widget.cs
Expand Up @@ -378,9 +378,10 @@ public string GetCursorOuter(int2 pos)
return null;

// Do any of our children specify a cursor?
foreach (var child in Children.OfType<Widget>().Reverse())
// PERF: Avoid LINQ.
for (var i = Children.Count - 1; i >= 0; --i)
{
var cc = child.GetCursorOuter(pos);
var cc = Children[i].GetCursorOuter(pos);
if (cc != null)
return cc;
}
Expand All @@ -405,8 +406,9 @@ public bool HandleMouseInputOuter(MouseInput mi)
var oldMouseOver = Ui.MouseOverWidget;

// Send the event to the deepest children first and bubble up if unhandled
foreach (var child in Children.OfType<Widget>().Reverse())
if (child.HandleMouseInputOuter(mi))
// PERF: Avoid LINQ.
for (var i = Children.Count - 1; i >= 0; --i)
if (Children[i].HandleMouseInputOuter(mi))
return true;

if (IgnoreChildMouseOver)
Expand All @@ -426,8 +428,9 @@ public virtual bool HandleKeyPressOuter(KeyInput e)
return false;

// Can any of our children handle this?
foreach (var child in Children.OfType<Widget>().Reverse())
if (child.HandleKeyPressOuter(e))
// PERF: Avoid LINQ.
for (var i = Children.Count - 1; i >= 0; --i)
if (Children[i].HandleKeyPressOuter(e))
return true;

// Do any widgety behavior
Expand All @@ -444,8 +447,9 @@ public virtual bool HandleTextInputOuter(string text)
return false;

// Can any of our children handle this?
foreach (var child in Children.OfType<Widget>().Reverse())
if (child.HandleTextInputOuter(text))
// PERF: Avoid LINQ.
for (var i = Children.Count - 1; i >= 0; --i)
if (Children[i].HandleTextInputOuter(text))
return true;

// Do any widgety behavior (enter text etc)
Expand Down Expand Up @@ -531,8 +535,9 @@ public virtual void Hidden()
ForceYieldKeyboardFocus();
ForceYieldMouseFocus();

foreach (var c in Children.OfType<Widget>().Reverse())
c.Hidden();
// PERF: Avoid LINQ.
for (var i = Children.Count - 1; i >= 0; --i)
Children[i].Hidden();
}

public virtual void Removed()
Expand All @@ -542,8 +547,9 @@ public virtual void Removed()
ForceYieldKeyboardFocus();
ForceYieldMouseFocus();

foreach (var c in Children.OfType<Widget>().Reverse())
c.Removed();
// PERF: Avoid LINQ.
for (var i = Children.Count - 1; i >= 0; --i)
Children[i].Removed();

if (LogicObjects != null)
{
Expand Down