Skip to content

Commit

Permalink
Merge pull request #426 from marco-carvalho/performance
Browse files Browse the repository at this point in the history
Preventing duplicate casts and optimize collection checks
  • Loading branch information
hellzerg authored Dec 4, 2023
2 parents ec7907c + e10c472 commit 7757f45
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Optimizer/DebugHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static void FindDifferenceInTwoJsons()
var p1 = file1.Properties().ToList();
var p2 = file2.Properties().ToList();

var missingProps = p1.Where(expected => p2.Where(actual => actual.Name == expected.Name).Count() == 0);
var missingProps = p1.Where(expected => !p2.Where(actual => actual.Name == expected.Name).Any());

StringBuilder sb = new StringBuilder();
foreach (var x in missingProps)
Expand Down
26 changes: 13 additions & 13 deletions Optimizer/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1192,42 +1192,42 @@ private void LoadNetworkAdapterConfig()
if (_currentDNS == null) return;
if (_currentDNS.Length == 0) return;

if (PingerHelper.CloudflareDNSv4.Any(x => _currentDNS.Select(y => y.ToString()).Contains(x)))
if (Array.Exists(PingerHelper.CloudflareDNSv4, x => _currentDNS.Select(y => y.ToString()).Contains(x)))
{
boxDNS.Text = Constants.CloudflareDNS;
return;
}
else if (PingerHelper.OpenDNSv4.Any(x => _currentDNS.Select(y => y.ToString()).Contains(x)))
else if (Array.Exists(PingerHelper.OpenDNSv4, x => _currentDNS.Select(y => y.ToString()).Contains(x)))
{
boxDNS.Text = Constants.OpenDNS;
return;
}
else if (PingerHelper.Quad9DNSv4.Any(x => _currentDNS.Select(y => y.ToString()).Contains(x)))
else if (Array.Exists(PingerHelper.Quad9DNSv4, x => _currentDNS.Select(y => y.ToString()).Contains(x)))
{
boxDNS.Text = Constants.Quad9DNS;
return;
}
else if (PingerHelper.GoogleDNSv4.Any(x => _currentDNS.Select(y => y.ToString()).Contains(x)))
else if (Array.Exists(PingerHelper.GoogleDNSv4, x => _currentDNS.Select(y => y.ToString()).Contains(x)))
{
boxDNS.Text = Constants.GoogleDNS;
return;
}
else if (PingerHelper.AlternateDNSv4.Any(x => _currentDNS.Select(y => y.ToString()).Contains(x)))
else if (Array.Exists(PingerHelper.AlternateDNSv4, x => _currentDNS.Select(y => y.ToString()).Contains(x)))
{
boxDNS.Text = Constants.AlternateDNS;
return;
}
else if (PingerHelper.AdguardDNSv4.Any(x => _currentDNS.Select(y => y.ToString()).Contains(x)))
else if (Array.Exists(PingerHelper.AdguardDNSv4, x => _currentDNS.Select(y => y.ToString()).Contains(x)))
{
boxDNS.Text = Constants.AdguardDNS;
return;
}
else if (PingerHelper.CleanBrowsingDNSv4.Any(x => _currentDNS.Select(y => y.ToString()).Contains(x)))
else if (Array.Exists(PingerHelper.CleanBrowsingDNSv4, x => _currentDNS.Select(y => y.ToString()).Contains(x)))
{
boxDNS.Text = Constants.CleanBrowsingDNS;
return;
}
else if (PingerHelper.CleanBrowsingAdultDNSv4.Any(x => _currentDNS.Select(y => y.ToString()).Contains(x)))
else if (Array.Exists(PingerHelper.CleanBrowsingAdultDNSv4, x => _currentDNS.Select(y => y.ToString()).Contains(x)))
{
boxDNS.Text = Constants.CleanBrowsingAdultFilterDNS;
return;
Expand Down Expand Up @@ -1318,7 +1318,7 @@ private void ApplyCustomDNS()
txtDns6B.Text
};

