Skip to content

Commit 4df1e9f

Browse files
authored
[Java.Interop] Reduce string allocations from assertions (#55)
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=42476 Bug #42476 notes that the `string.Format()` within the JNI reference count assertions contributes to process memory allocations which the profiler reports, and those generated `strings` are pointless because they're not used unless the assertion fails. This is a fair point, outside of whether or not the assertions should be present at all... Improve the `JniRuntime.JniObjectReferenceManager` JNI reference assertions so that they return early if the assertion wouldn't fire. This avoids the unnecessary string allocation.
1 parent 9fbade1 commit 4df1e9f

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Java.Interop/Java.Interop/JniRuntime.JniObjectReferenceManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ protected virtual void Dispose (bool disposing)
168168
[Conditional ("DEBUG")]
169169
static void AssertReferenceType (ref JniObjectReference reference, JniObjectReferenceType type)
170170
{
171+
if (reference.Type == type)
172+
return;
173+
171174
Debug.Assert (reference.Type == type,
172175
string.Format ("Object reference {0} should be of type {1}, is instead {2}!",
173176
reference.ToString (), type, reference.Type));
@@ -176,6 +179,9 @@ static void AssertReferenceType (ref JniObjectReference reference, JniObjectRefe
176179
[Conditional ("DEBUG")]
177180
void AssertCount (int count, string type, string value)
178181
{
182+
if (count >= 0)
183+
return;
184+
179185
Debug.Assert (count >= 0,
180186
string.Format ("{0} count is {1}, expected to be >= 0 when dealing with handle {2} on thread '{3}'({4}).",
181187
type, count, value, Runtime.GetCurrentManagedThreadName (), Environment.CurrentManagedThreadId));

0 commit comments

Comments
 (0)