#Make formatting function less expensive

27 messages · Page 1 of 1 (latest)

pearl axle
#

Hello,

I have a math document that I'm working on, which is getting to a sizeable length. It has alot of math interspersed with text. Currently, it's about 100 pages.

I'm using it within VS Code on Windows, using Tinymist.

The delay between typing and seeing a preview is getting noticeably long. Sometimes, it can take 5 seconds for the preview to update. It's usually in the range of 1-5 seconds to update. It's not terrible, but it's not snappy, either. I'm not sure why its such a big range between the low and high of the update interval.

There's one function that I've identified that's eating up a bunch of the processing time. When I comment it out, it cuts the update delay by about 50%. But, I need the functionality that this function provides because it's the main formatting function for the math typesetting, so it's not realistic to remove it.

This is the function here:

<message too long for discord: the rest as a comments>

#
show math.equation: it => {
    show regex("[0-9]+"):  it2 => text(kThemeColors.MathNumber)[#it2]
    show regex("\+|⋅|−|÷"): it2 => text(kThemeColors.MathOp)[#it2]
    show regex("\{|\}"):   it2 => text(kThemeColors.Braces)[#it2]
    show regex("\(|\)"):   it2 => text(kThemeColors.Parenthesis)[#it2]

    // Same color as {}. Useful for defining sets.
    show "|": it2 => text(kThemeColors.Braces)[#it2]

    // Display = and ≠ signs with a different color.
    show "=" : it => text(fill:kThemeColors.EqualSign)[#it]        
    show "≠" : it => text(fill:kThemeColors.EqualSign)[#it]
    show "≔" : it => text(fill:kThemeColors.EqualSign)[#it]        
    show "," : it => text(fill:kThemeColors.Comma)[#it]
    show ";" : it => text(fill:kThemeColors.Comma)[#it]
    show "⋯" : it => text(fill:kThemeColors.Comma)[#it]
    show "…" : it => text(fill:kThemeColors.Comma)[#it]
    
    // Display Greek letters as normal, but display other math font
    // as non-serif, non-italic.
    show regex("\p{Greek}+"): it => math.serif(it)
    math.upright(math.sans(it))                
}
#

I wish that I could accomplish what the function is doing, but in a less computationally expensive way. So, I have some feedback:

  • For entries like show regex("\{|\}"):, it would be nice if the language supported a simple array of entries to match against, so that the underlying engine doesn't have to run a full regex for simple matches. Maybe syntax like show ("{", "}"):
  • I want the default math to be normal, non-italic, sans-serif. It would be nice if there was a builtin setting for the math font to set these defaults, so that there wasn't a need to transform every math entry via a call like math.upright(math.sans(it)).
  • Because of the call to math.upright(math.sans(it)), for some reason that messes up the default Greek letter display; they display as bold, so that requires a fixeup in the form of another regex for Greek letters to undo the boldness that happens.
  • I'm not sure if the function performs those dictionary lookups every time it runs, like kThemeColors.Comma. If applicable, the engine could look up those values once, and cache them to avoid subsequent lookups.

The silver lining is that the function gets the job done; it gives me the formatting that I want. So, it accomplishes its task correctly. That's the most important thing. And so this post is about feedback for QOL enhancements related to compilation speed.

Thanks

warm magnet
#

not sure if thats the problem here, but something that would surely help is replacing show regex("[0-9]+"): it2 => text(kThemeColors.MathNumber)[#it2] with show regex("[0-9]+"): set text(kThemeColors.MathNumber) and the like

#

if you need more information on speed you can use --timings file.json when compiling

#

and then open file.json in perfetto web to see whats going on in more detail

#

regarding the regex for braces, you can just split it up into multiple rules if thats a concern

#

there is selector.or(a, b)

#

however it does not work with regex or strings

hot galleon
#

Are you running 0.13.1 or the development version?

#

The latter may improve performance somewhat

pearl axle
pearl axle
fierce basin
#

Besides changing show-function to show-set, have you looked into merging the rules for same colours into a same regex? I don't know if the regex overhead wouldn't make it worse, but might be worth trying

#

I believe there are also fonts with sans math by default (e.g. new computer modern sans math) but you might have to install them

pearl axle
#

There's not really alot to merge, besides the "|", which I just did. I had it separate cause I was playing with that particular color.
For the others, the equalsign and comma, its a choice between regex or individual matches. I decided to keep those as separate cause it doesn't seem that it would be a big different in speed between a few individual matches and a regex. If anything, I would expect the individual matches to be faster.

#

For the font, I just tried installing that one to check it out, but I can't get my setup to recognise the installed font. I tried "NewComputerModernSansMath" and "NewComputerModernSansMath-Regular", etc, but it just says "Unknown font family".

#

But, I use "STIX Two Math", and would likely want to remain on it because that font is very complete in its support for many characters.

#

Would still like to check out New Computer Mordern, though.

#

So, thanks for the suggestion.

hot galleon
#

though I haven't benchmarked it myself

pearl axle
#

I'll try it out, Evinex.

fierce basin
#

I believe new computer modern is also very complete

pearl axle
#

I tried "New Computer Modern Sans Math" just now, and it did work.
Reason I didn't try it is because every where I looked at the font details, it never shows up without spaces. So, didn't occurred to me to even try it (even though, in retrospect, is obvious that I should have).
Thanks for noting that.

tepid rapids
#

run typst font to see typst's idea of which fonts are installed and their names