Skip to content

Commit

Permalink
添加 优化补充元数据dll大小的文档
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Jan 12, 2024
1 parent 14772fd commit 3334360
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
38 changes: 38 additions & 0 deletions docs/basic/aotgeneric.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,44 @@ public class AOTGenericReferences : UnityEngine.MonoBehaviour
}
```

## 优化补充元数据dll大小

加载补充元数据dll不仅增加了包体或者热更新资源大小,运行时加载也消耗了可观的内存空间,详细见[内存与GC](./memory)文档。优化补充元数据dll大小
对于内存敏感的场合有积极意义。

补充元数据技术只用到了补充元数据dll中泛型函数的元数据信息,补充元数据dll中包含的非泛型函数的元数据是多余的,将它们完全剔除不会
影响补充元数据机制的正常工作。因此`com.code-philosophy.hybridclr`自v4.0.16版本起提供了补充元数据优化工具类`HybridCLR.Editor.AOT.AOTAssemblyMetadataStripper`
实现这个剔除优化工作。

这个剔除效果因assembly而异,效果差别较大,以下是我们在单元测试工程上的测试结果:

|程序集名|原始大小|优化后大小|优化率|
|-|-|-|-|
|mscorlib|2139k|1329k|37.9%|
|System|186k|63.0k|66.2%|
|System.Core|96.3k|89.1k|7.4%|


示例代码如下:

```csharp

/// 进一步剔除AOT dll中非泛型函数元数据,输出到StrippedAOTAssembly2目录下
public static void StripAOTAssembly()
{
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
string srcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
string dstDir = $"{SettingsUtil.HybridCLRDataDir}/StrippedAOTAssembly2/{target}";
foreach (var src in Directory.GetFiles(srcDir, "*.dll"))
{
string dllName = Path.GetFileName(src);
string dstFile = $"{dstDir}/{dllName}";
AOTAssemblyMetadataStripper.Strip(src, dstFile);
}
}

```


## full generic sharing 技术

Expand Down
3 changes: 3 additions & 0 deletions docs/beginner/generic.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ public class LoadDll : MonoBehaviour

现在你可以在热更新代码随意使用AOT泛型了。

## 优化补充元数据dll大小

默认生成的补充元数据dll中包含了大量补充元数据机制不需要的数据,自v4.0.16版本起支持对补充元数据dll进行剔除优化,详细文档见[AOT泛型](../basic/aotgeneric)
38 changes: 38 additions & 0 deletions i18n/en/docusaurus-plugin-content-docs/current/basic/aotgeneric.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,44 @@ See the sample code below for how to load the supplementary metadata dll in the
}
```

## Optimize supplementary metadata dll size

Loading supplementary metadata dll not only increases the size of the package body or hot update resources, but also consumes considerable memory space during runtime loading. For details, see the [Memory and GC] (./memory) document. Optimize supplementary metadata dll size
It has positive significance for memory-sensitive situations.

Supplementary metadata technology only uses the metadata information of generic functions in the supplementary metadata dll. The metadata of non-generic functions contained in the supplementary metadata dll is redundant. Eliminating them completely will not
Affects the normal working of the supplementary metadata mechanism. Therefore `com.code-philosophy.hybridclr` provides a supplementary metadata optimization tool class `HybridCLR.Editor.AOT.AOTAssemblyMetadataStripper` since version v4.0.16
Implement this elimination optimization work.

This elimination effect varies greatly from assembly to assembly. The following are our test results in the unit test project:

|Assembly name|Original size|Optimized size|Optimization rate|
|-|-|-|-|
|mscorlib|2139k|1329k|37.9%|
|System|186k|63.0k|66.2%|
|System.Core|96.3k|89.1k|7.4%|


The sample code is as follows:

```csharp

/// remove the non-generic function metadata from the trimmed AOT dlls and output it to the StrippedAOTAssembly2 directory.
public static void StripAOTAssembly()
{
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
string srcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
string dstDir = $"{SettingsUtil.HybridCLRDataDir}/StrippedAOTAssembly2/{target}";
foreach (var src in Directory.GetFiles(srcDir, "*.dll"))
{
string dllName = Path.GetFileName(src);
string dstFile = $"{dstDir}/{dllName}";
AOTAssemblyMetadataStripper.Strip(src, dstFile);
}
}

```

## AOT generic problems caused by some C# special mechanisms

The compiler may generate implicit AOT generic references for complex syntactic sugar such as async. Therefore, in order for these mechanisms to work properly, the AOT generic instantiation problems caused by them must also be resolved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@ public class LoadDll : MonoBehaviour
}
```

Now you can freely use AOT generics in hot update code.
Now you can freely use AOT generics in hot update code.

## Optimize supplementary metadata dll size

The supplementary metadata dll generated by default contains a large amount of data that is not required by the supplementary metadata mechanism. Starting from version v4.0.16, the supplementary metadata dll is supported for elimination and optimization. For detailed documentation, see [AOT Generics](../basic/ aotgeneric).

0 comments on commit 3334360

Please sign in to comment.