Closed Bluebugs closed 1 year ago
@benoitkugler Can that type be reused? If so, it does seem like it would be worthwhile to add tunable LRU caching for it.
Yes, actually.
The heavy work in NewFont
only depends on the font content (which is immutable).
However, there are three fields which are set by the user and could change over shaping (even with the same font).
(By the way, it is somewhat the same duality as with font.Font/font.Face
)
So I think a solution could be to extract the immutable fields in a (private) struct, and to store a cache font.Font -> this_private_fields on the shaper.
I can take a look at this tomorrow
Harfbuzz shaper Shape() function does generate a large amount of memory allocation due to the call to harfbuzz.NewFont() at : https://github.com/go-text/typesetting/blob/main/shaping/shaper.go#L77 . I have done a quick/hacky local experiment by putting that font in a map cache and the resulting operation save significant amount of memory allocation and seems to also improve performance significantly.
Does anyone have an idea on what would be the proper approach to cache this font structure?