go-text / typesetting

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

shaping: add lru font cache and benchmark impact #56

Closed whereswaldon closed 1 year ago

whereswaldon commented 1 year ago

This commit restructures the font cache within the HarfbuzzShaper type to function as an LRU cache with a configurable size, and it also updates the shaping benchmark to measure the performance impact of the cache. For small bodies of text, initial benchmark results suggest substantial performance improvements for complex scripts (and complex fonts) and marginal gains for simpler scripts like latin.

$ go test -run '^$' -bench Shaping -benchmem ./shaping/ goos: linux
goarch: amd64
pkg: github.com/go-text/typesetting/shaping
cpu: Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
BenchmarkShaping/10runes-arabic-0fontCache-8                7610            152486 ns/op           70570 B/op       1363 allocs/op
BenchmarkShaping/10runes-arabic-5fontCache-8               40285             29687 ns/op            7418 B/op        140 allocs/op
BenchmarkShaping/100runes-arabic-0fontCache-8               2629            445300 ns/op          139638 B/op       3339 allocs/op
BenchmarkShaping/100runes-arabic-5fontCache-8               3468            321211 ns/op           76501 B/op       2116 allocs/op
BenchmarkShaping/1000runes-arabic-0fontCache-8               357           3290018 ns/op          878659 B/op      24568 allocs/op
BenchmarkShaping/1000runes-arabic-5fontCache-8               375           3155476 ns/op          815639 B/op      23348 allocs/op
BenchmarkShaping/10runes-latin-0fontCache-8               436546              2617 ns/op            2288 B/op          9 allocs/op
BenchmarkShaping/10runes-latin-5fontCache-8               436644              2472 ns/op            2176 B/op          7 allocs/op
BenchmarkShaping/100runes-latin-0fontCache-8               71804             16676 ns/op            8176 B/op          9 allocs/op
BenchmarkShaping/100runes-latin-5fontCache-8               72498             16368 ns/op            8064 B/op          7 allocs/op
BenchmarkShaping/1000runes-latin-0fontCache-8               6877            158386 ns/op           67213 B/op          9 allocs/op
BenchmarkShaping/1000runes-latin-5fontCache-8               7297            156329 ns/op           67100 B/op          7 allocs/op
benoitkugler commented 1 year ago

Well this is awesome, thanks

whereswaldon commented 1 year ago

I'm going to go ahead and rebase+merge this with your approval, as that will add these changes to your existing PR. Then we can await @andydotxyz's feedback there.