[Refactor] Optimize LINQ queries.

This commit is contained in:
2025-11-24 11:45:16 +00:00
parent 46066fb9a7
commit 7af8b40f80
11 changed files with 35 additions and 37 deletions

View File

@@ -525,8 +525,8 @@ sealed partial class Dump
}
if(!outputOptical.OpticalCapabilities.HasFlag(OpticalImageCapabilities.CanStorePregaps) &&
tracks.Where(track => track.Sequence != tracks.First(t => t.Session == track.Session).Sequence)
.Any(static track => track.Pregap > 0))
tracks.Any(track => track.Sequence != tracks.First(t => t.Session == track.Session).Sequence &&
track.Pregap > 0))
{
if(!_force)
{
@@ -1084,8 +1084,9 @@ sealed partial class Dump
foreach(int sub in _resume.BadSubchannels) subchannelExtents.Add(sub);
if(_resume.NextBlock < blocks)
for(ulong i = _resume.NextBlock; i < blocks; i++)
subchannelExtents.Add((int)i);
{
for(ulong i = _resume.NextBlock; i < blocks; i++) subchannelExtents.Add((int)i);
}
}
if(_resume.NextBlock > 0)
@@ -1511,8 +1512,9 @@ sealed partial class Dump
supportsLongSectors);
foreach(Tuple<ulong, ulong> leadoutExtent in leadOutExtents.ToArray())
for(ulong e = leadoutExtent.Item1; e <= leadoutExtent.Item2; e++)
subchannelExtents.Remove((int)e);
{
for(ulong e = leadoutExtent.Item1; e <= leadoutExtent.Item2; e++) subchannelExtents.Remove((int)e);
}
if(subchannelExtents.Count > 0 && _retryPasses > 0 && _retrySubchannel)
{

View File

@@ -191,8 +191,8 @@ partial class Dump
if(oldToc.HasValue)
{
foreach(TOC.CDTOCTrackDataDescriptor trk in oldToc.Value.TrackDescriptors
.OrderBy(static t => t.TrackNumber)
.Where(static trk => trk.ADR is 1 or 4))
.Where(static trk => trk.ADR is 1 or 4)
.OrderBy(static t => t.TrackNumber))
{
switch(trk.TrackNumber)
{

View File

@@ -156,8 +156,7 @@ public sealed class Entropy
EndProgress2Event?.Invoke();
trackEntropy.Entropy += entTable.Select(l => l / (double)trackSize)
.Select(static frequency => -(frequency * Math.Log(frequency, 2)))
.Sum();
.Sum(static frequency => -(frequency * Math.Log(frequency, 2)));
if(duplicatedSectors) trackEntropy.UniqueSectors = uniqueSectorsPerTrack.Count;
@@ -230,8 +229,7 @@ public sealed class Entropy
EndProgressEvent?.Invoke();
entropy.Entropy += entTable.Select(l => l / (double)diskSize)
.Select(static frequency => -(frequency * Math.Log(frequency, 2)))
.Sum();
.Sum(static frequency => -(frequency * Math.Log(frequency, 2)));
if(duplicatedSectors) entropy.UniqueSectors = uniqueSectors.Count;
@@ -277,8 +275,7 @@ public sealed class Entropy
EndProgressEvent?.Invoke();
entropy.Entropy += entTable.Select(l => l / (double)data.Length)
.Select(static frequency => -(frequency * Math.Log(frequency, 2)))
.Sum();
.Sum(static frequency => -(frequency * Math.Log(frequency, 2)));
return entropy;
}

View File

@@ -167,7 +167,8 @@ public static class Partitions
// Be sure that device partitions are not excluded if not mapped by any scheme...
if(tapeImage is not null)
{
var startLocations = childPartitions.Select(static detectedPartition => detectedPartition.Start).ToList();
List<ulong> startLocations =
childPartitions.ConvertAll(static detectedPartition => detectedPartition.Start);
if(tapeImage.Files != null)
{
@@ -183,7 +184,8 @@ public static class Partitions
if(partitionableImage is not null)
{
var startLocations = childPartitions.Select(static detectedPartition => detectedPartition.Start).ToList();
List<ulong> startLocations =
childPartitions.ConvertAll(static detectedPartition => detectedPartition.Start);
if(partitionableImage.Partitions != null)
{

View File

@@ -31,7 +31,7 @@
// ****************************************************************************/
using System;
using System.Linq;
using System.Collections.Generic;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;
@@ -53,7 +53,7 @@ public sealed partial class PascalPlugin
if(!string.IsNullOrEmpty(path) && string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
return ErrorNumber.NotSupported;
var contents = _fileEntries.Select(ent => StringHandlers.PascalToString(ent.Filename, _encoding)).ToList();
List<string> contents = _fileEntries.ConvertAll(ent => StringHandlers.PascalToString(ent.Filename, _encoding));
if(_debug)
{

View File

@@ -157,5 +157,5 @@ public sealed partial class A2R
static bool IsCaptureTypeTiming(ulong resolution, byte[] buffer) =>
// TODO: This is only accurate for 300rpm
buffer.Select(static x => (int)x).Sum() * (long)resolution is > 230000000000 and < 270000000000;
buffer.Sum(static x => x) * (long)resolution is > 230000000000 and < 270000000000;
}

View File

@@ -501,9 +501,8 @@ public sealed partial class Cdrdao
}
if(_writingTracks != null && _writingStreams != null)
{
foreach(FileStream oldTrack in _writingStreams.Select(static t => t.Value).Distinct()) oldTrack.Close();
}
foreach(FileStream oldTrack in _writingStreams.Select(static t => t.Value).Distinct())
oldTrack.Close();
ulong currentOffset = 0;
_writingTracks = [];
@@ -650,8 +649,8 @@ public sealed partial class Cdrdao
(ulong)(track.RawBytesPerSector +
(track.SubchannelType != TrackSubchannelType.None ? 96 : 0)));
foreach(KeyValuePair<ushort, int> index in track.Indexes.OrderBy(static i => i.Key)
.Where(static i => i.Key > 1))
foreach(KeyValuePair<ushort, int> index in track.Indexes.Where(static i => i.Key > 1)
.OrderBy(static i => i.Key))
{
msf = LbaToMsf((ulong)index.Value - (track.Pregap + track.StartSector));

View File

@@ -612,8 +612,8 @@ public sealed partial class CloneCd
if(trackIndexes.TryGetValue((byte)tmpTrack.Sequence, out Dictionary<byte, int> indexes))
{
foreach((byte index, int value) in indexes.OrderBy(static i => i.Key)
.Where(static trackIndex => trackIndex.Key > 1))
foreach((byte index, int value) in indexes.Where(static trackIndex => trackIndex.Key > 1)
.OrderBy(static i => i.Key))
// Untested as of 20210711
tmpTrack.Indexes[index] = value;

View File

@@ -253,7 +253,7 @@ public abstract class OpticalImageConvertIssueTest
foreach(SectorTagType tag in inputFormat.Info.ReadableSectorTags
.Where(static t => t == SectorTagType.CdTrackIsrc)
.OrderBy(static t => t))
.Order())
{
foreach(Track track in tracks)
{
@@ -267,7 +267,7 @@ public abstract class OpticalImageConvertIssueTest
foreach(SectorTagType tag in inputFormat.Info.ReadableSectorTags
.Where(static t => t == SectorTagType.CdTrackFlags)
.OrderBy(static t => t))
.Order())
{
foreach(Track track in tracks)
{
@@ -286,7 +286,7 @@ public abstract class OpticalImageConvertIssueTest
subchannelExtents.Add((int)s);
}
foreach(SectorTagType tag in inputFormat.Info.ReadableSectorTags.OrderBy(static t => t).TakeWhile(_ => UseLong))
foreach(SectorTagType tag in inputFormat.Info.ReadableSectorTags.Order().TakeWhile(_ => UseLong))
{
switch(tag)
{

View File

@@ -342,7 +342,7 @@ public abstract class WritableOpticalMediaImageTest : BaseWritableMediaImageTest
foreach(SectorTagType tag in inputFormat.Info.ReadableSectorTags
.Where(static t => t == SectorTagType.CdTrackIsrc)
.OrderBy(static t => t))
.Order())
{
foreach(Track track in tracks)
{
@@ -356,7 +356,7 @@ public abstract class WritableOpticalMediaImageTest : BaseWritableMediaImageTest
foreach(SectorTagType tag in inputFormat.Info.ReadableSectorTags
.Where(static t => t == SectorTagType.CdTrackFlags)
.OrderBy(static t => t))
.Order())
{
foreach(Track track in tracks)
{
@@ -375,8 +375,7 @@ public abstract class WritableOpticalMediaImageTest : BaseWritableMediaImageTest
subchannelExtents.Add((int)s);
}
foreach(SectorTagType tag in inputFormat.Info.ReadableSectorTags.OrderBy(static t => t)
.TakeWhile(_ => useLong))
foreach(SectorTagType tag in inputFormat.Info.ReadableSectorTags.Order().TakeWhile(_ => useLong))
{
switch(tag)
{

View File

@@ -580,7 +580,7 @@ sealed class ConvertImageCommand : Command<ConvertImageCommand.Settings>
foreach(SectorTagType tag in inputOptical.Info.ReadableSectorTags
.Where(static t => t == SectorTagType.CdTrackIsrc)
.OrderBy(static t => t))
.Order())
{
foreach(Track track in tracks)
{
@@ -594,7 +594,7 @@ sealed class ConvertImageCommand : Command<ConvertImageCommand.Settings>
foreach(SectorTagType tag in inputOptical.Info.ReadableSectorTags
.Where(static t => t == SectorTagType.CdTrackFlags)
.OrderBy(static t => t))
.Order())
{
foreach(Track track in tracks)
{
@@ -613,8 +613,7 @@ sealed class ConvertImageCommand : Command<ConvertImageCommand.Settings>
subchannelExtents.Add((int)s);
}
foreach(SectorTagType tag in inputOptical.Info.ReadableSectorTags.OrderBy(static t => t)
.TakeWhile(_ => useLong))
foreach(SectorTagType tag in inputOptical.Info.ReadableSectorTags.Order().TakeWhile(_ => useLong))
{
switch(tag)
{