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

Improve the performance of the RtxtParser. #9839

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
28 changes: 17 additions & 11 deletions src/Xamarin.Android.Build.Tasks/Utilities/RtxtParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R> {
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")}"))} }}";
}
Expand Down Expand Up @@ -104,10 +104,13 @@ public IEnumerable<R> Parse (string file, TaskLoggingHelper logger, Dictionary<s
return result;
}

void ProcessRtxtFile (string file, IList<R> result)
void ProcessRtxtFile (string file, List<R> 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) {
Comment on lines +109 to +113
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The part about this one, File.ReadAllLines() will allocate a string[] as large as the file. This can be faster for small files and worse for large files.

What are the results if you undid this change and just took a "guess" of the result.Capacity to some number like 1024, for example? (Or choose a common size)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll switch to just reading the file size that should do

lineNumber++;
var items = line.Split (EmptyChar, 4);
if (items.Length < 4) {
Expand All @@ -119,11 +122,13 @@ void ProcessRtxtFile (string file, IList<R> 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,
});
Expand All @@ -133,6 +138,7 @@ void ProcessRtxtFile (string file, IList<R> result)
case "int":
result.Add (new R () {
ResourceTypeName = items [1],
Type = type,
Identifier = itemName,
Id = Convert.ToInt32 (items [3].Trim (), 10),
});
Expand Down
Loading