Browse Source
perf: Micro optimization EndsWith using (#13084)
* perf: Micro optimization EndsWith using
* fix: Address reviews
* fix: Address reviews
---------
Co-authored-by: Max Katz <maxkatz6@outlook.com>
pull/13106/head
workgroupengineering
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with
36 additions and
23 deletions
-
Avalonia.sln
-
src/Avalonia.Base/Animation/Cue.cs
-
src/Avalonia.Base/Media/Color.cs
-
src/Avalonia.Base/Media/Fonts/FontFamilyLoader.cs
-
src/Avalonia.Base/Media/HslColor.cs
-
src/Avalonia.Base/RelativePoint.cs
-
src/Avalonia.Build.Tasks/Avalonia.Build.Tasks.csproj
-
src/Avalonia.Build.Tasks/GenerateAvaloniaResourcesTask.cs
-
src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs
-
src/Shared/StringCompatibilityExtensions.cs
-
src/tools/Avalonia.Generators/NameGenerator/AvaloniaNameGenerator.cs
-
src/tools/DevGenerators/CompositionGenerator/CompositionRoslynGenerator.cs
|
|
|
@ -42,7 +42,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A689DE |
|
|
|
src\Shared\ModuleInitializer.cs = src\Shared\ModuleInitializer.cs |
|
|
|
src\Shared\NullableAttributes.cs = src\Shared\NullableAttributes.cs |
|
|
|
src\Shared\SourceGeneratorAttributes.cs = src\Shared\SourceGeneratorAttributes.cs |
|
|
|
src\Avalonia.Base\Compatibility\StringCompatibilityExtensions.cs = src\Avalonia.Base\Compatibility\StringCompatibilityExtensions.cs |
|
|
|
src\Shared\StringCompatibilityExtensions.cs = src\Shared\StringCompatibilityExtensions.cs |
|
|
|
EndProjectSection |
|
|
|
EndProject |
|
|
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.ReactiveUI", "src\Avalonia.ReactiveUI\Avalonia.ReactiveUI.csproj", "{6417B24E-49C2-4985-8DB2-3AB9D898EC91}" |
|
|
|
|
|
|
|
@ -34,7 +34,7 @@ namespace Avalonia.Animation |
|
|
|
{ |
|
|
|
string v = value; |
|
|
|
|
|
|
|
if (value.EndsWith("%")) |
|
|
|
if (value.EndsWith('%')) |
|
|
|
{ |
|
|
|
v = v.TrimEnd('%'); |
|
|
|
} |
|
|
|
|
|
|
|
@ -346,7 +346,7 @@ namespace Avalonia.Media |
|
|
|
|
|
|
|
if (workingString.Length >= 11 && |
|
|
|
workingString.StartsWith("rgba(", StringComparison.OrdinalIgnoreCase) && |
|
|
|
workingString.EndsWith(")", StringComparison.Ordinal)) |
|
|
|
workingString.EndsWith(')')) |
|
|
|
{ |
|
|
|
workingString = workingString.Substring(5, workingString.Length - 6); |
|
|
|
prefixMatched = true; |
|
|
|
@ -355,7 +355,7 @@ namespace Avalonia.Media |
|
|
|
if (prefixMatched == false && |
|
|
|
workingString.Length >= 10 && |
|
|
|
workingString.StartsWith("rgb(", StringComparison.OrdinalIgnoreCase) && |
|
|
|
workingString.EndsWith(")", StringComparison.Ordinal)) |
|
|
|
workingString.EndsWith(')')) |
|
|
|
{ |
|
|
|
workingString = workingString.Substring(4, workingString.Length - 5); |
|
|
|
prefixMatched = true; |
|
|
|
|
|
|
|
@ -94,14 +94,14 @@ namespace Avalonia.Media.Fonts |
|
|
|
{ |
|
|
|
var path = x.GetUnescapeAbsolutePath(); |
|
|
|
return path.IndexOf(filePattern, StringComparison.Ordinal) >= 0 |
|
|
|
&& path.EndsWith(fileExtension, StringComparison.Ordinal); |
|
|
|
&& path.EndsWith(fileExtension, StringComparison.OrdinalIgnoreCase); |
|
|
|
} |
|
|
|
|
|
|
|
private static bool IsFontTtfOrOtf(Uri uri) |
|
|
|
{ |
|
|
|
var sourceWithoutArguments = GetSubString(uri.OriginalString, '?'); |
|
|
|
return sourceWithoutArguments.EndsWith(".ttf", StringComparison.Ordinal) |
|
|
|
|| sourceWithoutArguments.EndsWith(".otf", StringComparison.Ordinal); |
|
|
|
return sourceWithoutArguments.EndsWith(".ttf", StringComparison.OrdinalIgnoreCase) |
|
|
|
|| sourceWithoutArguments.EndsWith(".otf", StringComparison.OrdinalIgnoreCase); |
|
|
|
} |
|
|
|
|
|
|
|
private static (string fileNameWithoutExtension, string extension) GetFileNameAndExtension( |
|
|
|
|
|
|
|
@ -256,7 +256,7 @@ namespace Avalonia.Media |
|
|
|
|
|
|
|
if (workingString.Length >= 11 && |
|
|
|
workingString.StartsWith("hsla(", StringComparison.OrdinalIgnoreCase) && |
|
|
|
workingString.EndsWith(")", StringComparison.Ordinal)) |
|
|
|
workingString.EndsWith(')')) |
|
|
|
{ |
|
|
|
workingString = workingString.Substring(5, workingString.Length - 6); |
|
|
|
prefixMatched = true; |
|
|
|
@ -265,7 +265,7 @@ namespace Avalonia.Media |
|
|
|
if (prefixMatched == false && |
|
|
|
workingString.Length >= 10 && |
|
|
|
workingString.StartsWith("hsl(", StringComparison.OrdinalIgnoreCase) && |
|
|
|
workingString.EndsWith(")", StringComparison.Ordinal)) |
|
|
|
workingString.EndsWith(')')) |
|
|
|
{ |
|
|
|
workingString = workingString.Substring(4, workingString.Length - 5); |
|
|
|
prefixMatched = true; |
|
|
|
|
|
|
|
@ -164,9 +164,9 @@ namespace Avalonia |
|
|
|
var unit = RelativeUnit.Absolute; |
|
|
|
var scale = 1.0; |
|
|
|
|
|
|
|
if (x.EndsWith("%")) |
|
|
|
if (x.EndsWith('%')) |
|
|
|
{ |
|
|
|
if (!y.EndsWith("%")) |
|
|
|
if (!y.EndsWith('%')) |
|
|
|
{ |
|
|
|
throw new FormatException("If one coordinate is relative, both must be."); |
|
|
|
} |
|
|
|
|
|
|
|
@ -118,6 +118,7 @@ |
|
|
|
<Compile Include="..\Avalonia.Base\Metadata\NullableAttributes.cs" Link="NullableAttributes.cs" /> |
|
|
|
<Compile Include="..\Avalonia.Base\Compatibility\TrimmingAttributes.cs" Link="TrimmingAttributes.cs" Visible="False" /> |
|
|
|
<Compile Include="..\Avalonia.Base\Utilities\SpanHelpers.cs" Link="Utilities\SpanHelpers.cs" /> |
|
|
|
<Compile Include="..\Shared\StringCompatibilityExtensions.cs" Link="Compatibility\StringCompatibilityExtensions.cs" /> |
|
|
|
<Compile Remove="../Markup/Avalonia.Markup.Xaml.Loader\xamlil.github\**\obj\**\*.cs" /> |
|
|
|
<Compile Remove="../Markup/Avalonia.Markup.Xaml.Loader\xamlil.github\src\XamlX\IL\SreTypeSystem.cs" /> |
|
|
|
<PackageReference Include="Mono.Cecil" Version="0.11.5" /> |
|
|
|
|
|
|
|
@ -88,7 +88,10 @@ namespace Avalonia.Build.Tasks |
|
|
|
|
|
|
|
foreach (var s in sources.ToArray()) |
|
|
|
{ |
|
|
|
if (s.Path.ToLowerInvariant().EndsWith(".xaml") || s.Path.ToLowerInvariant().EndsWith(".paml") || s.Path.ToLowerInvariant().EndsWith(".axaml")) |
|
|
|
var path = s.Path; |
|
|
|
if (path.EndsWith(".axaml", StringComparison.OrdinalIgnoreCase) |
|
|
|
|| path.EndsWith(".xaml", StringComparison.OrdinalIgnoreCase) |
|
|
|
|| path.EndsWith(".paml", StringComparison.OrdinalIgnoreCase) ) |
|
|
|
{ |
|
|
|
XamlFileInfo info; |
|
|
|
try |
|
|
|
@ -110,7 +113,7 @@ namespace Avalonia.Build.Tasks |
|
|
|
$"Duplicate x:Class directive, {info.XClass} is already used in {typeToXamlIndex[info.XClass]}"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
typeToXamlIndex[info.XClass] = s.Path; |
|
|
|
typeToXamlIndex[info.XClass] = path; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -23,11 +23,15 @@ namespace Avalonia.Build.Tasks |
|
|
|
public static partial class XamlCompilerTaskExecutor |
|
|
|
{ |
|
|
|
private const string CompiledAvaloniaXamlNamespace = "CompiledAvaloniaXaml"; |
|
|
|
|
|
|
|
static bool CheckXamlName(IResource r) => r.Name.ToLowerInvariant().EndsWith(".xaml") |
|
|
|
|| r.Name.ToLowerInvariant().EndsWith(".paml") |
|
|
|
|| r.Name.ToLowerInvariant().EndsWith(".axaml"); |
|
|
|
|
|
|
|
|
|
|
|
static bool CheckXamlName(IResource r) |
|
|
|
{ |
|
|
|
var name = r.Name; |
|
|
|
return name.EndsWith(".axaml", StringComparison.OrdinalIgnoreCase) |
|
|
|
|| name.EndsWith(".xaml",StringComparison.OrdinalIgnoreCase) |
|
|
|
|| name.EndsWith(".paml", StringComparison.OrdinalIgnoreCase); |
|
|
|
} |
|
|
|
|
|
|
|
public class CompileResult |
|
|
|
{ |
|
|
|
public bool Success { get; set; } |
|
|
|
|
|
|
|
@ -8,5 +8,9 @@ internal static class StringCompatibilityExtensions |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static bool Contains(this string str, char search) => |
|
|
|
str.Contains(search.ToString()); |
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static bool EndsWith(this string str, char search) => |
|
|
|
str.Length > 0 && str[str.Length - 1] == search; |
|
|
|
} |
|
|
|
#endif
|
|
|
|
|
|
|
|
@ -36,10 +36,11 @@ internal class AvaloniaNameGenerator : INameGenerator |
|
|
|
{ |
|
|
|
var resolveViews = |
|
|
|
from file in additionalFiles |
|
|
|
where (file.Path.EndsWith(".xaml") || |
|
|
|
file.Path.EndsWith(".paml") || |
|
|
|
file.Path.EndsWith(".axaml")) && |
|
|
|
_pathPattern.Matches(file.Path) |
|
|
|
let filePath = file.Path |
|
|
|
where (filePath.EndsWith(".xaml", StringComparison.OrdinalIgnoreCase) || |
|
|
|
filePath.EndsWith(".paml", StringComparison.OrdinalIgnoreCase) || |
|
|
|
filePath.EndsWith(".axaml", StringComparison.OrdinalIgnoreCase)) && |
|
|
|
_pathPattern.Matches(filePath) |
|
|
|
let xaml = file.GetText(cancellationToken)?.ToString() |
|
|
|
where xaml != null |
|
|
|
let view = _classes.ResolveView(xaml) |
|
|
|
|
|
|
|
@ -10,7 +10,7 @@ namespace Avalonia.SourceGenerator.CompositionGenerator |
|
|
|
public void Initialize(IncrementalGeneratorInitializationContext context) |
|
|
|
{ |
|
|
|
var schema = |
|
|
|
context.AdditionalTextsProvider.Where(static file => file.Path.EndsWith("composition-schema.xml")); |
|
|
|
context.AdditionalTextsProvider.Where(static file => file.Path.EndsWith("composition-schema.xml", System.StringComparison.OrdinalIgnoreCase)); |
|
|
|
var configs = schema.Select((t, _) => t.GetText()) |
|
|
|
.Where(source => source is not null) |
|
|
|
.Select((source, _) => (GConfig)new XmlSerializer(typeof(GConfig)).Deserialize(new StringReader(source!.ToString()))); |
|
|
|
|