From 6962a2a79eb7fa94775e5285a7cffc95491fd035 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Tue, 25 Feb 2025 13:05:45 +0000 Subject: [PATCH] Improve the performance of the RtxtParser. There are some small improvements in the RtxtParser that can be done to improve the performance of the parser. This commit includes the following changes: 1. Update the `enum` to be `byte`. This saves us a few bytes per instance. 2. Reorder the fields to reduce padding. 3. Encode is a type is a stylable into the `RType` enum. This allows us to reduce the number of `if` statements in the parser. --- .../Utilities/RtxtParser.cs | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/RtxtParser.cs b/src/Xamarin.Android.Build.Tasks/Utilities/RtxtParser.cs index d2d1f6aa42b..151db3ae919 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/RtxtParser.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/RtxtParser.cs @@ -5,35 +5,35 @@ using System.Text.RegularExpressions; using Microsoft.Android.Build.Tasks; using Microsoft.Build.Utilities; +using System.Runtime.InteropServices; namespace Xamarin.Android.Tasks { - public enum RType { + public enum RType : byte { Integer, + Integer_Styleable, Array, } - public enum ResourceType { + public enum ResourceType : byte { System, Custom, } + [StructLayout(LayoutKind.Sequential)] public struct R : IComparable { - public RType Type; public int Id; + public RType Type; + public ResourceType ResourceType; public int [] Ids; public string Identifier; public string ResourceTypeName; - public ResourceType ResourceType; - public string Key => $"{ResourceTypeName}:{Identifier}"; public override string ToString () { - if (Type == RType.Integer) { - if (ResourceTypeName == "styleable") - return $"int {ResourceTypeName} {Identifier} {Id}"; - return $"int {ResourceTypeName} {Identifier} 0x{Id.ToString ("x8")}"; + if (Type != RType.Array) { + return $"int {ResourceTypeName} {Identifier} { (Type == RType.Integer ? $"0x{Id.ToString ("x8")}" : "{Id}")}"; } return $"int[] {ResourceTypeName} {Identifier} {{ {String.Join (", ", Ids.Select (x => $"0x{x.ToString ("x8")}"))} }}"; } @@ -104,10 +104,13 @@ public IEnumerable Parse (string file, TaskLoggingHelper logger, Dictionary result) + void ProcessRtxtFile (string file, List result) { + var lines = File.ReadAllLines (file); + result.Capacity = lines.Length; + int lineNumber = 0; - foreach (var line in File.ReadLines (file)) { + foreach (var line in lines) { lineNumber++; var items = line.Split (EmptyChar, 4); if (items.Length < 4) { @@ -119,11 +122,13 @@ void ProcessRtxtFile (string file, IList result) continue; } int value = items [1] != "styleable" ? Convert.ToInt32 (items [3].Trim (), 16) : -1; + var type = items [1] != "styleable" ? RType.Integer : RType.Integer_Styleable; string itemName = ResourceIdentifier.GetResourceName(ResourceParser.GetNestedTypeName (items [1]), items [2], map, log); if (knownTypes.Contains (items [1])) { if (items [1] != "styleable") { result.Add (new R () { ResourceTypeName = items [1], + Type =type, Identifier = itemName, Id = value, }); @@ -133,6 +138,7 @@ void ProcessRtxtFile (string file, IList result) case "int": result.Add (new R () { ResourceTypeName = items [1], + Type = type, Identifier = itemName, Id = Convert.ToInt32 (items [3].Trim (), 10), });