You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 9, 2026. It is now read-only.
Repository navigation
Commit 0cb8e2d
Browse filesBrowse the repository at this point in the historyBrowse files
[Xamarin.Android.Tools.ApiXmlAdjuster] Use Dictionaries for perf (#756)
The `Xamarin.Android.Tools.ApiXmlAdjuster` process builds an in-memory
model of every Java type we know about, and then queries this model
many times in order to ensure we can resolve every needed Java type to
build a binding. The data structure of this model is:
public class JavaApi
{
public List<JavaPackage> Packages { get; }
}
public class JavaPackage
{
public List<JavaType> Types { get; }
}
The model is then queried using LINQ::
// Bad
var type = api.Packages.SelectMany (p => p.Types)
.FirstOrDefault (t => t.Name == type_name);
// Less bad
var type = api.Packages.FirstOrDefault (p => p.Name == pkg_name)
?.Types.FirstOrDefault (t => t.Name == type_name);
In the random GooglePlayServices package I used for testing, `JavaApi`
contained 310 packages and a total of 5941 types. Repeatedly looping
through them looking for the correct type takes a considerable amount
of time.
Instead, we can use a `Dictionary` to store packages and types keyed by
name to significantly speed up type resolution:
public class JavaApi
{
public Dictionary<string, JavaPackage> Packages { get; }
}
public class JavaPackage
{
public Dictionary<string, List<JavaType>> Types { get; }
}
For the GPS project, this reduced time taken considerably:
| Version | Time |
| ------- | ---- |
| master | 9.3s |
| This PR | 1.4s |
The only "interesting" detail is that we can have multiple types with
the same *Java* name, such as `MyInterface` and `MyInterfaceConsts`.
Thus we need to use `Dictionary<string, List<JavaType>>` to ensure we
collect them all.
0 commit comments