Interrupts and buffers
An ISR that only records each edge and its time into a ring, a main loop that drains and debounces it, volatile and atomic reads done right, and an overflow counter that tells you when the loop fell behind.
Open it in the simulator