Skip to content

[Native] Remove unused field weak_handle#12239

Open
AustinWise wants to merge 1 commit into
dotnet:mainfrom
AustinWise:awise/control-block-size
Open

[Native] Remove unused field weak_handle#12239
AustinWise wants to merge 1 commit into
dotnet:mainfrom
AustinWise:awise/control-block-size

Conversation

@AustinWise

Copy link
Copy Markdown

This field on JniObjectReferenceControlBlock was never written to. The only time it was read was for debug logging.
This shrinks the size of the control block from 32 to 16 bytes on 64-bit platforms.

While I was at it, I added the the StructLayout attribute to the JniObjectReferenceControlBlock in C#. This makes the warning suppression for CS0649 no longer necessary.


  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

This field on `JniObjectReferenceControlBlock` was never written to.
The only time it was read was for debug logging.
This shrinks the size of the control block from 32 to 16 bytes
on 64-bit platforms.

While I was at it, I added the the `StructLayout` attribute to the
`JniObjectReferenceControlBlock` in C#. This makes the warning
suppression for CS0649 no longer necessary.
@jonathanpeppers

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Android PR Reviewer completed successfully!

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Review — ✅ LGTM (minor)

Clean, well-scoped removal of the unused weak_handle field from JniObjectReferenceControlBlock. Verified independently before reading the description.

Verification performed:

  • 🔎 weak_handle has zero remaining readers or writers anywhere in the tree — confirmed via full-repo search.
  • 🧱 All four struct definitions are updated in lockstep, keeping the field layout identical across the managed/native boundary:
    • external/Java.Interop/.../JniObjectReferenceControlBlock.cs
    • src/Mono.Android/.../JavaMarshalRegisteredPeers.cs (internal mirror)
    • src/native/clr/include/host/gc-bridge.hh (CoreCLR)
    • src/native/mono/monodroid/osbridge.hh / osbridge.cc (Mono)
  • 📐 Resulting layout is handle (8) + handle_type (4) + refs_added (4) = 16 bytes on 64-bit, matching the PR's claim. bridge-processing.cc only touches handle/handle_type/refs_added, so the CLR bridge is unaffected.
  • 🪵 The two osbridge.cc diagnostic log sites that referenced key_handle (derived solely from weak_handle) are correctly cleaned up, including the format strings — no dangling format specifiers.
  • ✅ Dropping the #pragma warning disable CS0649 is now valid since the mirror struct is explicitly assigned/read via native memory and the previously-unassigned field is gone.

Notes:

  • 💡 One optional consistency suggestion posted inline re: adding the explicit [StructLayout] to the source Java.Interop struct as well.
  • PR is a draft and mergeable_state is blocked; the description leaves "Unit tests" unchecked. This is a pure field/logging removal with no behavioral change, so a dedicated test isn't strictly warranted — but confirm the native GC-bridge builds (Mono + CoreCLR) pass in CI before merging, since this touches the managed↔native struct contract.

Severity counts — ❌ 0 · ⚠️ 0 · 💡 1

Generated by Android PR Reviewer for #12239 · 64.4 AIC · ⌖ 18.6 AIC · ⊞ 6.8K
Comment /review to run again


#pragma warning disable CS0649 // Field 'JavaMarshalRegisteredPeers.HandleContext.JniObjectReferenceControlBlock.*' is never assigned to, and will always have its default value 0
// This is an internal mirror of the Java.Interop.JniObjectReferenceControlBlock
[StructLayout (LayoutKind.Sequential)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 💡 Consistency — Good call adding [StructLayout (LayoutKind.Sequential)] here to make the native-layout dependency explicit and drop the CS0649 suppression. For symmetry, consider adding the same attribute to the source Java.Interop.JniObjectReferenceControlBlock struct, since the native C++ definitions (gc-bridge.hh, osbridge.hh) read both by field offset. C# defaults structs to sequential layout so this isn't a bug today, but making it explicit on both mirrors documents that the ordering is intentional and load-bearing.

(Rule: Interop struct layout must match native definitions)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to keep these changes as minimal as possible, since .NET 11 is probably branching soon. Also, the definition of Java.Interop.JniObjectReferenceControlBlock was recently merged into this repository from the old Java.Interop repo. I'm assuming that duplicate copy is on the way out.

Anyways, I was not sure where to draw the line on these changes. Let me know if you want any revisions.

@AustinWise
AustinWise marked this pull request as ready for review July 27, 2026 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants