Skip to content

Commit f000abe

Browse files
committed
Add option to set maximum thread count for compression (and uncompression)
Add option to lock HDD threads to 1 (minimises performance impact, reduces fragmentation, can improve speed due to reduction in seek times) All background compression is now limited to one thread. Update Version
1 parent 8ab872f commit f000abe

File tree

8 files changed

+86
-18
lines changed

8 files changed

+86
-18
lines changed

CompactGUI.Core/Compactor.vb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Imports System.Runtime.InteropServices
33
Imports System.Threading
44

5+
56
Public Class Compactor : Implements IDisposable
67

78
Public Sub New(folder As String, cLevel As CompressionAlgorithm, excludedFilesTypes As String())
@@ -33,21 +34,33 @@ Public Class Compactor : Implements IDisposable
3334

3435

3536

36-
Public Async Function RunCompactAsync(Optional progressMonitor As IProgress(Of (percentageProgress As Integer, currentFile As String)) = Nothing) As Task(Of Boolean)
37+
Public Async Function RunCompactAsync(Optional progressMonitor As IProgress(Of (percentageProgress As Integer, currentFile As String)) = Nothing, Optional MaxParallelism As Integer = 1) As Task(Of Boolean)
3738
If _cancellationTokenSource.IsCancellationRequested Then Return False
3839

3940
Dim FilesList = Await BuildWorkingFilesList()
4041
Dim totalFilesSize As Long = FilesList.Sum(Function(f) f.Item2)
4142
_processedFilesBytes = 0
4243

44+
If MaxParallelism <= 0 Then MaxParallelism = Environment.ProcessorCount
45+
46+
47+
Dim sw As New Stopwatch
48+
sw.Start()
49+
50+
Dim paraOptions As New ParallelOptions With {.MaxDegreeOfParallelism = MaxParallelism}
4351

