The kernel under WebAssembly: 1.26 MB Brotli over the bare runtime, and 0.2 s to the first Simplify

未关闭
#2,296 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
半天
新手友好度
48/100
Issue 类型
文档
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp, wasm

调研方向

The change is in the WebAssembly section of the trimming document (§3), specifically the entry listing "the payload under Blazor WebAssembly" under Not measured. Read that section first, then the probe in the collapsed details block: trimmed.csproj (with the ProjectReference to Sources/AngouriMath/AngouriMath.csproj), Program.cs, and wwwroot/main.js. Done means the entry is replaced by a claim whose figures match the payload tables, and the probe can be rebuilt to reproduce them.

由索引模型根据 Issue 内容生成。

描述

What you ran: three minimal Microsoft.NET.Sdk.WebAssembly apps on master 17cd9da (.NET SDK 10.0.402, dotnet publish -c Release, which here is PublishTrimmed=true, TrimMode=full, no AOT; InvariantGlobalization), differing only in the kernel:

  • baseline: no AngouriMath; the export returns its argument.
  • trimmed: a project reference to the kernel, pinned to net10.0 as §3 says (SetTargetFramework="TargetFramework=net10.0"), and one [JSExport] that parses a string and calls Simplify.
  • whole: the same, plus <TrimmerRootAssembly Include="AngouriMath" />, so every member of the kernel is kept.

What it did:

Payload, counting the files dotnet.js loads (the compressed columns are the publish output's .br and .gz siblings):

files raw Brotli gzip
baseline 7 4,441,302 B 1,399,500 B 1,722,759 B
trimmed 22 8,837,785 B 2,657,073 B 3,300,554 B
whole 25 9,421,934 B 2,807,807 B 3,491,046 B

The kernel adds 1,257,573 B Brotli trimmed, 1,408,307 B whole. Of the 15 files it adds, AngouriMath.wasm is 828,707 B Brotli (3,128,089 B raw); the rest are its four package dependencies (Antlr4.Runtime.Standard, GenericTensor, HonkSharp, Numbers) and ten framework assemblies, the largest System.Linq.Expressions at 359,489 B raw. None of the three publishes gives a trim warning, as IsAotCompatible promises.

Time, in Chromium on a Ryzen 7 9800X3D, served from 127.0.0.1 with Cache-Control: no-store so that every load fetches every file; the first visit and four reloads each, in ms:

runtime ready first Simplify second Simplify first answer, from navigation
baseline 42–77 — — 147, then 69–81
trimmed 68–96 193–257 105–135 406, then 288–319
whole 70–92 191–258 102–134 415, then 286–316

"Runtime ready" is dotnet.create() plus getAssemblyExports, from the start of the page's script. The first call is sin(x)^2 + cos(x)^2 → 1, the second (x^2 - 1) / (x - 1) → x + 1 provided not x - 1 = 0. So, locally, loading the kernel costs about 25 ms over the bare runtime, the first Simplify about 0.2 s and the next about 0.12 s, in the interpreter (AOT not measured). Keeping the kernel whole costs 150,734 B Brotli and no measurable time.

Over a network the bytes are the cost: at 10 Mbit/s, the kernel's 1,257,573 B are about 1.0 s of transfer (arithmetic, not measured). That bears on the question left open in #1692.

Through the name localhost (the server bound 127.0.0.1 only) the same trimmed page took 2.0 s on this machine instead of 0.3 s; the tables are from the address.

For the table already in §3: master's net10.0 AngouriMath.dll is 3,633,664 B, against 1,184,256 B for the 2.3.0 package measured there. The WebAssembly trimmer keeps 3,128,320 B of it for parse + Simplify (86.1%) and 3,617,792 B rooted, so the section's finding that parsing reaches nearly everything holds, more strongly than before.

What it should have done: §3 lists "the payload under Blazor WebAssembly" under Not measured, and therefore not claimed. With these numbers that entry can become a claim, for instance:

Under WebAssembly (Microsoft.NET.Sdk.WebAssembly, .NET 10, trimmed, no AOT) the kernel adds 1.26 MB Brotli to a 1.40 MB bare runtime, in 15 files of which AngouriMath.wasm is 0.83 MB, and the first Simplify takes about 0.2 s in the interpreter. Rooting the kernel whole adds 0.15 MB.

This was measured without Blazor. Blazor's own assemblies grow the baseline, and some of them are ones the kernel also uses, so the kernel's increment should be no larger there.

The probe apps

trimmed.csproj (baseline: no ProjectReference, no DefineConstants; whole: add <TrimmerRootAssembly Include="AngouriMath" />):

<Project Sdk="Microsoft.NET.Sdk.WebAssembly">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <OutputType>Exe</OutputType>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <InvariantGlobalization>true</InvariantGlobalization>
    <DefineConstants>$(DefineConstants);ANGOURI</DefineConstants>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\AngouriMath\Sources\AngouriMath\AngouriMath.csproj" SetTargetFramework="TargetFramework=net10.0" />
  </ItemGroup>
</Project>

Program.cs:

using System.Runtime.InteropServices.JavaScript;

return;

public static partial class Probe
{
    [JSExport]
    public static string Run(string s) =>
#if ANGOURI
        ((AngouriMath.Entity)s).Simplify().ToString();
#else
        s;
#endif
}

wwwroot/main.js, loaded as a module by a one-line index.html:

import { dotnet } from './_framework/dotnet.js';

const t0 = performance.now();
const runtime = await dotnet.create();
const exports = await runtime.getAssemblyExports(runtime.getConfig().mainAssemblyName);
const t1 = performance.now();
const first = exports.Probe.Run('sin(x)^2 + cos(x)^2');
const t2 = performance.now();
const second = exports.Probe.Run('(x^2 - 1) / (x - 1)');
const t3 = performance.now();
window.probe = { runtimeReady: t1 - t0, firstCall: t2 - t1, secondCall: t3 - t2, sinceNavigation: t2, first, second };
主要语言
C#
星标
831
派生
79
平均合并
1 小时 12 分钟
30 天内合并 PR
518

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ASC-Community/AngouriMath 的其他 Issue

查看 ASC-Community/AngouriMath 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。