Skip to content

Commit 488164d

Browse files
Minor optimizations and cleaning
1 parent 7fa112e commit 488164d

File tree

12 files changed

+150
-173
lines changed

12 files changed

+150
-173
lines changed

StabilityMatrix.Avalonia/Controls/VendorLabs/Cache/InMemoryStorage.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Collections.Concurrent;
7-
using System.Collections.Generic;
8-
using System.Collections.Immutable;
9-
using System.Linq;
105
using System.Runtime.CompilerServices;
11-
using KGySoft.CoreLibraries;
12-
using StabilityMatrix.Core.Helper.Cache;
136

147
namespace StabilityMatrix.Avalonia.Controls.VendorLabs.Cache;
158

@@ -23,7 +16,7 @@ public class InMemoryStorage<T>
2316
private readonly LinkedList<InMemoryStorageItem<T>> _lruList = [];
2417

2518
private int _maxItemCount;
26-
private object _settingMaxItemCountLocker = new();
19+
private readonly Lock _settingMaxItemCountLocker = new();
2720

2821
/// <summary>
2922
/// Gets or sets the maximum count of Items that can be stored in this InMemoryStorage instance.

StabilityMatrix.Avalonia/Helpers/EnumHelpers.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using StabilityMatrix.Core.Extensions;
1+
using StabilityMatrix.Core.Extensions;
52
using StabilityMatrix.Core.Models;
63
using StabilityMatrix.Core.Models.Api;
74

@@ -10,22 +7,22 @@ namespace StabilityMatrix.Avalonia.Helpers;
107
public static class EnumHelpers
118
{
129
public static IEnumerable<CivitPeriod> AllCivitPeriods { get; } =
13-
Enum.GetValues(typeof(CivitPeriod)).Cast<CivitPeriod>();
10+
Enum.GetValues<CivitPeriod>().Cast<CivitPeriod>();
1411

1512
public static IEnumerable<CivitSortMode> AllSortModes { get; } =
16-
Enum.GetValues(typeof(CivitSortMode)).Cast<CivitSortMode>();
13+
Enum.GetValues<CivitSortMode>().Cast<CivitSortMode>();
1714

1815
public static IEnumerable<CivitModelType> AllCivitModelTypes { get; } =
19-
Enum.GetValues(typeof(CivitModelType))
16+
Enum.GetValues<CivitModelType>()
2017
.Cast<CivitModelType>()
2118
.Where(t => t == CivitModelType.All || t.ConvertTo<SharedFolderType>() > 0)
2219
.OrderBy(t => t.ToString());
2320

2421
public static IEnumerable<CivitModelType> MetadataEditorCivitModelTypes { get; } =
25-
Enum.GetValues(typeof(CivitModelType)).Cast<CivitModelType>().OrderBy(t => t.ToString());
22+
Enum.GetValues<CivitModelType>().Cast<CivitModelType>().OrderBy(t => t.ToString());
2623

2724
public static IEnumerable<CivitBaseModelType> AllCivitBaseModelTypes { get; } =
28-
Enum.GetValues(typeof(CivitBaseModelType)).Cast<CivitBaseModelType>();
25+
Enum.GetValues<CivitBaseModelType>().Cast<CivitBaseModelType>();
2926

3027
public static IEnumerable<CivitBaseModelType> MetadataEditorCivitBaseModelTypes { get; } =
3128
AllCivitBaseModelTypes.Where(x => x != CivitBaseModelType.All);

StabilityMatrix.Avalonia/Helpers/IOCommands.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using CommunityToolkit.Mvvm.Input;
1+
using CommunityToolkit.Mvvm.Input;
32
using StabilityMatrix.Core.Processes;
43

54
namespace StabilityMatrix.Avalonia.Helpers;

StabilityMatrix.Avalonia/Helpers/ImageProcessor.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using SkiaSharp;
1+
using SkiaSharp;
52
using StabilityMatrix.Core.Extensions;
63

74
namespace StabilityMatrix.Avalonia.Helpers;
@@ -45,9 +42,7 @@ public static SKImage CreateImageGrid(IReadOnlyList<SKImage> images, int spacing
4542
// Draw images
4643
using var canvas = new SKCanvas(output);
4744

48-
foreach (
49-
var (row, column) in Enumerable.Range(0, rows).Product(Enumerable.Range(0, columns))
50-
)
45+
foreach (var (row, column) in Enumerable.Range(0, rows).Product(Enumerable.Range(0, columns)))
5146
{
5247
// Stop if we have drawn all images
5348
var index = row * columns + column;

StabilityMatrix.Avalonia/Helpers/ImageSearcher.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using FuzzySharp;
1+
using FuzzySharp;
32
using FuzzySharp.PreProcess;
43
using StabilityMatrix.Core.Models.Database;
54

@@ -27,8 +26,7 @@ public Func<LocalImageFile, bool> GetPredicate(string? searchQuery)
2726

2827
if (
2928
SearchOptions.HasFlag(ImageSearchOptions.FileName)
30-
&& Fuzz.WeightedRatio(searchQuery, file.FileName, PreprocessMode.Full)
31-
> MinimumFuzzScore
29+
&& Fuzz.WeightedRatio(searchQuery, file.FileName, PreprocessMode.Full) > MinimumFuzzScore
3230
)
3331
{
3432
return true;
@@ -53,22 +51,18 @@ public Func<LocalImageFile, bool> GetPredicate(string? searchQuery)
5351
) ?? false
5452
)
5553
|| SearchOptions.HasFlag(ImageSearchOptions.Seed)
56-
&& parameters.Seed
57-
.ToString()
54+
&& parameters
55+
.Seed.ToString()
5856
.StartsWith(searchQuery, StringComparison.OrdinalIgnoreCase)
5957
|| SearchOptions.HasFlag(ImageSearchOptions.Sampler)
6058
&& (
61-
parameters.Sampler?.StartsWith(
62-
searchQuery,
63-
StringComparison.OrdinalIgnoreCase
64-
) ?? false
59+
parameters.Sampler?.StartsWith(searchQuery, StringComparison.OrdinalIgnoreCase)
60+
?? false
6561
)
6662
|| SearchOptions.HasFlag(ImageSearchOptions.ModelName)
6763
&& (
68-
parameters.ModelName?.StartsWith(
69-
searchQuery,
70-
StringComparison.OrdinalIgnoreCase
71-
) ?? false
64+
parameters.ModelName?.StartsWith(searchQuery, StringComparison.OrdinalIgnoreCase)
65+
?? false
7266
)
7367
)
7468
{
@@ -90,6 +84,6 @@ public enum ImageSearchOptions
9084
Seed = 1 << 3,
9185
Sampler = 1 << 4,
9286
ModelName = 1 << 5,
93-
All = int.MaxValue
87+
All = int.MaxValue,
9488
}
9589
}

