Skip to content

Performance - Investigation & Tracking issue #2393

Closed

Description

@Dherse

Over the past few days since #2364 was merged, I have been investigating performance again and looking into how we can improve said performance. In this issue, I will be mentioning what I have found and how I have (on specific test documents) doubled the performance of cold compile times and tripled incremental compile performance. I will discuss each optimization, where they came from. For some of these issues I will be opening PRs over the coming days since they're mostly very small changes

Thanks to everybody that helped with this investigation 🎉

Summary

This is a summary of the changes I am suggesting to improve performance, explanations will be in follow up sections:

Details

I talk a lot about percentages, they are not relative to the current main version, but rather to one another, hence why they add up to more than 100%.

Adding an allocator

While it has been discussed at length of the desire of not adding C dependencies, it is my strong belief that an exception for the CLI only should be made to use a faster allocator. As it turns out, using mimalloc instead of the default allocator provides a 20% compile time decrease on Linux, 30% compile time decrease on Windows, these numbers are repeatable on different CPUs, the gains being bigger on an AMD CPU than an Intel one. These improvements also affect incremental compiles similarly.

Change .into_iter() to .iter()

Calling .into_iter() on an EcoVec that is shared (between the comemo cache and the call site) leads to a complete copy of the entire tree of values, changing it to an iter merely borrows the value, on my thesis that uses a rather large bibliography (around 170 entries), this saves around 20% of the incremental compile times.

SourceDiagnostic cloning

It appears that the SourceDiagnostic struct is being cloned a lot when calling functions. I suspect that this has to do with the delayed error system introduced in 378ebe5, since when using bib elements in locate blocks, it can cause errors to be thrown and somehow cloned. Investigating the source of this clone to eliminate it would be worth it. However, making the elements inside of SourceDiagnostic to EcoVecs mostly solves the issue as well.

Removed Box in SourceResult

The idea was to limit the size of the SourceResult however, in most cases, the value contained in the Ok(T) variant of the result is big enough that this does not matter, but it adds an extra level of indirections that makes cloning the error variants slower than it needs to be.

Hashed/Interned field names & labels

In query heavy documents, the constant string comparisons for field names and labels makes the document compilation far slower than it needs to be, this can be solved by interning these values or pre-hashing them. In my test, when pre-hashing them and using glossarium (or rather the original version I developed for my thesis), this can lead to a huge reduction in compile times and especially incremental times of 15%.

Cached is_justifiable

This function is used for correct CJK text shaping and justification, it is therefore crucial. However, since it is being called multiple times on each character in the text, it becomes incredibly slow. The solution, which also speeds up CJK text, is to simply add a is_justifiable: bool to ShapedGlyph to cache this value. This improves performance tremendously for both CJK and non-CJK text!

Query using hashed labels & fields

This goes with hashed/interned strings, pre-hashing strings before querying also help gaining lots of performance.

Synthesize bibliography elements

Currently the bibliography is re-loaded (although cached by comemo) every time, instead if could be synthesized onto the BibliographyElement fixing the issue completely.

Memoized PNG, SVG, and PDF export

If the frame does not change, PNG and SVG exports don't need to change either. Ideally we would not even write the frame again, in my testing this allows already excellent gains. For PDF, I have only tested caching the compression of content streams, however, rendering frames to a PDF Chunk could further improve this by reusing an entire page's worth of content. This is only useful for incremental compiles, but it does provide about a 30% improvement on my thesis simply be removing the need to re-write/re-compress everything.

Additional query selector

This is specific to glossarium, and likely other packages that are query heavy: by adding the .rev(), .first(), and .last() selector, it makes it possible to optimize queries and decrease the amount of cloning being performance, as well as the number of nodes being checked. I have not yet tested the performance improvement of this, but I know that it will improve, I just need to comprehensively test it.

Conclusion

With these changes, as well as a couple of improvements to comemo, I was able to take the compile time of my thesis from 14.6s down to 6s, and incremental compilations from 2.9s down to just 600ms. On another 2.5k pages long document, it brought compile times down from 2m down to 1m30s, with incremental going from 30s to 15s. These gains are incredible for the little amount of code required to obtain them.

These results are reflected in the number of instructions being executed being halved for my thesis with no changes to either the Typst test suites, nor my thesis' output.

Activity

  1. added
    cli
    incremental
    tracking-issue
    perf
    on Oct 15, 2023
  2. Dherse commented on Oct 17, 2023

    @Dherse
    ContributorAuthor

    Update

    As of #2420 here are the improvements including the latests changes relative to a596663 (before I began). These tests are run on two machines: an Intel i9 12900k running Ubuntu, and an AMD 7950X3D running Windows. The following table shows a series of tests (I can't be bothered to explained the changes I am doing in my thesis just know that they're always the same) performed on both platforms, and for the 7950X3D executed on both: a core with X3D enabled and a core without.

    The tests are as follows: cold compile, 4x an edit and its undo (each edit is different: image PNG, outline, glossary, page break), as well as a test on a document from @Enter-tainer that has 2000 pages: cold compile, an edit and its undo. The tests are ran for each export format: PDF, PNG, and SVG.

    As I am collecting these by hands, the number of samples are fairly low, sorry not sorry, I value my sanity, unfortunately my benchmarking harness cannot test typst w :(

    For PDF I write to files to nul on Windows and /dev/null on Linux to try and remove I/O operations from the measurement as much as possible, all tests on the R9 7950X3D are executed on the same NVME SSD (PCIe 4.0 x4), for the Linux box I forgot what the drive is, I think it's a PCIe 3.0 x4 NVME SSD but I really don't remember. To try and keep the results as consistent as possible, the processes are always spawned on the same cores, with the highest priority.

    Please note that I had to do this by hand and it took hours, if scientific rigour isn't there 100% (looking at you @dccsillag) then so be it 🤷‍♂️ It took ten samples of each except for @Enter-tainer 2.5k pages long document where I limited myself to three samples.

    (other results will come in different posts because I need sleep)

    PDF results

    • 7950x3D: regardless of cache, a 30% improvement in cold compile time, and a 10% improvement in incremental time for my thesis, and a 20% improvement in cold compile time for @Enter-tainer's doc, and a 5% with 3D-VCache and 20% improvement without for incremental compilation.
    • 12900k: TODO

    PNG results

    TODO

    SVG results

    TODO

    Conclusion

    While I am not done taking all of the measurements, I have been testing extensively lately and I can say: for these first 4 changes, we have already gained a huge amount of performance basically "for free" (the complexity hasn't increase either, nor as memory consumption in any significant way). The next steps involve interning/pre-hashing and a better allocator. With these things we'll almost be there!

  3. removed
    cli
    on Nov 14, 2023
  4. laurmaedje commented on Jul 16, 2024

    @laurmaedje
    Member

    I think most of the stuff described in the issue has landed, so we can close this tracking issue as completed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    incrementalperftracking-issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions