The misallocation of resources is endemic to imperfect competition. We generalize the Spence-Dixit-Stiglitz framework to heterogeneous ﬁrms, addressing when the market provides optimal quantities, variety and productivity. This yields several insights. First, constant elasticity of substitution demand ensures market allocations are efﬁcient, despite differences in ﬁrm productivity. Second, when demand elasticity varies, allocations reﬂect the distortions of imperfect competition. Firm heterogeneity matters for distortions: some ﬁrms over-produce while others underproduce, and the pattern of misallocation is determined by two demand-side elasticities. Third, market imperfections derive from insufﬁcient competition. We show market expansion increases welfare when private and social incentives are aligned, and efﬁciency is obtained in large markets.