If you are losing encoder counts, then use an interrupt service routine.
Four of PORTB’s pins, RB<7:4>, have an interrupt-onchange feature.
Use two of these pins for the A/B encoder signals.

Luciano