Skip to content

Commit

Permalink
[Java.Interop] optimize JniTypeManager.AssertSimpleReference() (#1001)
Browse files Browse the repository at this point in the history
We noticed `dotnet trace` output was showing:

	21.28ms (0.45%) java.interop!Java.Interop.JniRuntime.JniTypeManager.AssertSimpleReference(string,string)

This code path was introduced by a new feature in 1f27ab5.

For now, I think we can rewrite this to use the
[`string.IndexOf(char)`][0] overload of `string.IndexOf()`, as well
as the `string` indexer instead of `string.StartsWith()` and
`string.EndsWith()`.

After these changes, I get a better time:

	 1.21ms java.interop!Java.Interop.JniRuntime.JniTypeManager.AssertSimpleReference(string,string)

We may have just *moved* the location that ICU is loaded, but this
change is good regardless.

[0]: https://docs.microsoft.com/en-us/dotnet/api/system.string.indexof?view=net-6.0#system-string-indexof(system-char)
  • Loading branch information
jonathanpeppers committed Jun 30, 2022
1 parent 4b4fedd commit 920ea64
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs
Expand Up @@ -115,14 +115,20 @@ void AssertValid ()

internal static void AssertSimpleReference (string jniSimpleReference, string argumentName = "jniSimpleReference")
{
if (jniSimpleReference == null)
if (string.IsNullOrEmpty (jniSimpleReference))
throw new ArgumentNullException (argumentName);
if (jniSimpleReference != null && jniSimpleReference.IndexOf (".", StringComparison.Ordinal) >= 0)
if (jniSimpleReference.IndexOf ('.') >= 0)
throw new ArgumentException ("JNI type names do not contain '.', they use '/'. Are you sure you're using a JNI type name?", argumentName);
if (jniSimpleReference != null && jniSimpleReference.StartsWith ("[", StringComparison.Ordinal))
throw new ArgumentException ("Arrays cannot be present in simplified type references.", argumentName);
if (jniSimpleReference != null && jniSimpleReference.StartsWith ("L", StringComparison.Ordinal) && jniSimpleReference.EndsWith (";", StringComparison.Ordinal))
throw new ArgumentException ("JNI type references are not supported.", argumentName);
switch (jniSimpleReference [0]) {
case '[':
throw new ArgumentException ("Arrays cannot be present in simplified type references.", argumentName);
case 'L':
if (jniSimpleReference [jniSimpleReference.Length - 1] == ';')
throw new ArgumentException ("JNI type references are not supported.", argumentName);
break;
default:
break;
}
}

// NOTE: This method needs to be kept in sync with GetTypeSignatures()
Expand Down

0 comments on commit 920ea64

Please sign in to comment.