Optimized rule matching