Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Micro optimization EndsWith using #13084

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Avalonia.Base/Animation/Cue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static Cue Parse(string value, CultureInfo? culture)
{
string v = value;

if (value.EndsWith("%"))
if (value.Length > 0 && value[value.Length -1 ] == '%')
MrJul marked this conversation as resolved.
Show resolved Hide resolved
{
v = v.TrimEnd('%');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Avalonia.Base/Media/Fonts/FontFamilyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ private static bool IsContainsFile(Uri x, string filePattern, string fileExtensi
{
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(
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.Base/RelativePoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ public static RelativePoint Parse(string s)
var unit = RelativeUnit.Absolute;
var scale = 1.0;

if (x.EndsWith("%"))
if (x.Length > 0 && x[x.Length - 1] == '%')
{
if (!y.EndsWith("%"))
if (!(y.Length > 0 && y[y.Length - 1] == '%'))
{
throw new FormatException("If one coordinate is relative, both must be.");
}
Expand Down
7 changes: 5 additions & 2 deletions src/Avalonia.Build.Tasks/GenerateAvaloniaResourcesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ private bool PreProcessXamlFiles(List<Source> sources)

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
Expand All @@ -110,7 +113,7 @@ private bool PreProcessXamlFiles(List<Source> sources)
$"Duplicate x:Class directive, {info.XClass} is already used in {typeToXamlIndex[info.XClass]}");
return false;
}
typeToXamlIndex[info.XClass] = s.Path;
typeToXamlIndex[info.XClass] = path;
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public IEnumerable<GeneratedPartialClass> GenerateNameReferences(IEnumerable<Add
{
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CompositionRoslynGenerator : IIncrementalGenerator
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())));
Expand Down