Sebastien Pouliot
|
fccd43f0c2
|
[dotnet][linker] Remove unused backing fields (#12001)
Problems:
* `Dispose` set the generated backing fields to `null` which means
the linker will mark every backing fields, even if not used
elsewhere (generally properties) inside the class
* Backing fields increase the memory footprint of the managed peer
instance (for the type and all it's subclasses)
* Backing fields also increase the app size. Not a huge problem as
they are all declared _weakly_ as `NSObject` but still...
Solution:
* When the linker process a `Dispose` method of an `NSObject`
subclass with the _optimizable_ attribute then we remove the
method body. This way the linker cannot mark the fields.
* Before saving back the assemblies we replace the cached method
body and NOP every field that were not marked by something else
than the `Dispose` method.
```diff
--- a.cs 2021-06-22 16:56:57.000000000 -0400
+++ b.cs 2021-06-22 16:57:00.000000000 -0400
@@ -3107,8 +3107,6 @@
private static readonly IntPtr class_ptr = Class.GetHandle("UIApplication");
- private object __mt_WeakDelegate_var;
-
public override IntPtr ClassHandle => class_ptr;
[DllImport("__Internal")]
@@ -3141,9 +3139,8 @@
protected override void Dispose(bool P_0)
{
base.Dispose(P_0);
- if (base.Handle == IntPtr.Zero)
+ if (!(base.Handle == IntPtr.Zero))
{
- __mt_WeakDelegate_var = null;
}
}
}
@@ -3209,10 +3206,6 @@
{
private static readonly IntPtr class_ptr = Class.GetHandle("UIScreen");
- private object __mt_FocusedItem_var;
-
- private object __mt_FocusedView_var;
-
public override IntPtr ClassHandle => class_ptr;
public virtual CGRect Bounds
@@ -3242,10 +3235,8 @@
protected override void Dispose(bool P_0)
{
base.Dispose(P_0);
- if (base.Handle == IntPtr.Zero)
+ if (!(base.Handle == IntPtr.Zero))
{
- __mt_FocusedItem_var = null;
- __mt_FocusedView_var = null;
}
}
}
@@ -3254,10 +3245,6 @@
{
private static readonly IntPtr class_ptr = Class.GetHandle("UIView");
- private object __mt_ParentFocusEnvironment_var;
-
- private object __mt_PreferredFocusedView_var;
-
public override IntPtr ClassHandle => class_ptr;
public virtual CGRect Bounds
@@ -3303,10 +3290,8 @@
protected override void Dispose(bool P_0)
{
base.Dispose(P_0);
- if (base.Handle == IntPtr.Zero)
+ if (!(base.Handle == IntPtr.Zero))
{
- __mt_ParentFocusEnvironment_var = null;
- __mt_PreferredFocusedView_var = null;
}
}
}
@@ -3315,12 +3300,6 @@
{
private static readonly IntPtr class_ptr = Class.GetHandle("UIViewController");
- private object __mt_ParentFocusEnvironment_var;
-
- private object __mt_PreferredFocusedView_var;
-
- private object __mt_WeakTransitioningDelegate_var;
-
public override IntPtr ClassHandle => class_ptr;
public virtual UIView View
@@ -3363,11 +3342,8 @@
protected override void Dispose(bool P_0)
{
base.Dispose(P_0);
- if (base.Handle == IntPtr.Zero)
+ if (!(base.Handle == IntPtr.Zero))
{
- __mt_ParentFocusEnvironment_var = null;
- __mt_PreferredFocusedView_var = null;
- __mt_WeakTransitioningDelegate_var = null;
}
}
}
@@ -3376,8 +3352,6 @@
{
private static readonly IntPtr class_ptr = Class.GetHandle("UIWindow");
- private object __mt_WindowScene_var;
-
public override IntPtr ClassHandle => class_ptr;
public virtual UIViewController RootViewController
@@ -3411,9 +3385,8 @@
protected override void Dispose(bool P_0)
{
base.Dispose(P_0);
- if (base.Handle == IntPtr.Zero)
+ if (!(base.Handle == IntPtr.Zero))
{
- __mt_WindowScene_var = null;
}
}
}
```
* Do not consider bindings with `[Dispose (...)]` as optimizable
Injected code makes it impossible for `bgen` to decide if it's optimizable (or not)
Filed https://github.com/xamarin/xamarin-macios/issues/12150 with more details (and for other similar attributes)
|
2021-07-21 09:03:25 -04:00 |