uWebSockets.js icon indicating copy to clipboard operation
uWebSockets.js copied to clipboard

Sending Pre-Compressed Buffers?

Open ronag opened this issue 1 year ago • 3 comments

Would it be possible to send pre-compressed data? i.e. if one caches messages one could cache the compressed version in order to avoid compressing during every send?

e.g.

Instead of:

cache.set(key, buf)
// ...
for (const socket of sockets) (  
  socket.send(cache.get(key)) // compressed by uws
}
cache.set(key, zlib.deflateSync(buf))
// ...
for (const socket of sockets) (
  socket.send(cache.get(key))
}

This would enable 2 things:

  • Re-use compression
  • Move compression to other thread

ronag avatar Jul 02 '24 06:07 ronag

If this is possible to implement I would consider funding the work.

ronag avatar Jul 02 '24 06:07 ronag

Yes it is possible. It was the behaviour in v0.14. But doing more investigation I found that, this was very poor for both compressive performance, and for CPU performance.

My first idea was that sharing a compressor would be best, but the cost of compressing is directly (linearly) correlated with the difference from what you have in your sliding window and what you are compressing.

This means that dedicated (per connection) compression is both faster and obviously compresses better.

If the difference is minimal, compression costs almost nothing. If the difference is total, it's slow as a snail.

Most who use compression prefers the dedicated compression. So the shared pub sub compression was removed (because of complexity and added work of keeping multiple variants of the outgoing data).

Adding something like a shared compressor (only) for pub sub is most likely doable

uNetworkingAB avatar Jul 12 '24 22:07 uNetworkingAB

+1

klauss194 avatar Aug 06 '24 23:08 klauss194