I would reckon that the optimism/pessimism in different grades of fantasy is not necessarily about the moral codes or behaviour of the characters in the setting, but, as you yourself pointed out, about the nature of the setting itself.
In an epic fantasy setting, provided the world/society is fallen or declined from a previous brighter age, there is the prospect of restoration. Perhaps not to the greatest heights of elder times - in Lord of the Rings, Aragorn's restoration of Gondor is clearly still not a restoration to the power of ancient Numenor, for instance - but nevertheless, an increase in peace and prosperity, and a ridding the world of great evil. In such a setting without a previous brighter age, the prospect is not of restoration, but instead progress, both in the material and metaphysical senses. That is the optimism of the setting.
In a sword & sorcery setting, whether or not the world/society is fallen or declined from a previous brighter age, there is no prospect of restoration or progress, except either in small areas, over brief time periods, or both. The world could be dying, à la Vance, or the eldritch horrors could be unfathomable, à la Lovecraft, or societies could exist in a more-or-less permanent cycle of rise and fall, with societies at each stage in the cycle clearly paying some price for whatever benefits they have - whether that's being forced to subsist in a harsh and unforgiving environment or in a state of decadence/corruption. That is the pessimism of the setting.
That difference also informs the cosmic forces at work in the setting. The cosmic forces of epic fantasy include ones that are, however hands-off, unabashedly benevolent. Such forces are feeble, ailing, or simply non-existent in swords & sorcery, and in their place are those that are indifferent or malevolent.