go-text / typesetting

High quality text shaping in pure Go.
Other
88 stars 11 forks source link

High memory allocation #53

Closed Bluebugs closed 1 year ago

Bluebugs commented 1 year ago

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?

whereswaldon commented 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.

benoitkugler commented 1 year ago

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