harfbuzz / harfbuzzjs

Providing HarfBuzz shaping library for client/server side JavaScript projects
https://harfbuzz.github.io/harfbuzzjs/
Other
204 stars 35 forks source link

Re-implement callbacks in JS #97

Closed khaledhosny closed 3 months ago

khaledhosny commented 6 months ago

This paves the way to passing callback functions from JS to wasm and serves as an example.

behdad commented 6 months ago

Any perf numbers?

khaledhosny commented 6 months ago

There does not seem to be any measurable difference. Drawing the same glyph a million times:

old:

real    0m10.935s
user    0m10.871s
sys 0m0.242s

new:

real    0m10.954s
user    0m10.875s
sys 0m0.256s

Drawing all glyphs in NotoSans-Regular a 100 times:

old:

real    0m2.510s
user    0m2.509s
sys 0m0.073s

new:

real    0m2.522s
user    0m2.522s
sys 0m0.078s
behdad commented 6 months ago

That's quite impressive!

chearon commented 6 months ago

That's consistent with what I've found. I did a lot of profiling of C++ map, string, and array operations vs JS equivalents, and JS was at worst the same. String concatenation was always faster in JS. Though one thing nice about C++ is you don't have the garbage collector running, which can take seconds when you're working with huge amounts of memory.

This looks great! I did not know about Module.addFunction. I can make a PR for <canvas> support, it's not hard after this.


A little off-topic (shameless plug?) but I moved my harfbuzzjs efforts to here. One thing I did to get hbjs working in a production environment was put the glyph infos, positions and flags in a flattened Int32Array (so like an array of structs in C) instead of the JS objects this currently uses. That was key in getting good perf for shaping massive amounts of text. I also added OpenType functions used for font selection, and necessary feature/lookup functions for porting Firefox's glyph cache. Hopefully I can contribute that stuff back, but it was difficult to have WASM bundles for every dependency. TypeScript in particular would be really nice to have here.

behdad commented 6 months ago

Thanks for sharing. I don't think TypeScript is a problem.

khaledhosny commented 3 months ago

Any objection to merging this?

behdad commented 3 months ago

Go ahead!