Thermodynamic limits to energy optimization