Skip to content

Commit e5574d2

Browse files
committed
perf: minor perf optimization for FlyleafLib
- Avoid boxing in Set method of NotifyPropertyChanged - Use IEquatable<T> for struct implemantion - Prevents unnecessary string conversion in Language string parsing
1 parent bbdedf1 commit e5574d2

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

FlyleafLib/Engine/Globals.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public enum VideoFilters
133133
StereoAdjustment = 0x80
134134
}
135135

136-
public struct AspectRatio
136+
public struct AspectRatio : IEquatable<AspectRatio>
137137
{
138138
public static readonly AspectRatio Keep = new(-1, 1);
139139
public static readonly AspectRatio Fill = new(-2, 1);
@@ -173,12 +173,12 @@ public AspectRatio(float value) : this(value, 1) { }
173173
public AspectRatio(float num, float den) { Num = num; Den = den; }
174174
public AspectRatio(string value) { Num = Invalid.Num; Den = Invalid.Den; FromString(value); }
175175

176-
public override bool Equals(object obj) => (obj == null) || !GetType().Equals(obj.GetType()) ? false : Num == ((AspectRatio)obj).Num && Den == ((AspectRatio)obj).Den;
176+
public bool Equals(AspectRatio other) => Num == other.Num && Den == other.Den;
177+
public override bool Equals(object obj) => obj is AspectRatio o && Equals(o);
178+
public override int GetHashCode() => HashCode.Combine(Num, Den);
177179
public static bool operator ==(AspectRatio a, AspectRatio b) => a.Equals(b);
178180
public static bool operator !=(AspectRatio a, AspectRatio b) => !(a == b);
179181

180-
public override int GetHashCode() => (int)(Value * 1000);
181-
182182
public void FromString(string value)
183183
{
184184
if (value == "Keep")
@@ -230,7 +230,7 @@ protected bool Set<T>(ref T field, T value, bool check = true, [CallerMemberName
230230
{
231231
//Utils.Log($"[===| {propertyName} |===] | Set | {IsUI()}");
232232

233-
if (!check || (field == null && value != null) || (field != null && !field.Equals(value)))
233+
if (!check || !EqualityComparer<T>.Default.Equals(field, value))
234234
{
235235
field = value;
236236

@@ -247,7 +247,7 @@ protected bool SetUI<T>(ref T field, T value, bool check = true, [CallerMemberNa
247247
{
248248
//Utils.Log($"[===| {propertyName} |===] | SetUI | {IsUI()}");
249249

250-
if (!check || (field == null && value != null) || (field != null && !field.Equals(value)))
250+
if (!check || !EqualityComparer<T>.Default.Equals(field, value))
251251
{
252252
field = value;
253253

FlyleafLib/Engine/Language.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static Language Get(string name)
126126

127127
public static CultureInfo StringToCulture(string lang)
128128
{
129-
if (string.IsNullOrWhiteSpace(lang) || lang.Length < 2)
129+
if (string.IsNullOrWhiteSpace(lang) || lang.Length < 2 || lang == "und")
130130
return null;
131131

132132
string langLower = lang.ToLower();
@@ -137,10 +137,12 @@ public static CultureInfo StringToCulture(string lang)
137137
ret = lang.Length == 3 ? ThreeLetterToCulture(langLower) : CultureInfo.GetCultureInfo(langLower);
138138
} catch { }
139139

140+
StringComparer cmp = StringComparer.OrdinalIgnoreCase;
141+
140142
// TBR: Check also -Country/region two letters?
141143
if (ret == null || ret.ThreeLetterISOLanguageName == "")
142144
foreach (var cult in CultureInfo.GetCultures(CultureTypes.AllCultures))
143-
if (cult.Name.ToLower() == langLower || cult.NativeName.ToLower() == langLower || cult.EnglishName.ToLower() == langLower)
145+
if (cmp.Equals(cult.Name, langLower) || cmp.Equals(cult.NativeName, langLower) || cmp.Equals(cult.EnglishName, langLower))
144146
return cult;
145147

146148
return ret;

0 commit comments

Comments
 (0)