[docs] Update optimizations document with upstream changes. (#4043)

Ref: https://github.com/MicrosoftDocs/xamarin-docs-pr/pull/204
This commit is contained in:
Rolf Bjarne Kvinge 2018-05-08 07:24:23 +02:00 коммит произвёл GitHub
Родитель 1d94887b46
Коммит 3c2f87abc5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 59 добавлений и 57 удалений

Просмотреть файл

@ -1,20 +1,20 @@
---
id: 84B67E31-B217-443D-89E5-CFE1923CB14E
title: Build optimizations
dateupdated: 2018-01-18
title: "Build optimizations"
description: "This document explains the various optimizations that are applied at build time for Xamarin.iOS and Xamarin.Mac apps."
ms.prod: xamarin
ms.assetid: 84B67E31-B217-443D-89E5-CFE1923CB14E
ms.technology: xamarin-cross-platform
author: bradumbaugh
ms.author: brumbaug
dateupdated: 04/16/2018
---
[//]: # (The original file resides under https://github.com/xamarin/xamarin-macios/tree/master/docs/website/)
[//]: # (This allows all contributors (including external) to submit, using a PR, updates to the documentation that match the tools changes)
[//]: # (Modifications outside of xamarin-macios/master will be lost on future updates)
# Build optimizations
Build optimizations
===================
This document explains the various optimizations that are applied at build time for
Xamarin.iOS and Xamarin.Mac apps.
This document explains the various optimizations that are applied at build time for Xamarin.iOS and Xamarin.Mac apps.
Remove UIApplication.EnsureUIThread / NSApplication.EnsureUIThread
------------------------------------------------------------------
## Remove UIApplication.EnsureUIThread / NSApplication.EnsureUIThread
Removes calls to [UIApplication.EnsureUIThread][1] (for Xamarin.iOS) or
`NSApplication.EnsureUIThread` (for Xamarin.Mac).
@ -47,10 +47,9 @@ The default behavior can be overridden by passing `--optimize=[+|-]remove-uithre
[1]: https://developer.xamarin.com/api/member/UIKit.UIApplication.EnsureUIThread/
Inline IntPtr.Size
------------------
## Inline IntPtr.Size
Inlines the constant value of IntPtr.Size according to the target platform.
Inlines the constant value of `IntPtr.Size` according to the target platform.
This optimization will change the following type of code:
@ -76,26 +75,25 @@ This optimization requires the linker to be enabled, and is only applied to
methods with the `[BindingImpl (BindingImplOptions.Optimizable)]` attribute.
By default it's enabled if targeting a single architecture, or for the
platform assembly (Xamarin.iOS.dll, Xamarin.TVOS.dll, Xamarin.WatchOS.dll or
Xamarin.Mac.dll).
platform assembly (**Xamarin.iOS.dll**, **Xamarin.TVOS.dll**,
**Xamarin.WatchOS.dll** or **Xamarin.Mac.dll**).
If targeting multiple architecture, this optimization will create different
If targeting multiple architectures, this optimization will create different
assemblies for the 32-bit version and the 64-bit version of the app, and both
versions will have to be included in the app, effectively increasing the final
app size instead of decreasing it.
The default behavior can be overridden by passing `--optimize=[+|-]inline-intptr-size` to mtouch/mmp.
Inline NSObject.IsDirectBinding
-------------------------------
## Inline NSObject.IsDirectBinding
NSObject.IsDirectBinding is an instance property that determines whether a
`NSObject.IsDirectBinding` is an instance property that determines whether a
particular instance is of a wrapper type or not (a wrapper type is a managed
type that maps to a native type; for instance the managed `UIKit.UIView` type
maps to the native `UIView` type - the opposite is a user type, in this case
`class MyUIView : UIKit.UIView` would be a user type).
It's necessary to know the value of IsDirectBinding when calling into
It's necessary to know the value of `IsDirectBinding` when calling into
Objective-C, because the value determines which version of `objc_msgSend` to
use.
@ -130,7 +128,8 @@ class MyUIView : UIView {
}
```
we can determine that in `UIView.SomeProperty` the value of `IsDirectBinding` is not a constant and can not be inlined:
We can determine that in `UIView.SomeProperty` the value of
`IsDirectBinding` is not a constant and cannot be inlined:
```csharp
void uiView = new UIView ();
@ -139,7 +138,7 @@ void myView = new MyUIView ();
Console.WriteLine (myView.SomeProperty); // prints 'false'
```
however it's possible to look at the all the types in the app and determine
However, it's possible to look at the all the types in the app and determine
that there are no types that inherit from `NSUrl`, and it's thus safe to
inline the `IsDirectBinding` value to a constant `true`:
@ -150,7 +149,7 @@ Console.WriteLine (myURL.SomeOtherProperty); // prints 'true'
```
In particular, this optimization will change the following type of code (this
is the binding code for NSUrl.AbsoluteUrl):
is the binding code for `NSUrl.AbsoluteUrl`):
```csharp
if (IsDirectBinding) {
@ -161,7 +160,7 @@ if (IsDirectBinding) {
```
into the following (when it can be determined that there are no subclasses of
NSUrl in the app):
`NSUrl` in the app):
```csharp
if (true) {
@ -179,10 +178,10 @@ default for Xamarin.Mac (because it's possible to dynamically load assemblies
in Xamarin.Mac, it's not possible to determine that a particular class is
never subclassed).
The default behavior can be overridden by passing `--optimize=[+|-]inline-isdirectbinding` to mtouch/mmp.
The default behavior can be overridden by passing
`--optimize=[+|-]inline-isdirectbinding` to mtouch/mmp.
Inline Runtime.Arch
-------------------
## Inline Runtime.Arch
This optimization will change the following type of code:
@ -210,10 +209,10 @@ methods with the `[BindingImpl (BindingImplOptions.Optimizable)]` attribute.
It is always enabled by default for Xamarin.iOS (it's not available for
Xamarin.Mac).
The default behavior can be overridden by passing `--optimize=[+|-]inline-runtime-arch` to mtouch.
The default behavior can be overridden by passing
`--optimize=[+|-]inline-runtime-arch` to mtouch.
Dead code elimination
---------------------
## Dead code elimination
This optimization will change the following type of code:
@ -249,7 +248,7 @@ Console.WriteLine ("Doing this");
This is a powerful optimization when used together with the inlining
optimizations, because it can transform the following type of code (this is
the binding code for NFCIso15693ReadMultipleBlocksConfiguration.Range):
the binding code for `NFCIso15693ReadMultipleBlocksConfiguration.Range`):
```csharp
NSRange ret;
@ -281,7 +280,9 @@ if (IsDirectBinding) {
return ret;
```
into this (when building for a 64-bit device, and when also able to ensure there are no NFCIso15693ReadMultipleBlocksConfiguration subclasses in the app):
into this (when building for a 64-bit device, and when also able to
ensure there are no `NFCIso15693ReadMultipleBlocksConfiguration` subclasses
in the app):
```csharp
NSRange ret;
@ -294,19 +295,19 @@ optimization is done inside the linker, which means that the linker able to
see that there are multiple methods that are not used anymore, and may thus be
removed (unless used elsewhere):
* global::ObjCRuntime.Messaging.NSRange_objc_msgSend_stret
* global::ObjCRuntime.Messaging.NSRange_objc_msgSendSuper
* global::ObjCRuntime.Messaging.NSRange_objc_msgSendSuper_stret
* `global::ObjCRuntime.Messaging.NSRange_objc_msgSend_stret`
* `global::ObjCRuntime.Messaging.NSRange_objc_msgSendSuper`
* `global::ObjCRuntime.Messaging.NSRange_objc_msgSendSuper_stret`
This optimization requires the linker to be enabled, and is only applied to
methods with the `[BindingImpl (BindingImplOptions.Optimizable)]` attribute.
It is always enabled by default (when the linker is enabled).
The default behavior can be overridden by passing `--optimize=[+|-]dead-code-elimination` to mtouch/mmp.
The default behavior can be overridden by passing
`--optimize=[+|-]dead-code-elimination` to mtouch/mmp.
Optimize calls to BlockLiteral.SetupBlock
-----------------------------------------
## Optimize calls to BlockLiteral.SetupBlock
The Xamarin.iOS/Mac runtime needs to know the block signature when creating an
Objective-C block for a managed delegate. This might be a fairly expensive
@ -346,10 +347,10 @@ It is enabled by default when using the static registrar (in Xamarin.iOS the
static registrar is enabled by default for device builds, and in Xamarin.Mac
the static registrar is enabled by default for release builds).
The default behavior can be overridden by passing `--optimize=[+|-]blockliteral-setupblock` to mtouch/mmp.
The default behavior can be overridden by passing
`--optimize=[+|-]blockliteral-setupblock` to mtouch/mmp.
Optimize support for protocols
------------------------------
## Optimize support for protocols
The Xamarin.iOS/Mac runtime needs information about how managed types
implements Objective-C protocols. This information is stored in interfaces
@ -379,10 +380,10 @@ On Xamarin.Mac this optimization is never enabled by default, because
Xamarin.Mac supports loading assemblies dynamically, and those assemblies
might not have been known at build time (and thus not optimized).
The default behavior can be overridden by passing `--optimize=-register-protocols` to mtouch/mmp.
The default behavior can be overridden by passing
`--optimize=-register-protocols` to mtouch/mmp.
Remove the dynamic registrar
----------------------------
## Remove the dynamic registrar
Both the Xamarin.iOS and the Xamarin.Mac runtime include support for
[registering managed types](https://developer.xamarin.com/guides/ios/advanced_topics/registrar/)
@ -403,21 +404,21 @@ were not known at build time), it's impossible to determine at build time
whether this is a safe optimization. This means that this optimization is
never enabled by default for Xamarin.Mac apps.
The default behavior can be overridden by passing `--optimize=[+|-]remove-dynamic-registrar` to mtouch/mmp.
The default behavior can be overridden by passing
`--optimize=[+|-]remove-dynamic-registrar` to mtouch/mmp.
If the default is overridden to remove the dynamic registrar, the linker will
emit warnings if it detects that it's not safe (but the dynamic registrar will
still be removed).
Inline Runtime.DynamicRegistrationSupported
-------------------------------------------
## Inline Runtime.DynamicRegistrationSupported
Inlines the value of Runtime.DynamicRegistrationSupported as determined at
Inlines the value of `Runtime.DynamicRegistrationSupported` as determined at
build time.
If the dynamic registrar is removed (see the "Remove the dynamic registrar"
optimization), this is a constant 'false' value, otherwise it's a constant
'true' value.
If the dynamic registrar is removed (see the [Remove the dynamic
registrar](#remove-the-dynamic-registrar) optimization), this is a
constant `false` value, otherwise it's a constant `true` value.
This optimization will change the following type of code:
@ -446,10 +447,10 @@ methods with the `[BindingImpl (BindingImplOptions.Optimizable)]` attribute.
It is always enabled by default (when the linker is enabled).
The default behavior can be overridden by passing `--optimize=[+|-]inline-dynamic-registration-supported` to mtouch/mmp.
The default behavior can be overridden by passing
`--optimize=[+|-]inline-dynamic-registration-supported` to mtouch/mmp.
Precompute methods to create managed delegates for Objective-C blocks
---------------------------------------------------------------------
## Precompute methods to create managed delegates for Objective-C blocks
When Objective-C calls a selector that takes a block as a parameter, and then
managed code has overriden that method, the Xamarin.iOS / Xamarin.Mac runtime
@ -461,12 +462,13 @@ can do this.
Given the following Objective-C code:
```objective-c
```objc
@interface ObjCBlockTester : NSObject {
}
-(void) classCallback: (void (^)())completionHandler;
-(void) callClassCallback;
@end
@implementation ObjCBlockTester
-(void) classCallback: (void (^)())completionHandler
{