optimize router matching algorithm