From 4813c0349bccec67a80d7d9a0044c6240a616915 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 29 Jun 2022 16:56:40 -0500 Subject: [PATCH 1/2] [Java.Interop] optimize JniTypeManager.AssertSimpleReference() 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 1f27ab55. For now, I think we can rewrite this to use the `char` overload of `IndexOf`, as well as the regular indexer on `string`. 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. --- .../Java.Interop/JniRuntime.JniTypeManager.cs | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs index 8e6738fa6..fd101cdfc 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs @@ -115,14 +115,20 @@ void AssertValid () internal static void AssertSimpleReference (string jniSimpleReference, string argumentName = "jniSimpleReference") { - if (jniSimpleReference == null) - throw new ArgumentNullException (argumentName); - if (jniSimpleReference != null && jniSimpleReference.IndexOf (".", StringComparison.Ordinal) >= 0) + if (string.IsNullOrEmpty (jniSimpleReference)) + throw new ArgumentNullException (nameof (jniSimpleReference)); + 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() From d6d46090892abea34f0de78e9920f745d2164f6f Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Wed, 29 Jun 2022 20:47:07 -0400 Subject: [PATCH 2/2] Consistently use argumentName src/Java.Interop/Java.Interop/JniTypeSignatureAttribute.cs: JniRuntime.JniTypeManager.AssertSimpleReference (simpleReference, nameof (simpleReference)); Not all codepaths use `jniSimpleReference`. Use `argumentName` for the argument name. --- src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs index fd101cdfc..535db5c64 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs @@ -116,7 +116,7 @@ void AssertValid () internal static void AssertSimpleReference (string jniSimpleReference, string argumentName = "jniSimpleReference") { if (string.IsNullOrEmpty (jniSimpleReference)) - throw new ArgumentNullException (nameof (jniSimpleReference)); + throw new ArgumentNullException (argumentName); 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); switch (jniSimpleReference [0]) {