Skip to content

Commit 920ea64

Browse files
[Java.Interop] optimize JniTypeManager.AssertSimpleReference() (dotnet#1001)
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)
1 parent 4b4fedd commit 920ea64

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,20 @@ void AssertValid ()
115115

116116
internal static void AssertSimpleReference (string jniSimpleReference, string argumentName = "jniSimpleReference")
117117
{
118-
if (jniSimpleReference == null)
118+
if (string.IsNullOrEmpty (jniSimpleReference))
119119
throw new ArgumentNullException (argumentName);
120-
if (jniSimpleReference != null && jniSimpleReference.IndexOf (".", StringComparison.Ordinal) >= 0)
120+
if (jniSimpleReference.IndexOf ('.') >= 0)
121121
throw new ArgumentException ("JNI type names do not contain '.', they use '/'. Are you sure you're using a JNI type name?", argumentName);
122-
if (jniSimpleReference != null && jniSimpleReference.StartsWith ("[", StringComparison.Ordinal))
123-
throw new ArgumentException ("Arrays cannot be present in simplified type references.", argumentName);
124-
if (jniSimpleReference != null && jniSimpleReference.StartsWith ("L", StringComparison.Ordinal) && jniSimpleReference.EndsWith (";", StringComparison.Ordinal))
125-
throw new ArgumentException ("JNI type references are not supported.", argumentName);
122+
switch (jniSimpleReference [0]) {
123+
case '[':
124+
throw new ArgumentException ("Arrays cannot be present in simplified type references.", argumentName);
125+
case 'L':
126+
if (jniSimpleReference [jniSimpleReference.Length - 1] == ';')
127+
throw new ArgumentException ("JNI type references are not supported.", argumentName);
128+
break;
129+
default:
130+
break;
131+
}
126132
}
127133

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

0 commit comments

Comments
 (0)