Skip to content

Commit b7b5857

Browse files
committed
Minor tweaks to analyser performance
1 parent c030848 commit b7b5857

File tree

5 files changed

+26
-20
lines changed

5 files changed

+26
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*.user
1010
*.userosscache
1111
*.sln.docstates
12+
CompactGUI.TestingGround/
1213

1314
# User-specific files (MonoDevelop/Xamarin Studio)
1415
*.userprefs

CompactGUI.Core/Analyser.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ public Analyser(string folder)
3131
.AsParallel()
3232
.WithCancellation(cancellationToken)
3333
.Select(AnalyseFile)
34-
.Where(details => details != null)
35-
.Cast<AnalysedFileDetails>()
36-
.ToList<AnalysedFileDetails>();
34+
.OfType<AnalysedFileDetails>()
35+
.ToList();
3736

3837
CompressedBytes = fileDetails.Sum(f => f.CompressedSize);
3938
UncompressedBytes = fileDetails.Sum(f => f.UncompressedSize);

CompactGUI.Core/CompactGUI.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PrivateAssets>all</PrivateAssets>
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
15+
<PackageReference Include="ZLinq" Version="1.4.10" />
1516
</ItemGroup>
1617

1718
</Project>

CompactGUI.Core/SharedMethods.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ static bool IsOneDriveFolder(string folderPath)
9494
public static void PreventSleep()
9595
{
9696
PInvoke.SetThreadExecutionState(
97-
EXECUTION_STATE.ES_CONTINUOUS |
98-
EXECUTION_STATE.ES_SYSTEM_REQUIRED |
97+
EXECUTION_STATE.ES_CONTINUOUS |
98+
EXECUTION_STATE.ES_SYSTEM_REQUIRED |
9999
EXECUTION_STATE.ES_DISPLAY_REQUIRED);
100100
}
101101

@@ -116,31 +116,35 @@ public static void RestoreSleep()
116116

117117

118118

119-
public static List<string> AsShortPathNames(this IEnumerable<string> filesList)
119+
public static IEnumerable<string> AsShortPathNames(this IEnumerable<string> filesList)
120120
{
121-
return filesList
122-
.Select(file => (file.Length >= 255 ? GetShortPath(file) ?? file : file))
123-
.ToList();
121+
return filesList.Select(file => (file.Length >= 255 ? GetShortPath(file) ?? file : file));
124122
}
125123

126124

127-
private static string? GetShortPath(string filePath)
125+
private static string GetShortPath(string filePath)
128126
{
129-
if (string.IsNullOrWhiteSpace(filePath)) return null;
127+
const string LongPathPrefix = @"\\?\";
128+
ReadOnlySpan<char> longPathPrefixSpan = LongPathPrefix;
130129

131-
bool addPrefix = filePath.Length >= 255 && !filePath.StartsWith(@"\\?\");
132-
if (addPrefix) filePath = @"\\?\" + filePath;
130+
if (string.IsNullOrWhiteSpace(filePath)) return filePath;
131+
ReadOnlySpan<char> filePathSpan = filePath;
133132

134-
Span<char> shortPath = stackalloc char[1024];
135-
uint res = PInvoke.GetShortPathName(filePath, shortPath);
136-
if (res == 0) return null;
133+
bool addPrefix = filePathSpan.Length >= 255 && !filePathSpan.StartsWith(longPathPrefixSpan, StringComparison.Ordinal);
134+
string pathToUse = addPrefix ? LongPathPrefix + filePath : filePath;
137135

138-
var result = new string(shortPath.Slice(0, (int)res));
139-
return addPrefix && result.StartsWith(@"\\?\") ? result[4..] : result;
136+
Span<char> shortPath = stackalloc char[1024];
137+
uint res = PInvoke.GetShortPathName(pathToUse, shortPath);
138+
if (res == 0) return filePath;
140139

140+
ReadOnlySpan<char> resultSpan = shortPath[..(int)res];
141+
return addPrefix && resultSpan.StartsWith(longPathPrefixSpan, StringComparison.Ordinal)
142+
? resultSpan[longPathPrefixSpan.Length..].ToString()
143+
: resultSpan.ToString();
141144
}
142145

143146

147+
144148
public static unsafe uint GetClusterSize(string folderPath)
145149
{
146150
UInt32 lpSectorsPerCluster;
@@ -163,7 +167,7 @@ public static unsafe long GetFileSizeOnDisk(string file)
163167
{
164168
uint highOrder;
165169
uint lowOrder = PInvoke.GetCompressedFileSize(file, &highOrder);
166-
if (lowOrder == 0xFFFFFFFF && (Marshal.GetLastWin32Error() != 0)) return -1;
170+
if (lowOrder == 0xFFFFFFFF && (Marshal.GetLastWin32Error() != 0)) return -1;
167171
return ((long)highOrder << 32) | lowOrder;
168172
}
169173

@@ -204,7 +208,7 @@ public static bool HasDirectoryWritePermission(string folderName)
204208
return writeAllowed && !writeDenied;
205209

206210
}
207-
catch (UnauthorizedAccessException) { return false;}
211+
catch (UnauthorizedAccessException) { return false; }
208212
}
209213

210214

CompactGUI.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</Configurations>
99
<Project Path="CompactGUI.Core/CompactGUI.Core.csproj" />
1010
<Project Path="CompactGUI.CoreVB/CompactGUI.CoreVB.vbproj" />
11+
<Project Path="CompactGUI.TestingGround/CompactGUI.TestingGround.csproj" Id="279814db-5112-4c5f-b013-df80d73bcf37" />
1112
<Project Path="CompactGUI.Watcher/CompactGUI.Watcher.vbproj">
1213
<BuildDependency Project="CompactGUI.CoreVB/CompactGUI.CoreVB.vbproj" />
1314
</Project>

0 commit comments

Comments
 (0)