Skip to content

Commit

Permalink
Merge pull request #61 from furesoft/master
Browse files Browse the repository at this point in the history
[WIP] Add Properties To Optimize The WASM binary Size per default
  • Loading branch information
mhmd-azeez authored Feb 18, 2024
2 parents 1ba2cc1 + 7570d92 commit 0efb990
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 38 deletions.
23 changes: 2 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,26 +506,7 @@ This is useful when you want to provide a common set of imports and exports that

### Optimize Size

Normally, the .NET runtime is very conservative when trimming and includes a lot of metadata for debugging and exception purposes. This makes sure code doesn't break (when using reflection for example) but it also means large binary sizes. A hello world sample is about 20mb. To instruct the .NET compiler to be aggresive about trimming, you can try out these options:
```xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<WasmBuildNative>true</WasmBuildNative>
<WasmSingleFileBundle>true</WasmSingleFileBundle>

<!-- Note: TrimMode Full breaks Extism's global exception handling hook -->
<TrimMode>partial</TrimMode>
<DebuggerSupport>false</DebuggerSupport>
<EventSourceSupport>false</EventSourceSupport>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<NativeDebugSymbols>false</NativeDebugSymbols>
</PropertyGroup>
</Project>
```
Normally, the .NET runtime is very conservative when trimming and includes a lot of metadata for debugging and exception purposes. We have enabled some options in Release mode by default that would make the resulting binary smaller (6mb for a hello world sample vs 20mb in debug mode).

If you have imports in referenced assemblies, make sure [you mark them as roots](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-7-0#root-assemblies) so that they don't get trimmed:
```xml
Expand All @@ -539,7 +520,7 @@ And then, run:
dotnet publish -c Release
```

Now, you'll have a significantly smaller `.wasm` file in `bin\Release\net8.0\wasi-wasm\AppBundle`.
Now, you'll have a smaller `.wasm` file in `bin\Release\net8.0\wasi-wasm\AppBundle`.

For more details, refer to [the official documentation](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-7-0#trimming-framework-library-features).

Expand Down
10 changes: 0 additions & 10 deletions samples/KitchenSink/KitchenSink.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,9 @@
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<WasmSingleFileBundle>true</WasmSingleFileBundle>
<WasmBuildNative>true</WasmBuildNative>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TrimmerSingleWarn>false</TrimmerSingleWarn>

<!-- Note: TrimMode Full breaks Extism's global exception handling hook -->
<TrimMode>partial</TrimMode>
<DebuggerSupport>false</DebuggerSupport>
<EventSourceSupport>false</EventSourceSupport>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<NativeDebugSymbols>false</NativeDebugSymbols>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions samples/SampleCSharpPlugin/SampleCSharpPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<WasmSingleFileBundle>true</WasmSingleFileBundle>
<WasmBuildNative>true</WasmBuildNative>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
3 changes: 0 additions & 3 deletions samples/SampleFSharpPlugin/SampleFSharpPlugin.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<WasmSingleFileBundle>true</WasmSingleFileBundle>
<WasmBuildNative>true</WasmBuildNative>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
15 changes: 14 additions & 1 deletion src/Extism.Pdk/build/Extism.Pdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
<TrimmerRootAssembly Include="Extism.Pdk" />
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<PublishTrimmed>true</PublishTrimmed>
<WasmBuildNative>true</WasmBuildNative>
<WasmSingleFileBundle>true</WasmSingleFileBundle>

<!-- Note: TrimMode Full breaks Extism's global exception handling hook -->
<TrimMode>partial</TrimMode>
<DebuggerSupport>false</DebuggerSupport>
<EventSourceSupport>false</EventSourceSupport>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<NativeDebugSymbols>false</NativeDebugSymbols>
</PropertyGroup>

<UsingTask TaskName="GenerateFFITask" AssemblyFile="$(MSBuildThisFileDirectory)..\build\Extism.Pdk.MSBuild.dll" Condition="'$(RuntimeIdentifier)' == 'wasi-wasm'"/>
<Target Name="GenerateGlueCode" AfterTargets="Build" BeforeTargets="_BeforeWasmBuildApp" Condition="'$(RuntimeIdentifier)' == 'wasi-wasm'">
<GenerateFFITask AssemblyPath="$(TargetPath)" OutputPath="$(IntermediateOutputPath)extism" ExtismPath="$(MSBuildThisFileDirectory)..\native\extism.c" />
Expand All @@ -18,4 +31,4 @@
<_WasiSdkClangArgs Include="-Wl,--wrap=mono_wasm_load_runtime" />
</ItemGroup>
</Target>
</Project>
</Project>

0 comments on commit 0efb990

Please sign in to comment.