Mc-Zen / zero

Advanced scientific number formatting for Typst.
https://typst.app/universe/package/zero
MIT License
30 stars 2 forks source link

[optimize] `num` and `ztable` #6

Closed Mc-Zen closed 1 month ago

Mc-Zen commented 1 month ago

Especially in combination with state.get(), show rules on table cells can get slow. This PR makes a series of optimizations that considerably improve run times for both the num() function and alignment of numerals in tables.

Some ideas that do not (currently) lead to an improved runtime:

Benchmarking results for num

Exemplary code (for most cases)

#set page(height: auto, width: auto)
#import "src/zero.typ": *
#grid(columns: 10, ..range(int(sys.inputs.n)).map(i => str(i/8)).map(num))

Code for manually specifying the state

#set page(height: auto, width: auto)
#import "src/zero.typ": *
#let state = default-state
#{state.decimal-separator = ","}
#let num = num.with(state: state)

#grid(columns: 10, ..range(int(sys.inputs.n)).map(i => str(i/8)).map(num))

image

Benchmarking results for ztable:

#set page(height: auto, width: auto)
#import "src/zero.typ": *
#ztable(stroke: none, columns: 10, format: (auto,) * 10, ..range(int(sys.inputs.n)).map(i => str(i/8)))

image

With the new version, the runtime for ztable now seems to scale linearly with the number of num objects inside on a large scale. image