44-
Await Parallel.ForEachAsync(FilesList,
52+
Await Parallel.ForEachAsync(FilesList, paraOptions,
4553
Function(file, _ctx) As ValueTask
4654
If _ctx.IsCancellationRequested Then Return ValueTask.FromCanceled(_ctx)
4755
Return New ValueTask(PauseAndProcessFile(file.Item1, _cancellationTokenSource.Token, file.Item2, totalFilesSize, progressMonitor))
4856
End Function).ConfigureAwait(False)
4957

5058

59+
sw.Stop()
60+
61+
Debug.WriteLine($"Completed in {sw.Elapsed.TotalSeconds} s")
62+
63+
5164
If _cancellationTokenSource.IsCancellationRequested Then Return False
5265

5366
Return True

CompactGUI.Core/Uncompactor.vb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,30 @@ Public Class Uncompactor
99
Private _cancellationTokenSource As New CancellationTokenSource
1010

1111

12-
Public Async Function UncompactFiles(filesList As List(Of String), Optional progressMonitor As IProgress(Of (percentageProgress As Integer, currentFile As String)) = Nothing) As Task(Of Boolean)
12+
Public Async Function UncompactFiles(filesList As List(Of String), Optional progressMonitor As IProgress(Of (percentageProgress As Integer, currentFile As String)) = Nothing, Optional MaxParallelism As Integer = 1) As Task(Of Boolean)
1313

1414
Dim totalFiles As Integer = filesList.Count
1515

16+
If MaxParallelism <= 0 Then MaxParallelism = Environment.ProcessorCount
17+
18+
Dim paraOptions As New ParallelOptions With {.MaxDegreeOfParallelism = MaxParallelism}
19+
20+
21+
Dim sw As New Stopwatch
22+
sw.Start()
23+
24+
1625
_processedFileCount.Clear()
17-
Await Parallel.ForEachAsync(filesList,
26+
Await Parallel.ForEachAsync(filesList, paraOptions,
1827
Function(file, _ctx) As ValueTask
1928
If _ctx.IsCancellationRequested Then Return ValueTask.FromCanceled(_ctx)
2029
Return New ValueTask(PauseAndProcessFile(file, _cancellationTokenSource.Token, totalFiles, progressMonitor))
2130
End Function).ConfigureAwait(False)
31+
32+
33+
sw.Stop()
34+
Debug.WriteLine($"Completed in {sw.Elapsed.TotalSeconds} s")
35+
2236
Return True
2337
End Function
2438

CompactGUI/CompactGUI.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</ItemGroup>
5454

5555
<ItemGroup>
56+
<PackageReference Include="diskdetector-net" Version="0.3.2" />
5657
<PackageReference Include="Fody" Version="6.8.1">
5758
<PrivateAssets>all</PrivateAssets>
5859
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

CompactGUI/Models/ActiveFolder.vb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Imports System.Collections.ObjectModel
33

44
Imports Microsoft.Toolkit.Mvvm.ComponentModel
5+
Imports DiskDetector
56

67
Public Class ActiveFolder : Inherits ObservableObject
78

@@ -19,6 +20,14 @@ Public Class ActiveFolder : Inherits ObservableObject
1920

2021
Public Property IsFreshlyCompressed As Boolean = False
2122

23+
Public ReadOnly Property DiskType As DiskDetector.Models.HardwareType
24+
Get
25+
If FolderName Is Nothing Then Return Models.HardwareType.Unknown
26+
Return DiskDetector.Detector.DetectDrive(FolderName.First, DiskDetector.Models.QueryType.RotationRate).HardwareType
27+
28+
End Get
29+
End Property
30+
2231
Public ReadOnly Property CompressionRatio As Decimal
2332
Get
2433
If UncompressedBytes = 0 OrElse CompressedBytes = 0 OrElse CompressedBytes = 1010101010101010 Then Return 0

CompactGUI/Models/Settings.vb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Public Class Settings : Inherits ObservableObject
1515
Public Property ShowNotifications As Boolean = False
1616
Public Property StartInSystemTray As Boolean = False
1717

18+
Public Property MaxCompressionThreads As Integer = 0
19+
Public Property LockHDDsToOneThread As Boolean = True
20+
1821
Private _EnableBackgroundWatcher As Boolean = True
1922
Public Property EnableBackgroundWatcher As Boolean
2023
Get

CompactGUI/Models/UpdateHandler.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Imports System.Text.Json
33
Public Class UpdateHandler
44

5-
Public Shared Property CurrentVersion As New SemVersion(3, 5, 1)
5+
Public Shared Property CurrentVersion As New SemVersion(3, 6, 0)
66
Public Shared Property NewVersion As SemVersion
77
Public Shared ReadOnly UpdateURL As String = "https://raw.githubusercontent.com/IridiumIO/CompactGUI/database/version.json"
88
Public Shared ReadOnly httpClient As New HttpClient()

CompactGUI/ViewModels/MainViewModel.vb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,13 @@ Public Class MainViewModel : Inherits ObservableObject
190190
PauseResumeStatus = "Pause"
191191
CancelStatus = "Cancel"
192192

193+
193194
CProgress.Report((0, ""))
194195

195196
Dim exclist As String() = GetSkipList()
196197

197198
CoreCompactor = New Core.Compactor(ActiveFolder.FolderName, Core.WOFConvertCompressionLevel(ActiveFolder.SelectedCompressionMode), exclist)
198-
Dim res = Await CoreCompactor.RunCompactAsync(CProgress)
199+
Dim res = Await CoreCompactor.RunCompactAsync(CProgress, If(ActiveFolder.DiskType = DiskDetector.Models.HardwareType.Hdd AndAlso SettingsHandler.AppSettings.LockHDDsToOneThread, 1, SettingsHandler.AppSettings.MaxCompressionThreads))
199200

200201
ActiveFolder.IsFreshlyCompressed = False
201202
If res Then ActiveFolder.IsFreshlyCompressed = True
@@ -236,7 +237,7 @@ Public Class MainViewModel : Inherits ObservableObject
236237

237238
Dim compressedFilesList = ActiveFolder.AnalysisResults.Where(Function(rs) rs.CompressedSize < rs.UncompressedSize).Select(Of String)(Function(f) f.FileName).ToList
238239
CoreUncompactor = New Core.Uncompactor
239-
Await CoreUncompactor.UncompactFiles(compressedFilesList, CProgress)
240+
Await CoreUncompactor.UncompactFiles(compressedFilesList, CProgress, If(ActiveFolder.DiskType = DiskDetector.Models.HardwareType.Hdd AndAlso SettingsHandler.AppSettings.LockHDDsToOneThread, 1, SettingsHandler.AppSettings.MaxCompressionThreads))
240241

241242
ActiveFolder.IsFreshlyCompressed = False
242243

CompactGUI/Views/SettingsView.xaml

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
<StackPanel Margin="0,40,0,0">
1919
<Label Content="Filetype Management" FontWeight="SemiBold" />
2020

21-
<StackPanel Margin="10,10,0,5">
22-
<Grid Margin="0,0,0,5">
21+
<StackPanel Margin="10,5,0,5">
22+
<Grid Margin="0,0,0,2">
2323
<TextBlock Text="manage local skipped filetypes"
2424
VerticalAlignment="Center"
2525
FontSize="14" />
@@ -77,26 +77,49 @@
7777
<Separator Margin="0,5,0,5" />
7878
<Label Content="System Integration" FontWeight="SemiBold" />
7979
<StackPanel Margin="10,5,0,7">
80-
<CheckBox x:Name="uiIsContextEnabled"
80+
<CheckBox x:Name="uiIsContextEnabled" Margin="0,-3"
8181
Content="add to right-click context menu"
8282
IsChecked="{Binding AppSettings.IsContextIntegrated, Mode=TwoWay}" />
83-
<CheckBox x:Name="uiIsStartMenuEnabled"
83+
<CheckBox x:Name="uiIsStartMenuEnabled" Margin="0,-3"
8484
Content="add to start menu"
8585
IsChecked="{Binding AppSettings.IsStartMenuEnabled, Mode=TwoWay}"
8686
/>
87-
<CheckBox x:Name="uiShowNotifications"
87+
<CheckBox x:Name="uiShowNotifications" Margin="0,-3"
8888
Content="show notification on completion"
8989
IsChecked="{Binding AppSettings.ShowNotifications, Mode=TwoWay}" />
90-
<CheckBox x:Name="uiAlwaysStartInTray"
90+
<CheckBox x:Name="uiAlwaysStartInTray" Margin="0,-3"
9191
Content="start CompactGUI in system tray"
9292
IsChecked="{Binding AppSettings.StartInSystemTray}" />
9393

9494
</StackPanel>
95+
<Separator Margin="0,5,0,5" />
96+
97+
<Label Content="Maximum Compression Threads" FontWeight="SemiBold" />
98+
99+
<StackPanel Orientation="Horizontal" Margin="15,0,15,0">
100+
101+
<Slider x:Name="compressionParallelism" Width="130"
102+
103+
IsSnapToTickEnabled="True" LargeChange="1" Maximum="16" Minimum="0"
104+
Orientation="Horizontal" SmallChange="1" TickFrequency="1"
105+
TickPlacement="BottomRight"
106+
Value="{Binding AppSettings.MaxCompressionThreads, Mode=TwoWay}">
107+
108+
</Slider>
109+
110+
<Label Margin="15,5" FontWeight="SemiBold" Content="{Binding AppSettings.MaxCompressionThreads, FallbackValue=0}"/>
111+
<CheckBox x:Name="uiLockHDDToOneThread" Margin="15,0"
112+
Content="HDDs only use 1 thread"
113+
IsChecked="{Binding AppSettings.LockHDDsToOneThread}" />
114+
</StackPanel>
115+
116+
117+
95118
<Separator Margin="0,5,0,5" />
96119

97120
<Label Content="Background Watcher Settings" FontWeight="SemiBold" />
98121
<StackPanel Margin="10,5,0,7">
99-
<CheckBox x:Name="uiEnableBackgroundWatcher"
122+
<CheckBox x:Name="uiEnableBackgroundWatcher" Margin="0,-3"
100123
Content="monitor compressed folders for changes"
101124
IsChecked="{Binding AppSettings.EnableBackgroundWatcher, Mode=TwoWay}">
102125

@@ -106,9 +129,9 @@
106129
</i:EventTrigger>
107130
</i:Interaction.Triggers>
108131
</CheckBox>
109-
<CheckBox x:Name="uiEnableBackgroundAutoCompression"
132+
<CheckBox x:Name="uiEnableBackgroundAutoCompression"
110133
Content="periodically check and keep folders compressed"
111-
Margin="30,0,0,0"
134+
Margin="30,-3,0,-3"
112135
IsChecked="{Binding AppSettings.EnableBackgroundAutoCompression, Mode=TwoWay}">
113136

114137
<i:Interaction.Triggers>
@@ -119,11 +142,13 @@
119142
</CheckBox>
120143

121144
</StackPanel>
145+
146+
122147
<Separator Margin="0,5,0,5" />
123148

124149
<Label Content="Updates" FontWeight="SemiBold" />
125150
<StackPanel Margin="10,5,0,7">
126-
<CheckBox x:Name="uiEnablePreReleaseUpdates"
151+
<CheckBox x:Name="uiEnablePreReleaseUpdates" Margin="0,-3"
127152
Content="Check for pre-release updates"
128153
IsChecked="{Binding AppSettings.EnablePreReleaseUpdates}" />
129154

@@ -134,7 +159,7 @@
134159
<Label Content="UI Scaling" FontWeight="SemiBold" />
135160

136161
<Slider x:Name="uiScalingFactor"
137-
Margin="15,5,15,20"
162+
Margin="15,0,15,0"
138163
IsSnapToTickEnabled="True" LargeChange="0.125" Maximum="1.5" Minimum="0.5"
139164
Orientation="Horizontal" SmallChange="0.125" TickFrequency="0.125"
140165
TickPlacement="BottomRight"
@@ -149,7 +174,9 @@
149174
</i:Interaction.Triggers>
150175
</Slider>
151176

177+
152178
<Separator Margin="0,0,0,30" />
179+
153180
<Grid HorizontalAlignment="Center">
154181
<Grid.ColumnDefinitions>
155182
<ColumnDefinition Width="*" />

0 commit comments

Comments
 (0)