routing-kit icon indicating copy to clipboard operation
routing-kit copied to clipboard

Change routing to search all possible routes and choose most specific…

Open ULazdins opened this issue 4 years ago • 5 comments

Changes routing to match most specific of possible routes instead of current behaviour. Fixes #92

Consider attached test cases which currently fail, but are fixed in this Pull Request:

    func testMatchesLongestRoute() throws {
        let router = TrieRouter(String.self)
        
        router.register("longest", at: ["a", "b", ":param", "d"])
        router.register("shortest", at: ["a", "b", "c"])
        
        var params = Parameters()
        // Fails with:   // XCTAssertEqual failed: ("nil") is not equal to ("Optional("longest")")
        XCTAssertEqual(router.route(path: ["a", "b", "c", "d"], parameters: &params), "longest")
        XCTAssertEqual(params.get("param"), "c")
    }
    
    func testMatchesLongestRoute2() throws {
        let router = TrieRouter(String.self)
        
        router.register("longest", at: ["a", "b", ":param", .catchall])
        router.register("shortest", at: ["a", "b", "c"])
        
        var params = Parameters()

        // Fails with:   XCTAssertEqual failed: ("nil") is not equal to ("Optional("longest")")
        XCTAssertEqual(router.route(path: ["a", "b", "c", "d"], parameters: &params), "longest")
        XCTAssertEqual(params.get("param"), "c")
        XCTAssertEqual(params.getCatchall(), ["d"])
    }

These changes have small time and space complexity penalty, but I'm not able to wrap my head around what exactly is the new performance hit. The performance tests seems OK.

ULazdins avatar Jan 06 '21 17:01 ULazdins

@vapor-bot test performance

twof avatar Jan 08 '21 08:01 twof

Yeah this isn't getting merged. I've just run the performance tests locally. Current implementation took 25s to run them all. This PR took 5 minutes and 19 seconds. We could potentially give up a few percent of performance for improved behaviour of routing but 1200% is a bit much! 😅

0xTim avatar Jan 08 '21 08:01 0xTim

@0xTim , sorry what? 😅 Runs fine on 2019 MBP

xcode

Can you post your test results?

ULazdins avatar Jan 08 '21 08:01 ULazdins

You need to change the configuration to release to actually enable the performance tests - you can see they all took 0.01s because they were ignored by https://github.com/vapor/routing-kit/blob/master/Tests/RoutingKitTests/RouterPerformanceTests.swift#L132

0xTim avatar Jan 08 '21 08:01 0xTim

Got it. Will investigate if can be improved

ULazdins avatar Jan 08 '21 09:01 ULazdins