StabilityMatrix.Avalonia/Helpers/PngDataHelper.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Diagnostics;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Text;
1+
using System.Text;
62
using System.Text.Json;
73
using Force.Crc32;
84
using StabilityMatrix.Avalonia.Models;

StabilityMatrix.Avalonia/Models/TagCompletion/CompletionProvider.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
1+
using System.Diagnostics;
52
using System.Text.RegularExpressions;
6-
using System.Threading.Tasks;
73
using AsyncAwaitBestPractices;
84
using AutoComplete.Builders;
95
using AutoComplete.Clients.IndexSearchers;
@@ -358,12 +354,12 @@ private IEnumerable<ICompletionData> GetCompletionNetworkTypes(string searchTerm
358354
{
359355
(PromptExtraNetworkType.Lora, "lora"),
360356
(PromptExtraNetworkType.LyCORIS, "lyco"),
361-
(PromptExtraNetworkType.Embedding, "embedding")
357+
(PromptExtraNetworkType.Embedding, "embedding"),
362358
};
363359

364360
return availableTypes
365-
.Where(
366-
type => type.Item1.GetStringValue().StartsWith(searchTerm, StringComparison.OrdinalIgnoreCase)
361+
.Where(type =>
362+
type.Item1.GetStringValue().StartsWith(searchTerm, StringComparison.OrdinalIgnoreCase)
367363
)
368364
.Select(type => new ModelTypeCompletionData(type.Item2, type.Item1));
369365
}
@@ -381,7 +377,7 @@ private IEnumerable<ICompletionData> GetCompletionTags(string searchTerm, int it
381377
{
382378
Term = searchTerm,
383379
MaxItemCount = itemsCount,
384-
SuggestWhenFoundStartsWith = suggest
380+
SuggestWhenFoundStartsWith = suggest,
385381
};
386382

387383
var result = searcher.Search(searchOptions);

StabilityMatrix.Avalonia/Services/RunningPackageService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ IPyRunner pyRunner
4646
)
4747
{
4848
// Get lock
49-
using var @lock = await packageLocks.LockAsync(installedPackage.Id, cancellationToken);
49+
using var _ = await packageLocks.LockAsync(installedPackage.Id, cancellationToken);
5050

5151
// Ignore if already running after lock
5252
if (RunningPackages.ContainsKey(installedPackage.Id))
@@ -266,7 +266,7 @@ await basePackage.RunPackage(
266266
public async Task StopPackage(Guid id, CancellationToken cancellationToken = default)
267267
{
268268
// Get lock
269-
using var @lock = await packageLocks.LockAsync(id, cancellationToken);
269+
using var _ = await packageLocks.LockAsync(id, cancellationToken);
270270

271271
// Ignore if not running after lock
272272
if (!RunningPackages.TryGetValue(id, out var vm))

StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CivitDetailsPageViewModel.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.ObjectModel;
1+
using System.Collections.Frozen;
2+
using System.Collections.ObjectModel;
23
using System.ComponentModel;
34
using System.ComponentModel.DataAnnotations;
45
using System.Globalization;
@@ -15,7 +16,6 @@
1516
using FluentAvalonia.UI.Controls;
1617
using Injectio.Attributes;
1718
using Microsoft.Extensions.Logging;
18-
using Refit;
1919
using StabilityMatrix.Avalonia.Animations;
2020
using StabilityMatrix.Avalonia.Languages;
2121
using StabilityMatrix.Avalonia.Models;
@@ -65,7 +65,7 @@ IModelImportService modelImportService
6565
[NotifyPropertyChangedFor(nameof(CanGoNext), nameof(CanGoPrevious))]
6666
public required partial int CurrentIndex { get; set; }
6767

68-
private List<string> ignoredFileNameFormatVars =
68+
private readonly FrozenSet<string> ignoredFileNameFormatVars =
6969
[
7070
"seed",
7171
"prompt",
@@ -86,17 +86,17 @@ IModelImportService modelImportService
8686
.Substitutions.Where(kv => !ignoredFileNameFormatVars.Contains(kv.Key))
8787
.Select(kv => new FileNameFormatVar { Variable = $"{{{kv.Key}}}", Example = kv.Value.Invoke() });
8888

89-
private SourceCache<CivitImage, string> imageCache = new(x => x.Url);
89+
private readonly SourceCache<CivitImage, string> imageCache = new(x => x.Url);
9090

9191
public IObservableCollection<ImageSource> ImageSources { get; set; } =
9292
new ObservableCollectionExtended<ImageSource>();
9393

94-
private SourceCache<CivitModelVersion, int> modelVersionCache = new(x => x.Id);
94+
private readonly SourceCache<CivitModelVersion, int> modelVersionCache = new(x => x.Id);
9595

9696
public IObservableCollection<ModelVersionViewModel> ModelVersions { get; set; } =
9797
new ObservableCollectionExtended<ModelVersionViewModel>();
9898

99-
private SourceCache<CivitFile, int> civitFileCache = new(x => x.Id);
99+
private readonly SourceCache<CivitFile, int> civitFileCache = new(x => x.Id);
100100

101101
public IObservableCollection<CivitFileViewModel> CivitFiles { get; set; } =
102102
new ObservableCollectionExtended<CivitFileViewModel>();
@@ -670,12 +670,15 @@ private async Task DeleteModelVersion(CivitModelVersion modelVersion)
670670
if (file is not { Type: CivitFileType.Model, Hashes.BLAKE3: not null })
671671
continue;
672672

673-
var matchingModels = (await modelIndexService.FindByHashAsync(file.Hashes.BLAKE3)).ToList();
673+
List<LocalModelFile> matchingModels =
674+
[
675+
.. (await modelIndexService.FindByHashAsync(file.Hashes.BLAKE3)),
676+
];
674677

675678
if (matchingModels.Count == 0)
676679
{
677680
await modelIndexService.RefreshIndex();
678-
matchingModels = (await modelIndexService.FindByHashAsync(file.Hashes.BLAKE3)).ToList();
681+
matchingModels = [.. (await modelIndexService.FindByHashAsync(file.Hashes.BLAKE3))];
679682
}
680683

681684
if (matchingModels.Count == 0)

StabilityMatrix.Avalonia/ViewModels/ConsoleViewModel.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.ComponentModel;
1+
using System.ComponentModel;
32
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
73
using System.Threading.Tasks.Dataflow;
84
using System.Web;
95
using Avalonia.Threading;

0 commit comments

Comments
 (0)