Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- `compiler.Compile(outputFile, format)` and `compiler.CompileSvg(...)` no longer copy the rendered output onto the managed heap before writing or decoding it.
- Fonts are now discovered once per process for each font configuration, instead of once per `TypstCompiler`. Scanning the system fonts was most of what creating a compiler cost, so the static one-shot helpers such as `TypstCompiler.CompilePdf(source)` paid it on every call: with system fonts enabled, creating, compiling a short document and freeing measured 71 to 77 ms per call before and 1.4 to 1.8 ms after the first. Compilers with the same `Fonts` settings now also share the fonts they have loaded, rather than each holding a copy of every font it used. Two consequences: a font installed or added to a font directory after a configuration was first used is not seen until the process restarts, and the fonts a configuration has loaded stay in memory after its last compiler is disposed. `EvictCache()` does not release them.
- `sysInputs` parameters on `TypstCompiler` and the `SetSysInputs` argument are now `IDictionary<string, string>` instead of `Dictionary<string, string>`, so a `ReadOnlyDictionary<string, string>`, `ImmutableDictionary<string, string>` or any other implementation can be passed. Existing calls that pass a `Dictionary<string, string>` are unaffected.
- The native library is now built with the release settings Typst itself uses, thin LTO and a single codegen unit, which makes it about 6% smaller: the Windows x64 `typst_core.dll` measured 51.6 MB before and 48.3 MB after. Compile speed is unchanged within measurement noise.
- The native library is now built with size optimisation (`opt-level = "z"`), thin LTO (`lto = "thin"`), a single codegen unit, symbol stripping (`strip = "symbols"`), and single-target crate compilation (`--crate-type cdylib` / `staticlib`, with Rust mangled symbol hiding and stripping on Linux musl), so that LTO runs across the dependency graph and unreferenced Rust symbols and DWARF debug sections are stripped. The Linux `libtypst_core.so` drops from ~59 MB (and ~90–100 MB on musl / MinGW in the NuGet package) down to ~47–49 MB (~20 MB compressed in the `.nupkg`).

### Added
- Added `TypstCompiler.EvictCache()` to clear and release the memory held by Typst's incremental compilation cache. The cache is shared by the whole process and was only ever trimmed to what the last ten compilations used, so an application that compiled one large document kept memory in proportion to it for the next ten compilations, and could not release it any sooner. The cache is process-wide, so clearing it makes the next compilation of every compiler start cold; it is safe while other threads compile, and leaves compiled documents valid. The packaged Linux library uses musl, which reuses the freed memory rather than returning it to the operating system, and a single compilation's peak is unchanged, so a very large document still has to be split to bound that.
Expand Down
39 changes: 0 additions & 39 deletions src/typst_core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 7 additions & 16 deletions src/typst_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ version = "0.0.1"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib", "staticlib"]
crate-type = ["cdylib", "rlib"]

