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

Đang mở
#2,296 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
Nửa ngày
Mức phù hợp với người mới
48/100
Loại issue
Tài liệu
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp, wasm
Lĩnh vực
documentation, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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 };
Ngôn ngữ chính
C#
Star
831
Fork
79
Merge trung bình
1 giờ 11 phút
Pull request đã merge (30 ngày)
523

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ASC-Community/AngouriMath

Tất cả issue của ASC-Community/AngouriMath

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.