The trap is your belief in a linear relationship between effort and outcome. Working harder and longer produces more productivity, right?
That's false.
As you say so yourself, your memory goes to shit. It's not just a law of diminishing returns. For coders, past a certain point, exhaustion leads you to start undoing your work. You are into negative productivity.
I've pushed through all-nighters where I poured out a few thousand LOC only to find the next day that I had broken everything and introduced more bugs than I had time to fix. I just had to revert. An exhausted hacker is worse than no hacker at all. You're a liability to yourself and the project.
So, like driving when tired, realise that the urge to continue is completely irrational, and probably based in the emotional drives of fear or pride. It would be completely irrational for any manager to want you to code in that state, and anyone who pushes you to is also the idiot.