I would argue that the ending of the book is optimistic despite the event that precedes it. An imperfect father wants the best for his child, and does the best he can with the hand he's dealt. In a dying world of cannibals and worse, there are people who are good, and whose surroundings don't poison their view on what it means to be good. "Do you carry the fire?" is, to my mind, an incredibly optimistic sentiment.
The ending is definitely the most optimistic part of the book, but on balance I think the overall picture is still excruciatingly bleak. It gives me the impression that any optimism of the part of the characters is likely unwarranted. They're still doomed.
The point is that everyone is doomed (even if you imagine we can survive the civilization-murdering tools we've cobbled up, we can't outrun physics), but that even at our most vulnerable, since the book occurs during a period directly after Armageddon, it is possible for some goodness in us to persist.
I don't want to spoil, but the optimism isn't for the characters, it's for we the reader, and the species.
The thimble of fire joins the wider flame. Goodness survives even there, and even then.
I don't think so; preserving goodness and decency comes at little personal cost to most of us, but McCarthy's effort in the book is at its core a depiction of these things surviving even the apocalypse, and at an incredible cost.
That fire they carry is not extinguished even in a world where all systems and pretenses at civilization have been ruined. It finds the wider flame, and decent folks to tend it.
It is one of the more optimistic works he's done, not despite the setting, but because.