jerch / xterm-addon-image

Image addon for xterm.js
MIT License
51 stars 6 forks source link

quick demo of complex serialization #48

Open jerch opened 1 year ago

jerch commented 1 year ago

Only playground branch atm...

Can be tested with:

Result should look like this: image

jerch commented 1 year ago

A more involved example with the line base serializer (still not using serialize addon, thus no FG/BG attributes):

image

exec in console after running command in terminal (grr4 == output of imgcat palette.png):

ia=term._addonManager._addons[5].instance;
res = ia.serialize(0,8);
term.write('\r\n\r\n\r\nfrom serialize:\r\n'+res.join('\r\n'))
jerch commented 1 year ago

While the line based serialization works as expected, it is still pretty wasteful, as it creates a full line image, if there is at least one cell tile. This has bad impact on the fifo buffer when reading back. Furthermore it duplicates most of ImageStorage.render and ImageRenderer.draw.

A better way to deal with things here might be to create a method extractCanvasAtBufferRange and let it work on multiple lines at once. This way render and draw is just a special buffer range case, where it points to the active viewport. Also single line extraction for serialization becomes just a special case, where we can apply left&right truncation rules on top saving precious fifo buffer space later on.

jerch commented 1 year ago

Added the QOI image format temporarily to IIP sequence, as it promises huge performance gains for a lossless serialization format, while only being slightly worse in compression rate compared to PNG.

In my early tests decoding is 2-3x faster than PNG. Have yet to test the encoding path, but QOI itself states to be 30-50x faster during encoding than PNG.

Here one of the test images with alpha channel: image

jerch commented 1 year ago

The last commit makes it possible to directly compare PNG vs. QOI serialization: image

Meaning of the numbers:

To repro the numbers yourself:

In general QOI encoding gives a significant speedup over builtin PNG method. For different images the speedup is 2.5 - 5x, which is way lower than propagated by QOI (20-50x). The reason for the lower values is the fact, that the string serialization is highly dominated by other tasks as well (see profiler below):

image

TL;DR QOI serialization with custom encoders is 2.5 - 5 times faster than builtin PNG serialization.


Update: As with all data intensive profiling in JS - the runtime numbers above are slightly flawed from opened devtools. With closed devtools, QOI encoding finishes the test above in 250 ms, PNG encoding in 1100 ms, speedup is ~4.4x. So QOI is in fact a tiny bit faster than described here.

jerch commented 1 year ago

My earlier wasm serializer compared to serialize addon:

(Tested with ls-lR /usr output on 10k scrollbuffer at 87 cols width.)

The important entry is serialize (highlighted above). It shows a whopping speed difference of ~10x (and in fact it is much higher at ~20x when looking only at the real impl under wasm-function[2], but half of the speedup gets eaten from data transfers).

The wasm text buffer serializer still needs cleanup and a few more config options, before we can look into a proper way to integrate image serialization as well.