[dependencies]
chrono = { version = "0.4.41", default-features = false, features = [
"clock",
"std",
] }
codespan-reporting = "0.12"
comemo = "0.5.0"
ecow = "0.2"
pathdiff = "0.2"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.9"
typst = "0.15.1"
typst-kit = { version = "0.15.1", features = [
"system-packages",
Expand All @@ -30,18 +22,17 @@ typst-kit = { version = "0.15.1", features = [
typst-pdf = "0.15.1"
typst-svg = "0.15.1"
typst-render = "0.15.1"
typst-eval = "0.15.1"
typst-html = "0.15.1"
typst-layout = "0.15.1"

[build-dependencies]
csbindgen = "1.9.7"

# The settings Typst itself is released with. A single codegen unit and thin LTO let
# the optimiser see across crate boundaries and drop what the exported functions never
# reach, which makes the native library about 6% smaller. Speed is within measurement
# noise. The cost is a slower release build. `panic` must stay at its default,
# `unwind`: `compile` catches panics so that they never cross the C ABI.
# Optimise for binary size, strip symbols and debug sections, and run thin LTO across a
# single codegen unit so that dead code across the dependency graph is dropped without
# excessive link-time memory use. `panic` must stay at its default, `unwind`: `compile`
# catches panics so that they never cross the C ABI.
[profile.release]
opt-level = "z"
lto = "thin"
codegen-units = 1
strip = "symbols"
5 changes: 5 additions & 0 deletions src/typst_core/exports.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
local:
_R*;
_ZN*;
};
6 changes: 3 additions & 3 deletions src/typstsharp/typstsharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@
<Target Name="BuildRust" BeforeTargets="PrepareForBuild" DependsOnTargets="DetectMusl">
<Message Text="Building typst_core for %(RustTargetsToBuild.Identity) (%(RustTargetsToBuild.TargetTriple))" Importance="high" Condition="$([System.String]::Copy(';$(RustTargets);').Contains(';%(RustTargetsToBuild.Identity);'))" />
<Exec Command="rustup target add %(RustTargetsToBuild.TargetTriple)" Condition="$([System.String]::Copy(';$(RustTargets);').Contains(';%(RustTargetsToBuild.Identity);'))" />
<Exec Command="CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-linux-musl-gcc cargo build --release --manifest-path ../typst_core/Cargo.toml --target %(RustTargetsToBuild.TargetTriple)" WorkingDirectory="$(MSBuildThisFileDirectory)" Condition="'%(RustTargetsToBuild.TargetTriple)' == 'x86_64-unknown-linux-musl' and $([System.String]::Copy(';$(RustTargets);').Contains(';%(RustTargetsToBuild.Identity);'))" />
<Exec Command="x86_64-linux-musl-gcc -shared -o $(RustCoreDir)/target/%(RustTargetsToBuild.TargetTriple)/release/libtypst_core.so -Wl,--whole-archive $(RustCoreDir)/target/%(RustTargetsToBuild.TargetTriple)/release/libtypst_core.a -Wl,--no-whole-archive -static-libgcc -Wl,-Bstatic -lc -Wl,-z,noexecstack" WorkingDirectory="$(MSBuildThisFileDirectory)" Condition="'%(RustTargetsToBuild.TargetTriple)' == 'x86_64-unknown-linux-musl' and $([System.String]::Copy(';$(RustTargets);').Contains(';%(RustTargetsToBuild.Identity);'))" />
<Exec Command="cargo build --release --manifest-path ../typst_core/Cargo.toml --target %(RustTargetsToBuild.TargetTriple)" WorkingDirectory="$(MSBuildThisFileDirectory)" Condition="'%(RustTargetsToBuild.TargetTriple)' != 'x86_64-unknown-linux-musl' and $([System.String]::Copy(';$(RustTargets);').Contains(';%(RustTargetsToBuild.Identity);'))" />
<Exec Command="CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-linux-musl-gcc cargo rustc --release --manifest-path ../typst_core/Cargo.toml --target %(RustTargetsToBuild.TargetTriple) --crate-type staticlib" WorkingDirectory="$(MSBuildThisFileDirectory)" Condition="'%(RustTargetsToBuild.TargetTriple)' == 'x86_64-unknown-linux-musl' and $([System.String]::Copy(';$(RustTargets);').Contains(';%(RustTargetsToBuild.Identity);'))" />
<Exec Command="x86_64-linux-musl-gcc -shared -o $(RustCoreDir)/target/%(RustTargetsToBuild.TargetTriple)/release/libtypst_core.so.tmp.$$ -Wl,--version-script=$(RustCoreDir)/exports.map -Wl,--whole-archive $(RustCoreDir)/target/%(RustTargetsToBuild.TargetTriple)/release/libtypst_core.a -Wl,--no-whole-archive -Wl,-s -static-libgcc -Wl,-Bstatic -lc -Wl,-z,noexecstack &amp;&amp; mv -f $(RustCoreDir)/target/%(RustTargetsToBuild.TargetTriple)/release/libtypst_core.so.tmp.$$ $(RustCoreDir)/target/%(RustTargetsToBuild.TargetTriple)/release/libtypst_core.so" WorkingDirectory="$(MSBuildThisFileDirectory)" Condition="'%(RustTargetsToBuild.TargetTriple)' == 'x86_64-unknown-linux-musl' and $([System.String]::Copy(';$(RustTargets);').Contains(';%(RustTargetsToBuild.Identity);'))" />
<Exec Command="cargo rustc --release --manifest-path ../typst_core/Cargo.toml --target %(RustTargetsToBuild.TargetTriple) --crate-type cdylib" WorkingDirectory="$(MSBuildThisFileDirectory)" Condition="'%(RustTargetsToBuild.TargetTriple)' != 'x86_64-unknown-linux-musl' and $([System.String]::Copy(';$(RustTargets);').Contains(';%(RustTargetsToBuild.Identity);'))" />
</Target>

<Target Name="CollectRustNativeLibraries" DependsOnTargets="BuildRust" AfterTargets="BuildRust">
Expand Down
Loading