Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Next]: Natasha8.0 【轻量化】计划 #210

Closed
7 tasks done
NMSAzulX opened this issue Apr 26, 2023 · 7 comments
Closed
7 tasks done

[Next]: Natasha8.0 【轻量化】计划 #210

NMSAzulX opened this issue Apr 26, 2023 · 7 comments
Assignees
Labels
dependencies 有依赖需要升级 done enhancement New feature or request.
Milestone

Comments

@NMSAzulX
Copy link
Collaborator

NMSAzulX commented Apr 26, 2023

💡 描述一下你理想中的解决方案 (Describe the solution you'd like).

Natasha 8.0 轻量化计划

@NMSAzulX NMSAzulX added the enhancement New feature or request. label Apr 26, 2023
@NMSAzulX
Copy link
Collaborator Author

@whuanle

@NMSAzulX NMSAzulX pinned this issue Apr 28, 2023
@NMSAzulX
Copy link
Collaborator Author

NMSAzulX commented Apr 28, 2023

1. 尝试从现有 Assembly 中提取 元数据引用.

实验与描述

内存元数据引用:
运行时反射出来的是真实程序集, 包含了很多多余/不可用的类型及命名空间, 而且每次 Runtime 更新都有新的"危险"出现.
SDK 的引用程序集目前是最标准的最安全的方案.

第一次优化:

  1. 编译前的准备全部从真实程序集中采集可降低约 40M 内存. (单元未通过, 含有内部程序集)
  2. 引用从真实程序集中采集, using 覆盖从 引用程序集中采集, 可降低约 10M 内存. (单元测试通过)

第二次优化:

  1. 低内存模式: 尽可能降低 MetadataLoadContext 的占用, 理想情况可降低 30M 内存, 实际在获取 type 元数据是大致有8-9M内存涨幅. (单元测试通过)

第三次优化:

  1. 对于 CORE 以上版本, 增加排除指定程序集项, 预热方法参数支持两种低内存参数, useRuntimeUsinguseRuntimeReference, 全部开启时内存可降低至 40M. (单元测试通过)
  2. CORE 以下版本不做更改.

问题记录:

2. 减小发布包尺寸

使用缓存文件存储预热的一些数据。

综合优化:

阶段 环境 动作 内存 包输出大小
当前版本 WIN .NET6 控制台 预热/编译 67.3M 21.4 MB
试验版本v1 WIN .NET6 控制台 预热/编译 49.3M 21.4 MB
试验版本v2 WIN .NET6 控制台 预热/编译 20.0M(剪裁)/57.0M(覆盖) 16.5 MB

@NMSAzulX
Copy link
Collaborator Author

NMSAzulX commented Apr 28, 2023

3. 轻量化编译.

新版 Natasha 将编译部分分为 Natasha.CSharp.Compiler 与 Natasha.CSharp.Template.

  • Natasha.CSharp.Compiler 为编译部分,主要提供编译单元/引用管理/Using管理等编译所需的基本功能。

  • Natasha.CSharp.Template 为模板部分,主要提供简单方便的程序集代码构建功能。

  • Natasha 支持单域定制化.

    • 单域管理引用
    • 单域屏蔽相关检查
    • 屏蔽主域加载
    • 单域 using 覆盖

@NMSAzulX
Copy link
Collaborator Author

NMSAzulX commented Apr 28, 2023

4. C# 12 支持.

@NMSAzulX
Copy link
Collaborator Author

NMSAzulX commented Apr 28, 2023

5. 便捷委托 与 动态计算脚本.

便捷委托需要用到方法反解器,反解器属于模板模块内容,不符合轻量化路线,会增加编译单元的耦合,因此,便捷委托仍然在模板中实现。

动态计算脚本新项目:

dotnet/roslyn#8064

  • 定制化编译:
    • 单域编译
    • 数学计算相关元数据覆盖
    • 数学计算相关 Using 覆盖
    • 动态查询?
    • 方便的扩展

@NMSAzulX
Copy link
Collaborator Author

NMSAzulX commented Apr 28, 2023

6. 脚本 Debug 调试

API 设计:

public AssemblyCSharpBuilder WithDebugCompile(Action<DebugOutput>? action = null)
    /// <summary>
    /// 采用文件加载方式搜集源代码信息
    /// </summary>
    /// <returns></returns>
    public DebugOutput WriteToFile()
    {
        _informationFormat = DebugInformationFormat.PortablePdb;
        return this;
    }
    /// <summary>
    /// 将 Pdb 输出到程序集中,并直接从程序集加载 Pdb 调试信息
    /// </summary>
    /// <returns></returns>
    public DebugOutput WriteToAssembly()
    {
        _informationFormat = DebugInformationFormat.Embedded;
        return this;
    }

@NMSAzulX
Copy link
Collaborator Author

NMSAzulX commented Nov 28, 2023

7. 程序集合并(模块加载).

API 设计:

var assembly1 = builder1.GetAssembly();

var asssembly2 = builder2.GetAssembly(assembly1);

Assert.Equl(assembly1,assembly2);

再找找其他方案.
image

.NET Core 起已经不再支持多模块程序集了。

LoadModule is used to load multi-module assemblies. Multi-module assemblies are not supported by .NET Core.

方案2 热重载:

需要长期调研,无开发文档,可参考代码过于复杂。

@NMSAzulX NMSAzulX added the dependencies 有依赖需要升级 label Dec 1, 2023
@NMSAzulX NMSAzulX added the done label Dec 24, 2023
@NMSAzulX NMSAzulX changed the title [Feature]: Natasha 轻量化计划 [Next]: Natasha 轻量化计划 Jan 1, 2024
@NMSAzulX NMSAzulX changed the title [Next]: Natasha 轻量化计划 [Next]: Natasha8.0 【轻量化】计划 Jan 1, 2024
@NMSAzulX NMSAzulX added this to the v8.0.0.0 milestone Jan 6, 2024
@NMSAzulX NMSAzulX unpinned this issue Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies 有依赖需要升级 done enhancement New feature or request.
Projects
No open projects
Status: In Progress
Development

No branches or pull requests

3 participants