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

オープン
#2,296 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
518

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ASC-Community/AngouriMath のほかの issue

ASC-Community/AngouriMath の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。