This patch implements the idea in #17515 (closed)
Needs a bit of documentation, but otherwise ready to review