For a general open set, we characterize the compactness of the embedding for the homogeneous Sobolev space $\mathcal{D}^{1,p}_0\hookrightarrow L^q$ in terms of the summability of its torsion function. In particular, for $1\le q<p$ we obtain that the embedding is continuous if and only if it is compact.
The proofs crucially exploit a torsional Hardy inequality that we investigate in details.