if (customDns4.Any(x => string.IsNullOrEmpty(x)) || customDns6.Any(x => string.IsNullOrEmpty(x)))
if (Array.Exists(customDns4, x => string.IsNullOrEmpty(x)) || Array.Exists(customDns6, x => string.IsNullOrEmpty(x)))
{
return;
}
Expand Down Expand Up @@ -2223,9 +2223,9 @@ private void Translate(bool skipFull = false)

if (element == null) continue;

if (element is ToggleCard)
if (element is ToggleCard tc)
{
((ToggleCard)element).LabelText = x.Value;
tc.LabelText = x.Value;
continue;
}

Expand Down Expand Up @@ -3352,7 +3352,7 @@ private void chkSelectAllModernApps_CheckedChanged(object sender, EventArgs e)
{
foreach (Control c in Utilities.GetSelfAndChildrenRecursive(panelUwp))
{
if (c is MoonCheck) ((MoonCheck)c).Checked = chkSelectAllModernApps.Checked;
if (c is MoonCheck mc) mc.Checked = chkSelectAllModernApps.Checked;
}
}

Expand Down Expand Up @@ -3833,7 +3833,7 @@ private async void btnDownloadApps_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(x.Tag)) continue;
temp = appsTab.Controls.Find(x.Tag, true);
if (temp.Count() == 0) continue;
if (!temp.Any()) continue;
currentCheck = (MoonCheck)temp[0];
if (currentCheck == null) continue;
if (!currentCheck.Checked) continue;
Expand Down
2 changes: 1 addition & 1 deletion Optimizer/Forms/StartupRestoreForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void RefreshBackups()
listRestoreItems.Items.Add(Path.GetFileNameWithoutExtension(x));
}

if (_backups.Count() > 0) listRestoreItems.SelectedIndex = 0;
if (_backups.Any()) listRestoreItems.SelectedIndex = 0;
}

private void Translate()
Expand Down
4 changes: 2 additions & 2 deletions Optimizer/SilentOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ internal static void ProcessPinger()
{
bool atLeastOnePrimary4 = CurrentSilentConfig.Pinger.CustomDNSv4.Length > 0 && CurrentSilentConfig.Pinger.CustomDNSv4.Length < 3;
bool atLeastOnePrimary6 = CurrentSilentConfig.Pinger.CustomDNSv6.Length > 0 && CurrentSilentConfig.Pinger.CustomDNSv6.Length < 3;
bool notEmptyDNS4 = CurrentSilentConfig.Pinger.CustomDNSv4.Any(x => !string.IsNullOrEmpty(x));
bool notEmptyDNS6 = CurrentSilentConfig.Pinger.CustomDNSv6.Any(x => !string.IsNullOrEmpty(x));
bool notEmptyDNS4 = Array.Exists(CurrentSilentConfig.Pinger.CustomDNSv4, (x => !string.IsNullOrEmpty(x)));
bool notEmptyDNS6 = Array.Exists(CurrentSilentConfig.Pinger.CustomDNSv6, (x => !string.IsNullOrEmpty(x)));

if (atLeastOnePrimary4 && atLeastOnePrimary6 && notEmptyDNS4 && notEmptyDNS6)
{
Expand Down
2 changes: 1 addition & 1 deletion Optimizer/UWPHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal static List<KeyValuePair<string, string>> GetUWPApps(bool showAll)
foreach (PSObject x in psResult)
{
tmp = x.ToString().Replace("@", string.Empty).Replace("{", string.Empty).Replace("}", string.Empty).Replace("Name=", string.Empty).Replace("InstallLocation=", string.Empty).Trim().Split(';');
if (!modernApps.Any(i => i.Key == tmp[0]))
if (!modernApps.Exists(i => i.Key == tmp[0]))
{
modernApps.Add(new KeyValuePair<string, string>(tmp[0], tmp[1]));
}
Expand Down
2 changes: 1 addition & 1 deletion Optimizer/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ internal static void ActivateMainForm()

internal static bool ServiceExists(string serviceName)
{
return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
return Array.Exists(ServiceController.GetServices(), (serviceController => serviceController.ServiceName.Equals(serviceName)));
}

internal static void StopService(string serviceName)
Expand Down

0 comments on commit 7757f45

Please sign in to comment.