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

feat: add multiple parametric nodes support #320

Merged
merged 2 commits into from
Feb 21, 2023

Conversation

ivan-tymoshenko
Copy link
Collaborator

Close #317

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

does this affects our benchmarks?

@ivan-tymoshenko
Copy link
Collaborator Author

No, all changes are in the compile time.

@ivan-tymoshenko ivan-tymoshenko force-pushed the add-multiple-parametric-nodes-support branch from 87531a6 to 7e102a8 Compare February 16, 2023 21:16
@ivan-tymoshenko
Copy link
Collaborator Author

This PR is ready to review. It allows you to have two identical routes with different param types: general parameter and regexp parameter.

Example:

  • /foo/:param
  • /foo/:param(123)

Code in the index.js is pretty tricky. I don't ask you to review it unless you have a lot of time to deal with it. Please take a look at two things:

  1. Params sorting function. It describes how we would prioritese parametric nodes. (param node with long suffix -> param node with short suffix -> regexp node -> param node without suffix)

  2. Please look at the test file. It more or less describes all params collisions and priorities. It's pretty simple and readable.

If you have any suggestions, you are more than welcome.

cc @mcollina @climba03003 @Eomm

@ivan-tymoshenko ivan-tymoshenko marked this pull request as ready for review February 16, 2023 21:32
Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Can you add some docs for this?

Copy link
Contributor

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

Can we defer the sorting to certain point later?
For example, once the first call of lookup.

It seems to do a lot of unnecessary sorting when there are many routes.

@ivan-tymoshenko
Copy link
Collaborator Author

ivan-tymoshenko commented Feb 17, 2023

Can we defer the sorting to certain point later? For example, once the first call of lookup.

It seems to do a lot of unnecessary sorting when there are many routes.

Technically we can, you are right. But you will have this sorting in a very small amount of cases. You a sorting parametric childs of one parent node. IMHO, in 99% of all use cases, you have one parametric child, and in 1%, you have two children. All other cases are less than the statistical error.

On the other hand, this will complicate the already complicated code and introduce an additional error in measuring the hot path. I would agree with you 100% if it would make any difference, but you will not notice it.

@mcollina mcollina requested a review from delvedor February 17, 2023 11:01
@climba03003
Copy link
Contributor

You a sorting parametric childs of one parent node. IMHO, in 99% of all use cases, you have one parametric child, and in 1%, you have two children. All other cases are less than the statistical error.

At least we can do if(this.parametricChildren.length > 1) to prevent single item array being sorted.

@ivan-tymoshenko
Copy link
Collaborator Author

At least we can do if(this.parametricChildren.length > 1) to prevent single item array being sorted.

I think that the sort function is smart enough to not call a sorting handler for an array with one item. But I can add this, not a problem at all.

Copy link
Owner

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

LGTM with some docs!

@climba03003
Copy link
Contributor

At least we can do if(this.parametricChildren.length > 1) to prevent single item array being sorted.

I think that the sort function is smart enough to not call a sorting handler for an array with one item. But I can add this, not a problem at all.

nvm, it is actually smart enough to skip the run.

function sorter(a, b) {
  console.log('did run')
  return a - b
}

;['a'].sort(sorter)
;['a', 'b'].sort(sorter)

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit 501e909 into main Feb 21, 2023
@ivan-tymoshenko ivan-tymoshenko deleted the add-multiple-parametric-nodes-support branch March 6, 2023 22:32
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.

Allow users to have an additional regex node
4 participants