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

[XABT] Use SortedSets in JCWGenerator.EnsureIdenticalCollections for better performance. #9208

Merged
merged 1 commit into from
Aug 16, 2024
Merged
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
[XABT] Use SortedSets in JCWGenerator.EnsureIdenticalCollections fo…
…r better performance.
  • Loading branch information
jpobst committed Aug 16, 2024
commit fa0af372276236afe81a5f4b2a24ebbbce0aaa51
37 changes: 9 additions & 28 deletions src/Xamarin.Android.Build.Tasks/Utilities/JCWGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,39 +245,20 @@ static void EnsureIdenticalCollections (TaskLoggingHelper logger, NativeCodeGenS
{
logger.LogDebugMessage ($"Ensuring Java type collection in architecture '{state.TargetArch}' matches the one in architecture '{templateState.TargetArch}'");

List<TypeDefinition> templateTypes = templateState.AllJavaTypes;
List<TypeDefinition> types = state.AllJavaTypes;
var templateSet = new SortedSet<string> (templateState.AllJavaTypes.Select (t => t.FullName), StringComparer.Ordinal);
var typesSet = new SortedSet<string> (state.AllJavaTypes.Select (t => t.FullName), StringComparer.Ordinal);
Comment on lines +248 to +249
Copy link
Member

Choose a reason for hiding this comment

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

One trick, I've been doing to ensure there are no closures:

Suggested change
var templateSet = new SortedSet<string> (templateState.AllJavaTypes.Select (t => t.FullName), StringComparer.Ordinal);
var typesSet = new SortedSet<string> (state.AllJavaTypes.Select (t => t.FullName), StringComparer.Ordinal);
var templateSet = new SortedSet<string> (templateState.AllJavaTypes.Select (static t => t.FullName), StringComparer.Ordinal);
var typesSet = new SortedSet<string> (state.AllJavaTypes.Select (static t => t.FullName), StringComparer.Ordinal);

This wouldn't compile if you captured a variable on accident.

These are so short, though, it's fine as-is.


if (types.Count != templateTypes.Count) {
throw new InvalidOperationException ($"Internal error: architecture '{state.TargetArch}' has a different number of types ({types.Count}) than the template architecture '{templateState.TargetArch}' ({templateTypes.Count})");
if (typesSet.Count != templateSet.Count) {
throw new InvalidOperationException ($"Internal error: architecture '{state.TargetArch}' has a different number of types ({typesSet.Count}) than the template architecture '{templateState.TargetArch}' ({templateSet.Count})");
}

var matchedTemplateTypes = new HashSet<TypeDefinition> ();
var mismatchedTypes = new List<TypeDefinition> ();

foreach (TypeDefinition type in types) {
TypeDefinition? matchedType = null;

foreach (TypeDefinition templateType in templateTypes) {
if (matchedTemplateTypes.Contains (templateType) || !CheckWhetherTypesMatch (templateType, type)) {
continue;
}

matchedTemplateTypes.Add (templateType);
matchedType = templateType;
break;
}
if (!typesSet.SetEquals (templateSet)) {
logger.LogError ($"Architecture '{state.TargetArch}' has Java types which have no counterparts in template architecture '{templateState.TargetArch}':");

if (matchedType == null) {
mismatchedTypes.Add (type);
}
}
typesSet.ExceptWith (templateSet);

if (mismatchedTypes.Count > 0) {
logger.LogError ($"Architecture '{state.TargetArch}' has Java types which have no counterparts in template architecture '{templateState.TargetArch}':");
foreach (TypeDefinition td in mismatchedTypes) {
logger.LogError ($" {td.FullName}");
}
foreach (var type in typesSet)
logger.LogError ($" {type}");
}
}

Expand Down