You don't need to optimise for the market. The market is the optimising machine. Get in its way with slow regulators or subsidies or bailouts and you get all the problems.
Why would this even be so. It's not magic it's just people doing stupid people things often to maximize short term factors. It only gets long-term planning insofar as embedded agents do. One of the things agents do is use incentives to inspire behaviour that is conta individuals short term incentives to achieve behaviour that contributes to long-term success.
Not only are many individual agents aligned with short term interests they often either can't because they will be pushed out by short term thinkers or literally benefit from net harm to all. America mostly being composed of the rich not the masses voting with their wallets.
Heh sure, it's great at optimizing.
The problem is it's an optimizing function for the rich getting richer, not for the good of society, not for reducing human suffering, not even, y'know, the survival of the human race.
Markets optimize for the current gradient, not for the local maxima
Markets will make you climb a hill ignoring it ends on a cliff end