evanw / thumbhash

A very compact representation of an image placeholder
https://evanw.github.io/thumbhash/
MIT License
3.49k stars 69 forks source link

Performance/speed comparison with BlurHash #4

Closed ToshY closed 1 year ago

ToshY commented 1 year ago

The documentation/demo states the following:

Much faster to encode and decode

Howmuch faster compared to BlurHash? If you've done tests to compare performance/speed between ThumbHash and BlurHash, it would be nice to show these results.

evanw commented 1 year ago

I should probably remove that part of the website. I don't want that to be the focus of this. Partially because the performance doesn't matter too much (encode typically happens once and is stored, and decode is pretty trivial), partially because optimal performance isn't even a goal (it'd be pretty trivial to speed this up even more with SIMD or a hand-rolled cos implementation, but that goes against the goal of having simple code), and partially because BlurHash is slow for uninteresting reasons.

Some data points to answer your question, since I should still back it up (1 μs is a microsecond, which is 0.000001 seconds):

One big reason BlurHash is currently slower is that their sRGBToLinear function should really be implemented as a lookup table but isn't. That's a 2x speedup to BlurHash right there. I'm sure there are other easy wins too. I wouldn't be surprised if it's possible to make a BlurHash implementation that's just as fast as (or even faster than) ThumbHash since they are doing pretty similar things.

Another reason BlurHash's Swift implementation is so slow in debug builds is that they are using for-in loops instead of while loops (see e.g. https://github.com/woltapp/blurhash/pull/215). This is a really dumb performance issue with the current Swift compiler implementation. You could argue that debug performance doesn't matter and that readability should be prioritized as long as release performance is good, so maybe BlurHash shouldn't even be changed. Personally I value quick iteration cycles with debug builds so I have tuned ThumbHash's code for that.