Skip to content

Commit

Permalink
since .NET 9, some Linq methods (FirstOrDefault/Any) became faster
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-carvalho committed Nov 6, 2024
1 parent a0e418e commit b01ed43
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/Ryujinx.Graphics.Gpu/Image/TextureCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Ryujinx.Memory.Range;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Ryujinx.Graphics.Gpu.Image
Expand Down Expand Up @@ -998,7 +999,7 @@ public Texture FindOrCreateTexture(
{
bool dataOverlaps = texture.DataOverlaps(overlap, compatibility);

if (!overlap.IsView && dataOverlaps && !incompatibleOverlaps.Exists(incompatible => incompatible.Group == overlap.Group))
if (!overlap.IsView && dataOverlaps && !incompatibleOverlaps.Any(incompatible => incompatible.Group == overlap.Group))
{
incompatibleOverlaps.Add(new TextureIncompatibleOverlap(overlap.Group, compatibility));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Ryujinx.Graphics.Gpu/Image/TextureGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Ryujinx.Memory.Tracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Ryujinx.Graphics.Gpu.Image
Expand Down Expand Up @@ -1555,7 +1556,7 @@ public void CreateCopyDependency(TextureGroup other, bool copyTo)
/// <param name="copy">True if the overlap should register copy dependencies</param>
public void RegisterIncompatibleOverlap(TextureIncompatibleOverlap other, bool copy)
{
if (!_incompatibleOverlaps.Exists(overlap => overlap.Group == other.Group))
if (!_incompatibleOverlaps.Any(overlap => overlap.Group == other.Group))
{
if (copy && other.Compatibility == TextureViewCompatibility.LayoutIncompatible)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Ryujinx.Graphics.Gpu/Image/TextureGroupHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Ryujinx.Memory.Tracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Ryujinx.Graphics.Gpu.Image
Expand Down Expand Up @@ -553,7 +554,7 @@ public void RemoveDependency(TextureDependency dependency)
/// <returns>True if at least one of the handles is dirty</returns>
private bool CheckDirty()
{
return Array.Exists(Handles, handle => handle.Dirty);
return Handles.Any(handle => handle.Dirty);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Ryujinx.Graphics.Vulkan/ShaderCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private async Task BackgroundCompilation()
{
await Task.WhenAll(_shaders.Select(shader => shader.CompileTask));

if (Array.Exists(_shaders, shader => shader.CompileStatus == ProgramLinkStatus.Failure))
if (_shaders.Any(shader => shader.CompileStatus == ProgramLinkStatus.Failure))
{
LinkStatus = ProgramLinkStatus.Failure;

Expand Down
16 changes: 8 additions & 8 deletions src/Ryujinx.HLE/FileSystem/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ private void RemoveLocationEntry(ulong titleId, NcaContentType contentType, Stor
if (locationList != null)
{
LocationEntry entry =
locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType);
locationList.ToList().FirstOrDefault(x => x.TitleId == titleId && x.ContentType == contentType);

if (entry.ContentPath != null)
{
Expand Down Expand Up @@ -423,7 +423,7 @@ private LocationEntry GetLocation(ulong titleId, NcaContentType contentType, Sto
{
LinkedList<LocationEntry> locationList = _locationEntries[storageId];

return locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType);
return locationList.ToList().FirstOrDefault(x => x.TitleId == titleId && x.ContentType == contentType);
}

public void InstallFirmware(string firmwareSource)
Expand Down Expand Up @@ -650,7 +650,7 @@ SystemVersion VerifyAndGetVersionZip(ZipArchive archive)

if (updateNcas.TryGetValue(SystemUpdateTitleId, out var ncaEntry))
{
string metaPath = ncaEntry.Find(x => x.type == NcaContentType.Meta).path;
string metaPath = ncaEntry.FirstOrDefault(x => x.type == NcaContentType.Meta).path;

CnmtContentMetaEntry[] metaEntries = null;

Expand Down Expand Up @@ -686,7 +686,7 @@ SystemVersion VerifyAndGetVersionZip(ZipArchive archive)

if (updateNcas.TryGetValue(SystemVersionTitleId, out var updateNcasItem))
{
string versionEntry = updateNcasItem.Find(x => x.type != NcaContentType.Meta).path;
string versionEntry = updateNcasItem.FirstOrDefault(x => x.type != NcaContentType.Meta).path;

using Stream ncaStream = GetZipStream(archive.GetEntry(versionEntry));
Nca nca = new(_virtualFileSystem.KeySet, ncaStream.AsStorage());
Expand All @@ -705,9 +705,9 @@ SystemVersion VerifyAndGetVersionZip(ZipArchive archive)
{
if (updateNcas.TryGetValue(metaEntry.TitleId, out ncaEntry))
{
metaPath = ncaEntry.Find(x => x.type == NcaContentType.Meta).path;
metaPath = ncaEntry.FirstOrDefault(x => x.type == NcaContentType.Meta).path;

string contentPath = ncaEntry.Find(x => x.type != NcaContentType.Meta).path;
string contentPath = ncaEntry.FirstOrDefault(x => x.type != NcaContentType.Meta).path;

// Nintendo in 9.0.0, removed PPC and only kept the meta nca of it.
// This is a perfect valid case, so we should just ignore the missing content nca and continue.
Expand Down Expand Up @@ -846,8 +846,8 @@ SystemVersion VerifyAndGetVersion(IFileSystem filesystem)
{
if (updateNcas.TryGetValue(metaEntry.TitleId, out var ncaEntry))
{
string metaNcaPath = ncaEntry.Find(x => x.type == NcaContentType.Meta).path;
string contentPath = ncaEntry.Find(x => x.type != NcaContentType.Meta).path;
string metaNcaPath = ncaEntry.FirstOrDefault(x => x.type == NcaContentType.Meta).path;
string contentPath = ncaEntry.FirstOrDefault(x => x.type != NcaContentType.Meta).path;

// Nintendo in 9.0.0, removed PPC and only kept the meta nca of it.
// This is a perfect valid case, so we should just ignore the missing content nca and continue.
Expand Down
2 changes: 1 addition & 1 deletion src/Ryujinx.HLE/HOS/Kernel/Threading/KAddressArbiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void SignalProcessWideKey(ulong address, int count)

WakeThreads(_condVarThreads, count, TryAcquireMutex, x => x.CondVarAddress == address);

if (!_condVarThreads.Exists(x => x.CondVarAddress == address))
if (!_condVarThreads.Any(x => x.CondVarAddress == address))
{
KernelTransfer.KernelToUser(address, 0);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Ryujinx.HLE/HOS/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ private static void AddModsFromDirectory(ModCache mods, DirectoryInfo dir, ModMe

if (StrEquals(RomfsDir, modDir.Name))
{
var modData = modMetadata.Mods.Find(x => modDir.FullName.Contains(x.Path));
var modData = modMetadata.Mods.FirstOrDefault(x => modDir.FullName.Contains(x.Path));
var enabled = modData?.Enabled ?? true;

mods.RomfsDirs.Add(mod = new Mod<DirectoryInfo>(dir.Name, modDir, enabled));
types.Append('R');
}
else if (StrEquals(ExefsDir, modDir.Name))
{
var modData = modMetadata.Mods.Find(x => modDir.FullName.Contains(x.Path));
var modData = modMetadata.Mods.FirstOrDefault(x => modDir.FullName.Contains(x.Path));
var enabled = modData?.Enabled ?? true;

mods.ExefsDirs.Add(mod = new Mod<DirectoryInfo>(dir.Name, modDir, enabled));
Expand Down Expand Up @@ -280,7 +280,7 @@ private static void QueryApplicationDir(ModCache mods, DirectoryInfo application
var fsFile = new FileInfo(Path.Combine(applicationDir.FullName, RomfsContainer));
if (fsFile.Exists)
{
var modData = modMetadata.Mods.Find(x => fsFile.FullName.Contains(x.Path));
var modData = modMetadata.Mods.FirstOrDefault(x => fsFile.FullName.Contains(x.Path));
var enabled = modData == null || modData.Enabled;

mods.RomfsContainers.Add(new Mod<FileInfo>($"<{applicationDir.Name} RomFs>", fsFile, enabled));
Expand All @@ -289,7 +289,7 @@ private static void QueryApplicationDir(ModCache mods, DirectoryInfo application
fsFile = new FileInfo(Path.Combine(applicationDir.FullName, ExefsContainer));
if (fsFile.Exists)
{
var modData = modMetadata.Mods.Find(x => fsFile.FullName.Contains(x.Path));
var modData = modMetadata.Mods.FirstOrDefault(x => fsFile.FullName.Contains(x.Path));
var enabled = modData == null || modData.Enabled;

mods.ExefsContainers.Add(new Mod<FileInfo>($"<{applicationDir.Name} ExeFs>", fsFile, enabled));
Expand Down
7 changes: 4 additions & 3 deletions src/Ryujinx.HLE/HOS/Services/Nfc/Nfp/VirtualAmiibo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp
{
Expand Down Expand Up @@ -94,7 +95,7 @@ public static bool OpenApplicationArea(string amiiboId, uint applicationAreaId)
{
VirtualAmiiboFile virtualAmiiboFile = LoadAmiiboFile(amiiboId);

if (virtualAmiiboFile.ApplicationAreas.Exists(item => item.ApplicationAreaId == applicationAreaId))
if (virtualAmiiboFile.ApplicationAreas.Any(item => item.ApplicationAreaId == applicationAreaId))
{
_openedApplicationAreaId = applicationAreaId;

Expand Down Expand Up @@ -123,7 +124,7 @@ public static bool CreateApplicationArea(string amiiboId, uint applicationAreaId
{
VirtualAmiiboFile virtualAmiiboFile = LoadAmiiboFile(amiiboId);

if (virtualAmiiboFile.ApplicationAreas.Exists(item => item.ApplicationAreaId == applicationAreaId))
if (virtualAmiiboFile.ApplicationAreas.Any(item => item.ApplicationAreaId == applicationAreaId))
{
return false;
}
Expand All @@ -143,7 +144,7 @@ public static void SetApplicationArea(string amiiboId, byte[] applicationAreaDat
{
VirtualAmiiboFile virtualAmiiboFile = LoadAmiiboFile(amiiboId);

if (virtualAmiiboFile.ApplicationAreas.Exists(item => item.ApplicationAreaId == _openedApplicationAreaId))
if (virtualAmiiboFile.ApplicationAreas.Any(item => item.ApplicationAreaId == _openedApplicationAreaId))
{
for (int i = 0; i < virtualAmiiboFile.ApplicationAreas.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static ProcessResult Load(this IFileSystem exeFs, Switch device, BlitStru

if (string.IsNullOrWhiteSpace(programName))
{
programName = Array.Find(nacpData.Value.Title.ItemsRo.ToArray(), x => x.Name[0] != 0).NameString.ToString();
programName = nacpData.Value.Title.ItemsRo.ToArray().FirstOrDefault(x => x.Name[0] != 0).NameString.ToString();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Ryujinx.HLE/Loaders/Processes/ProcessLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Concurrent;
using System.IO;
using Path = System.IO.Path;
using System.Linq;

namespace Ryujinx.HLE.Loaders.Processes
{
Expand Down Expand Up @@ -177,7 +178,7 @@ public bool LoadNxo(string path)

if (string.IsNullOrWhiteSpace(programName))
{
programName = Array.Find(nacpData.Value.Title.ItemsRo.ToArray(), x => x.Name[0] != 0).NameString.ToString();
programName = nacpData.Value.Title.ItemsRo.ToArray().FirstOrDefault(x => x.Name[0] != 0).NameString.ToString();
}

if (nacpData.Value.PresenceGroupId != 0)
Expand Down
7 changes: 4 additions & 3 deletions src/Ryujinx.HLE/Loaders/Processes/ProcessResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Ryujinx.HLE.Loaders.Processes.Extensions;
using Ryujinx.Horizon.Common;
using System;
using System.Linq;

namespace Ryujinx.HLE.Loaders.Processes
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public ProcessResult(

if (string.IsNullOrWhiteSpace(Name))
{
Name = Array.Find(ApplicationControlProperties.Title.ItemsRo.ToArray(), x => x.Name[0] != 0).NameString.ToString();
Name = ApplicationControlProperties.Title.ItemsRo.ToArray().FirstOrDefault(x => x.Name[0] != 0).NameString.ToString();
}

DisplayVersion = ApplicationControlProperties.DisplayVersionString.ToString();
Expand All @@ -85,8 +86,8 @@ public bool Start(Switch device)
}

// TODO: LibHac npdm currently doesn't support version field.
string version = ProgramId > 0x0100000000007FFF
? DisplayVersion
string version = ProgramId > 0x0100000000007FFF
? DisplayVersion
: device.System.ContentManager.GetCurrentFirmwareVersion()?.VersionString ?? "?";

Logger.Info?.Print(LogClass.Loader, $"Application Loaded: {Name} v{version} [{ProgramIdText}] [{(Is64Bit ? "64-bit" : "32-bit")}]");
Expand Down
2 changes: 1 addition & 1 deletion src/Ryujinx.Input/HLE/NpadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ internal InputConfig GetPlayerInputConfigByIndex(int index)
{
lock (_lock)
{
return _inputConfig.Find(x => x.PlayerIndex == (Common.Configuration.Hid.PlayerIndex)index);
return _inputConfig.FirstOrDefault(x => x.PlayerIndex == (Common.Configuration.Hid.PlayerIndex)index);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Ryujinx/UI/ViewModels/AmiiboWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private void ParseAmiiboData()

private void SelectLastScannedAmiibo()
{
AmiiboApi scanned = _amiiboList.Find(amiibo => amiibo.GetId() == LastScannedAmiiboId);
AmiiboApi scanned = _amiiboList.FirstOrDefault(amiibo => amiibo.GetId() == LastScannedAmiiboId);

SeriesSelectedIndex = AmiiboSeries.IndexOf(scanned.AmiiboSeries);
AmiiboSelectedIndex = AmiiboList.IndexOf(scanned);
Expand Down Expand Up @@ -393,7 +393,7 @@ private void SetAmiiboDetails()

AmiiboApi selected = _amiibos[_amiiboSelectedIndex];

string imageUrl = _amiiboList.Find(amiibo => amiibo.Equals(selected)).Image;
string imageUrl = _amiiboList.FirstOrDefault(amiibo => amiibo.Equals(selected)).Image;

StringBuilder usageStringBuilder = new();

Expand Down
8 changes: 4 additions & 4 deletions src/Ryujinx/UI/ViewModels/Input/InputViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public InputViewModel()

private void LoadConfiguration(InputConfig inputConfig = null)
{
Config = inputConfig ?? ConfigurationState.Instance.Hid.InputConfig.Value.Find(inputConfig => inputConfig.PlayerIndex == _playerId);
Config = inputConfig ?? ConfigurationState.Instance.Hid.InputConfig.Value.FirstOrDefault(inputConfig => inputConfig.PlayerIndex == _playerId);

if (Config is StandardKeyboardInputConfig keyboardInputConfig)
{
Expand Down Expand Up @@ -586,7 +586,7 @@ public InputConfig LoadDefaultConfiguration()
}
else if (activeDevice.Type == DeviceType.Controller)
{
bool isNintendoStyle = Devices.ToList().Find(x => x.Id == activeDevice.Id).Name.Contains("Nintendo");
bool isNintendoStyle = Devices.ToList().FirstOrDefault(x => x.Id == activeDevice.Id).Name.Contains("Nintendo");

string id = activeDevice.Id.Split(" ")[0];

Expand Down Expand Up @@ -812,11 +812,11 @@ public void Save()

newConfig.AddRange(ConfigurationState.Instance.Hid.InputConfig.Value);

newConfig.Remove(newConfig.Find(x => x == null));
newConfig.Remove(newConfig.FirstOrDefault(x => x == null));

if (Device == 0)
{
newConfig.Remove(newConfig.Find(x => x.PlayerIndex == this.PlayerId));
newConfig.Remove(newConfig.FirstOrDefault(x => x.PlayerIndex == this.PlayerId));
}
else
{
Expand Down
17 changes: 9 additions & 8 deletions src/Ryujinx/UI/Windows/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;

namespace Ryujinx.Ava.UI.Windows
{
Expand Down Expand Up @@ -317,7 +318,7 @@ private async Task CheckLaunchState()

if (_launchApplicationId != null)
{
applicationData = applications.Find(application => application.IdString == _launchApplicationId);
applicationData = applications.FirstOrDefault(application => application.IdString == _launchApplicationId);

if (applicationData != null)
{
Expand Down Expand Up @@ -471,7 +472,7 @@ protected override void OnOpened(EventArgs e)
{
LoadApplications();
}

_ = CheckLaunchState();
}

Expand All @@ -494,8 +495,8 @@ private void SetMainContent(Control content = null)
public static void UpdateGraphicsConfig()
{
#pragma warning disable IDE0055 // Disable formatting
GraphicsConfig.ResScale = ConfigurationState.Instance.Graphics.ResScale == -1
? ConfigurationState.Instance.Graphics.ResScaleCustom
GraphicsConfig.ResScale = ConfigurationState.Instance.Graphics.ResScale == -1
? ConfigurationState.Instance.Graphics.ResScaleCustom
: ConfigurationState.Instance.Graphics.ResScale;
GraphicsConfig.MaxAnisotropy = ConfigurationState.Instance.Graphics.MaxAnisotropy;
GraphicsConfig.ShadersDumpPath = ConfigurationState.Instance.Graphics.ShadersDumpPath;
Expand Down Expand Up @@ -666,10 +667,10 @@ private void ShowNewContentAddedDialog(int numDlcAdded, int numDlcRemoved, int n
await ContentDialogHelper.ShowTextDialog(
LocaleManager.Instance[LocaleKeys.DialogConfirmationTitle],
msg,
string.Empty,
string.Empty,
string.Empty,
LocaleManager.Instance[LocaleKeys.InputDialogOk],
string.Empty,
string.Empty,
string.Empty,
LocaleManager.Instance[LocaleKeys.InputDialogOk],
(int)Symbol.Checkmark);
});
}
Expand Down

0 comments on commit b01ed43

Please sign in to comment.