Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Performance

generate:site on the full export (9 languages × 1042 books, 9387 book files) takes ~4.3s with dotnet run (JIT) on a 16-core machine, best of 5 runs. Most of the work is spread over every core: the process uses ~38s of CPU time in that run. A published Native AOT binary skips JIT compilation, which a one-shot batch CLI pays on every run; publish it for any real generation run (see “Development” in the top-level README.md).

Parallelism

Each book is cheap: read its JSON file, render its page, write the HTML file. What matters is that they overlap. LanguageSiteGenerator.GenerateAsync processes a language’s books concurrently with Parallel.ForEachAsync; its doc comment explains how output order and the file name collision suffixes stay deterministic. TemplateEngine’s template cache is a ConcurrentDictionary: Scriban’s parsed Template is immutable and safe to render from several threads against separate TemplateContexts, the cache holding it has to be too.

The degree of parallelism is Environment.ProcessorCount * 4. The workload mixes CPU work (JSON deserialization, Scriban rendering) with small-file disk I/O, so running more tasks than cores lets tasks blocked on I/O overlap with CPU-bound ones. On the 16-core machine, ProcessorCount alone leaves cores idle, and ProcessorCount * 16 only adds scheduling overhead.

Hot paths

Each choice below was found by timing one language’s 1042 books phase by phase (Stopwatch around each phase), not guessed.

  • Locations are rendered in C#, not in Scriban. Scriban’s interpreter overhead, not the formatting itself, dominates rendering. One language has 178,872 locations (~1.6 million across all 9), and calling four custom functions per location through Scriban’s reflection-based DynamicCustomFunction dispatch costs ~2.4ms per book. Rendering/Pages/BookPage/LocationsHtmlBuilder.Build builds the whole <ul class="locations">...</ul> fragment in a plain C# loop with the same TemplateFunctions methods (same escaping, same output), ~0.18ms per book, and book_page.sbn inserts the string as is.
  • The book JSON files are read synchronously. BookExportReader.ReadIndexAsync/ReadBookAsync use File.ReadAllBytes and JsonSerializer.Deserialize. Driving a FileStream not opened with FileOptions.Asynchronous through JsonSerializer.DeserializeAsync is “async over sync”: the thread-pool reads and extra state machines cost ~30× the synchronous read for the same files. The methods still return a Task: they run inside a bounded Parallel.ForEachAsync worker, where a short synchronous read blocks nothing else.
  • Book markup is scanned by hand. BookContentSanitizer (book markup to HTML, see Bethesda markup handling) uses no System.Text.RegularExpressions: it walks the content in one forward scan with IndexOf and ReadOnlySpan<char>. Every pattern it needs (the next </> pair, a tag name compared to a fixed word, one attribute’s value) is simple enough to scan directly, and regex engine overhead over every book of every language is a real cost.

PDF export

Printing PDFs dominates a generate:site --with-pdf run. Its figures and parallelism are on the PDF export page.