Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate implementation of route trie #6

Merged
merged 5 commits into from
Aug 7, 2023
Merged

Conversation

jhump
Copy link
Member

@jhump jhump commented Aug 7, 2023

Hopefully this isn't too bad to understand. It supports the nesting of variable definitions by using a stack of paths when matching a route while descending through the trie.

This does not yet bother parsing a path template string into the AST model (the routePath struct). So the tests all use struct literals to create routePath instances.

This implements both populating the trie and matching a route to an HTTP request.

Resolves TCN-2192

@jhump jhump requested a review from emcfarlane August 7, 2023 14:02
@linear
Copy link

linear bot commented Aug 7, 2023

TCN-2192 Implement router that uses prefix tree

Needs to support nesting of variable definitions as well as wildcards and double-wildcards.

@connectrpc connectrpc deleted a comment from linear bot Aug 7, 2023
Copy link
Collaborator

@emcfarlane emcfarlane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to get a benchmark, I think allocating maps for each variable capture is going to be expensive and it looks like it grows when adding new routes as you need to keep all variables alive (presumably small though).

From the current implementation I think theres a neat way to simplify the variable capture using indexes of segments. So the method would have a field of an array of vars:

vars [][]struct{
    fds []protoreflect.FieldDesc
    start int
    end int // -1 if `**` captures all remaining segments
}

Then if we convert a nested variable capture /v1/{a=part/{b}/*} to part/*/* with variables as {fds: "a", start: 1, end: 3}, {fds: "b", start: 2, end: 2} on matching a route to the method we can resolve variables by simply taking the index of segments from the path. Thoughts?

router.go Outdated Show resolved Hide resolved
router.go Outdated Show resolved Hide resolved
router.go Outdated Show resolved Hide resolved
router.go Outdated Show resolved Hide resolved
router.go Outdated Show resolved Hide resolved
@jhump
Copy link
Member Author

jhump commented Aug 7, 2023

From the current implementation I think theres a neat way to simplify the variable capture using indexes of segments. So the method would have a field of an array of vars:

Ooh, that's very nice indeed! That gets rid of accumulation entirely in the hot path.

Copy link
Collaborator

@emcfarlane emcfarlane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@jhump jhump merged commit 380458b into main Aug 7, 2023
2 checks passed
@jhump jhump deleted the jh/alternate-router branch September 11, 2023 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants