Skip to content

Commit

Permalink
ConfigFriendlyParams: profiling speed improvements
Browse files Browse the repository at this point in the history
ParameterMetaDataRepositoryAPMpdef: allocation improvements
  • Loading branch information
meee1 committed Jul 20, 2023
1 parent a73932f commit 134e3ea
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 108 deletions.
11 changes: 11 additions & 0 deletions ExtLibs/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,17 @@ public static void ForEach<T>(this ReadOnlySpan<T> span, Action<T> action)
}
}

public static IEnumerable<T1> OrderedParallel<T, T1>(this IEnumerable<T> list, Func<T, T1> action)
{
var unorderedResult = new ConcurrentBag<(long, T1)>();
Parallel.ForEach(list, (o, state, i) =>
{
unorderedResult.Add((i, action.Invoke(o)));
});
var ordered = unorderedResult.OrderBy(o => o.Item1);
return ordered.Select(o => o.Item2);
}

public static IEnumerable<TOut> Select<T,TOut>(this ReadOnlySpan<T> span, Func<T,TOut> action)
{
List<TOut> list = new List<TOut>();
Expand Down
3 changes: 2 additions & 1 deletion ExtLibs/Utilities/ParameterMetaDataRepositoryAPMpdef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public static string GetParameterMetaData(string nodeKey, string metaKey, string
{
try
{
var vechileKey = vechileType + ":" + nodeKey;
foreach (var paramfile in _parameterMetaDataXML[vechileType].Element("paramfile").Elements())
{
foreach (var parameters in paramfile.Elements())
Expand All @@ -157,7 +158,7 @@ public static string GetParameterMetaData(string nodeKey, string metaKey, string
{
foreach (var param in parameters.Elements())
{
if (param.Attribute("name").Value == (vechileType + ":" + nodeKey) ||
if (param.Attribute("name").Value == vechileKey ||
param.Attribute("name").Value == nodeKey)
{
if (param.Attribute(metaKey) != null)
Expand Down
12 changes: 6 additions & 6 deletions GCSViews/ConfigurationView/ConfigFriendlyParams.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 46 additions & 81 deletions GCSViews/ConfigurationView/ConfigFriendlyParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

Expand Down Expand Up @@ -59,10 +60,10 @@ void filterList(string searchfor)
if (searchfor.Length >= 2 || searchfor.Length == 0)
{
y = 10;
tableLayoutPanel1.Enabled = false;
tableLayoutPanel1.SuspendLayout();
flowLayoutPanel1.Enabled = false;
flowLayoutPanel1.SuspendLayout();

foreach (Control ctl in tableLayoutPanel1.Controls)
foreach (Control ctl in flowLayoutPanel1.Controls)
{
if (ctl.GetType() == typeof(RangeControl))
{
Expand Down Expand Up @@ -111,8 +112,8 @@ void filterList(string searchfor)
}
}

tableLayoutPanel1.ResumeLayout();
tableLayoutPanel1.Enabled = true;
flowLayoutPanel1.ResumeLayout();
flowLayoutPanel1.Enabled = true;
}
}

Expand Down Expand Up @@ -145,7 +146,7 @@ void filterList(string searchfor)
public ConfigFriendlyParams()
{
InitializeComponent();
tableLayoutPanel1.Height = Height;
flowLayoutPanel1.Height = Height;

Resize += this_Resize;

Expand Down Expand Up @@ -241,7 +242,7 @@ protected void BUT_rerequestparams_Click(object sender, EventArgs e)
/// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
protected void this_Resize(object sender, EventArgs e)
{
tableLayoutPanel1.Height = Height - 50;
flowLayoutPanel1.Height = Height - 50;
}

/// <summary>
Expand All @@ -258,30 +259,21 @@ public void Activate()
Console.WriteLine("Activate Done " + DateTime.Now.ToString("ss.fff"));
}

/// <summary>
/// Handles the ParamListChanged event of the comPort control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
protected void comPort_ParamListChanged(object sender, EventArgs e)
{
SortParamList();
}

#endregion

#region Methods

/// <summary>
/// Sorts the param list.
/// </summary>
private void SortParamList()
private void FilterParamList()
{
// Clear list
_params.Clear();
var locker = new object();

// When the parameter list is changed, re sort the list for our View's purposes
MainV2.comPort.MAV.param.Keys.ForEach(x =>
Parallel.ForEach(MainV2.comPort.MAV.param.Keys, x =>
{
var displayName = ParameterMetaDataRepository.GetParameterMetaData(x.ToString(),
ParameterMetaDataConstants.DisplayName, MainV2.comPort.MAV.cs.firmware.ToString());
Expand All @@ -295,87 +287,57 @@ private void SortParamList()
// The user type is empty and this is in Advanced mode
string.IsNullOrEmpty(parameterMode) && ParameterMode == ParameterMetaDataConstants.Advanced))
{
_params.Add(x.ToString(), displayName);
lock (locker)
_params.Add(x.ToString(), displayName);
}
});
_params = _params.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
}

/// <summary>
/// Binds the param list.
/// </summary>
private void BindParamList()
{
//this.Visible = true;

// fix memory leak
foreach (Control ctl in tableLayoutPanel1.Controls)
{
// ctl.Visible = true;
// ctl.Dispose();
}

Console.WriteLine("Disposed " + DateTime.Now.ToString("ss.fff"));

// tableLayoutPanel1.Controls.Clear();

Console.WriteLine("BindParamList " + DateTime.Now.ToString("ss.fff"));

try
{
SortParamList();
FilterParamList();
Console.WriteLine("Sorted " + DateTime.Now.ToString("ss.fff"));
}
catch
{
}

// get the params if nothing exists already
if (_params != null && _params.Count == 0)
{
try
{
//Utilities.ParameterMetaDataParser.GetParameterInformation();
//ParameterMetaDataRepository.Reload();
//SortParamList();
}
catch (Exception exp)
{
log.Error(exp);
} // just to cleanup any errors
}

Console.WriteLine("next " + DateTime.Now.ToString("ss.fff"));

tableLayoutPanel1.VerticalScroll.Value = 0;

SuspendLayout();
flowLayoutPanel1.VerticalScroll.Value = 0;

var toadd = new List<Control>();

var list = Settings.Instance.GetList("fav_params");
var favlist = Settings.Instance.GetList("fav_params");

_params.OrderBy(x =>
{
if (list.Contains(x.Key))
return "0" + x.Key;
return x.Key;
}).ForEach(x =>
Parallel.ForEach(_params, x =>
{
AddControl(x, toadd); //,ref ypos);
AddControl(x, toadd);
Console.WriteLine("add ctl " + x.Key + " " + DateTime.Now.ToString("ss.fff"));
});

tableLayoutPanel1.SuspendLayout();
tableLayoutPanel1.Visible = false;
tableLayoutPanel1.Controls.AddRange(toadd.ToArray());
tableLayoutPanel1.Visible = true;
tableLayoutPanel1.ResumeLayout();

Console.WriteLine("Add done" + DateTime.Now.ToString("ss.fff"));
// sort and favs
toadd = toadd.OrderBy(x =>
{
if (favlist.Contains(x.Name))
return "0" + x.Name;
return x.Name;
}).ToList();

ResumeLayout(false);
flowLayoutPanel1.Visible = false;
flowLayoutPanel1.Controls.AddRange(toadd.ToArray());
flowLayoutPanel1.Visible = true;

Console.WriteLine("Add done" + DateTime.Now.ToString("ss.fff"));
}

private object locker = new object();

private void AddControl(KeyValuePair<string, string> x, List<Control> toadd) //, ref int ypos)
{
if (!string.IsNullOrEmpty(x.Key))
Expand All @@ -386,7 +348,7 @@ private void AddControl(KeyValuePair<string, string> x, List<Control> toadd) //,

var value = (MainV2.comPort.MAV.param[x.Key].Value).ToString("0.###");

var items = tableLayoutPanel1.Controls.Find(x.Key, false);
var items = flowLayoutPanel1.Controls.Find(x.Key, false);
if (items.Length > 0)
{
if (items[0].GetType() == typeof(RangeControl))
Expand Down Expand Up @@ -463,12 +425,12 @@ private void AddControl(KeyValuePair<string, string> x, List<Control> toadd) //,
// increment /= 100;
}

var desc = FitDescriptionText(units, description, tableLayoutPanel1.Width);
var desc = FitDescriptionText(units, description, flowLayoutPanel1.Width);

var rangeControl = new RangeControl(x.Key, desc, displayName, increment, displayscale,
lowerRange, upperRange, value);

rangeControl.Width = tableLayoutPanel1.Width - 50;
rangeControl.Width = flowLayoutPanel1.Width - 50;

//Console.WriteLine("{0} {1} {2} {3} {4}", x.Key, increment, lowerRange, upperRange, value);

Expand All @@ -487,7 +449,8 @@ private void AddControl(KeyValuePair<string, string> x, List<Control> toadd) //,
// set pos
// rangeControl.Location = new Point(0, y);
// add control - let it autosize height
toadd.Add(rangeControl);
lock(locker)
toadd.Add(rangeControl);
// add height for next control
y += rangeControl.Height;

Expand All @@ -507,15 +470,16 @@ private void AddControl(KeyValuePair<string, string> x, List<Control> toadd) //,
bitmask.setup(x.Key, MainV2.comPort.MAV.param);

bitmask.myLabel1.Text = displayName;
bitmask.label1.Text = FitDescriptionText(units, description, tableLayoutPanel1.Width - 50);
bitmask.Width = tableLayoutPanel1.Width - 50;
bitmask.label1.Text = FitDescriptionText(units, description, flowLayoutPanel1.Width - 50);
bitmask.Width = flowLayoutPanel1.Width - 50;

ThemeManager.ApplyThemeTo(bitmask);

// set pos
//bitmask.Location = new Point(0, y);
// add control - let it autosize height
toadd.Add(bitmask);
lock (locker)
toadd.Add(bitmask);
// add height for next control
y += bitmask.Height;

Expand All @@ -536,10 +500,10 @@ private void AddControl(KeyValuePair<string, string> x, List<Control> toadd) //,
if (availableValues.Any())
{
var valueControl = new ValuesControl();
valueControl.Width = tableLayoutPanel1.Width - 50;
valueControl.Width = flowLayoutPanel1.Width - 50;
valueControl.Name = x.Key;
valueControl.DescriptionText = FitDescriptionText(units, description,
tableLayoutPanel1.Width);
flowLayoutPanel1.Width);
valueControl.LabelText = displayName;

ThemeManager.ApplyThemeTo(valueControl);
Expand All @@ -563,7 +527,8 @@ private void AddControl(KeyValuePair<string, string> x, List<Control> toadd) //,
// set pos
//valueControl.Location = new Point(0, y);
// add control - let it autosize height
toadd.Add(valueControl);
lock (locker)
toadd.Add(valueControl);
// add height for next control
y += valueControl.Height;
}
Expand Down
20 changes: 10 additions & 10 deletions GCSViews/ConfigurationView/ConfigFriendlyParams.ko-KR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,33 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="tableLayoutPanel1.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms">
<data name="flowLayoutPanel1.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms">
<value>Top, Bottom, Left, Right</value>
</data>
<assembly alias="mscorlib" name="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="tableLayoutPanel1.AutoScroll" type="System.Boolean, mscorlib">
<data name="flowLayoutPanel1.AutoScroll" type="System.Boolean, mscorlib">
<value>True</value>
</data>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="tableLayoutPanel1.Location" type="System.Drawing.Point, System.Drawing">
<data name="flowLayoutPanel1.Location" type="System.Drawing.Point, System.Drawing">
<value>12, 36</value>
</data>
<data name="tableLayoutPanel1.Size" type="System.Drawing.Size, System.Drawing">
<data name="flowLayoutPanel1.Size" type="System.Drawing.Size, System.Drawing">
<value>626, 145</value>
</data>
<data name="tableLayoutPanel1.TabIndex" type="System.Int32, mscorlib">
<data name="flowLayoutPanel1.TabIndex" type="System.Int32, mscorlib">
<value>0</value>
</data>
<data name="&gt;&gt;tableLayoutPanel1.Name" xml:space="preserve">
<value>tableLayoutPanel1</value>
<data name="&gt;&gt;flowLayoutPanel1.Name" xml:space="preserve">
<value>flowLayoutPanel1</value>
</data>
<data name="&gt;&gt;tableLayoutPanel1.Type" xml:space="preserve">
<data name="&gt;&gt;flowLayoutPanel1.Type" xml:space="preserve">
<value>System.Windows.Forms.Panel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;tableLayoutPanel1.Parent" xml:space="preserve">
<data name="&gt;&gt;flowLayoutPanel1.Parent" xml:space="preserve">
<value>$this</value>
</data>
<data name="&gt;&gt;tableLayoutPanel1.ZOrder" xml:space="preserve">
<data name="&gt;&gt;flowLayoutPanel1.ZOrder" xml:space="preserve">
<value>4</value>
</data>
<data name="BUT_rerequestparams.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
Expand Down
Loading

0 comments on commit 134e3ea

Please sign in to comment.