Deduplicate TLS

WIP

see #6024

/cc @bnoordhuis