actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

Prefer cooperative compression over offload to blocking pool.

Open mstyura opened this issue 2 years ago • 0 comments

PR Type

Refactor

PR Checklist

  • [x] Tests for the changes have been added / updated.
  • [x] Documentation comments have been added / updated.
  • [x] A changelog entry has been made for the appropriate packages.
  • [x] Format code with the latest stable rustfmt.
  • [ ] (Team) Label with affected crates and semver status.

Overview

The current implementation of compression middleware offloads compression of chunks larger than 1024 bytes to a blocking pool. Presumably, this was done to avoid preventing other requests from being served, as no other future can make progress due to the CPU-bound operation that compression is.

Threads from the blocking pool do not provide more computational resources than what is already available to runtime polling futures. Therefore, it makes sense to perform compression in chunks by yielding control from the compression future to other futures, allowing everyone to make progress.

I've also observed uncontrollable thread count growth when benchmarking a real-world application with compression of JSON responses enabled: image image

mstyura avatar Sep 30 '23 17:09 mstyura