-
Notifications
You must be signed in to change notification settings - Fork 556
[NativeAOT] Generate optimized type mapping #9856
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
Merged
jonpryor
merged 21 commits into
main
from
dev/simonrozsival/managed-jni-to-managed-type-map
Mar 7, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
97b4829
Add managed typemap
simonrozsival d7fbbbd
Fix loading System.IO.Hashing in NativeAOT builds
simonrozsival d7480b7
Load System.IO.Hashing dependency in the custom linker step
simonrozsival 3e934f1
Rename TypeMap to TypeMapping
simonrozsival 97fcb17
Avoid converting UTF-16 to UTF-8
simonrozsival decbeef
Address first feedback
simonrozsival 2e29a43
Avoid array allocation
simonrozsival 4341abc
Avoid using PrivateImplementationDetails
simonrozsival 99a21e7
Fix invalid IL
simonrozsival f6d0b9b
fixup! Avoid using PrivateImplementationDetails
simonrozsival 9c6c807
Apply suggestions from code review
simonrozsival b85d4a0
Use Assembly.Load
simonrozsival 0b74cd5
Use LE for hashes on all platforms
simonrozsival 2823ed9
Ensure we don't return incorrect Type bacause of a hash collision
simonrozsival 3462874
Update src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.Native…
simonrozsival e5e6a09
Fix build error
simonrozsival 5c3b4c9
Setup `%(NoSymbols)=true`
jonathanpeppers cdfc2d0
Improve managed -> java class name mapping
simonrozsival 4569942
Fix CS8604
simonrozsival 3775e65
Update NativeAOT build test
simonrozsival dc7d06b
Avoid duplicating nested classes
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/TypeMapping.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.Buffers.Binary; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO.Hashing; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Android.Runtime; | ||
|
||
namespace Microsoft.Android.Runtime; | ||
|
||
internal static class TypeMapping | ||
{ | ||
internal static bool TryGetType (string javaClassName, [NotNullWhen (true)] out Type? type) | ||
{ | ||
ulong hash = Hash (javaClassName); | ||
|
||
// the hashes array is sorted and all the hashes are unique | ||
int typeIndex = MemoryExtensions.BinarySearch (JavaClassNameHashes, hash); | ||
if (typeIndex < 0) { | ||
type = null; | ||
return false; | ||
} | ||
|
||
type = GetTypeByIndex (typeIndex); | ||
if (type is null) { | ||
throw new InvalidOperationException ($"Type with hash {hash} not found."); | ||
} | ||
|
||
// ensure this is not a hash collision | ||
var resolvedJavaClassName = GetJavaClassNameByIndex (TypeIndexToJavaClassNameIndex [typeIndex]); | ||
if (resolvedJavaClassName != javaClassName) { | ||
type = null; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
internal static bool TryGetJavaClassName (Type type, [NotNullWhen (true)] out string? className) | ||
{ | ||
string? fullName = type.FullName; | ||
if (fullName is null) { | ||
className = null; | ||
return false; | ||
} | ||
|
||
ulong hash = Hash (fullName); | ||
|
||
// the hashes array is sorted and all the hashes are unique | ||
int javaClassNameIndex = MemoryExtensions.BinarySearch (TypeNameHashes, hash); | ||
if (javaClassNameIndex < 0) { | ||
className = null; | ||
return false; | ||
} | ||
|
||
className = GetJavaClassNameByIndex (javaClassNameIndex); | ||
if (className is null) { | ||
throw new InvalidOperationException ($"Java class name with hash {hash} not found."); | ||
} | ||
|
||
// ensure this is not a hash collision | ||
var resolvedType = GetTypeByIndex (JavaClassNameIndexToTypeIndex [javaClassNameIndex]); | ||
if (resolvedType?.FullName != type.FullName) { | ||
className = null; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static ulong Hash (string javaClassName) | ||
{ | ||
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes (javaClassName.AsSpan ()); | ||
ulong hash = XxHash3.HashToUInt64 (bytes); | ||
|
||
// The bytes in the hashes array are stored as little endian. If the target platform is big endian, | ||
// we need to reverse the endianness of the hash. | ||
if (!BitConverter.IsLittleEndian) { | ||
hash = BinaryPrimitives.ReverseEndianness (hash); | ||
} | ||
|
||
return hash; | ||
} | ||
|
||
// Replaced by src/Microsoft.Android.Sdk.ILLink/TypeMappingStep.cs | ||
private static ReadOnlySpan<ulong> JavaClassNameHashes => throw new NotImplementedException (); | ||
private static ReadOnlySpan<ulong> TypeNameHashes => throw new NotImplementedException (); | ||
private static ReadOnlySpan<int> JavaClassNameIndexToTypeIndex => throw new NotImplementedException (); | ||
private static ReadOnlySpan<int> TypeIndexToJavaClassNameIndex => throw new NotImplementedException (); | ||
private static Type? GetTypeByIndex (int index) => throw new NotImplementedException (); | ||
private static string? GetJavaClassNameByIndex (int index) => throw new NotImplementedException (); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you going to check the uniqueness of the hashcodes at build time to guarantee this?
An alternative way to deal with this would be to allow non-unique hashcodes and check all candidates in the map. If you do that, you can change the hashcodes to 32-bit that makes the map 2x smaller and dealing with hashcodes faster, at the cost of a rare hash collision.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the check is here: https://github.com/dotnet/android/pull/9856/files#diff-8e22a99ebc58c9417a48013888fc90d72fa6b638aff5d2d449cbf2c78bd0a8fbR149-R161
That's definitely worth trying. Assuming even the XxHash32 collisions are very rare, in the typical case there should be just 1 string comparison to verify that the hash belongs to the expected result.