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

Annotations Router to use patterns as prefix #14259

Merged
merged 4 commits into from
Sep 10, 2019

Conversation

zsilbi
Copy link
Member

@zsilbi zsilbi commented Jul 24, 2019

Hello!

  • Type: performance tuning

In raising this pull request, I confirm the following:

  • I have read and understood the Contributing Guidelines
  • I have checked that another pull request for this purpose does not exist
  • I wrote some tests for this PR
  • I have updated the relevant CHANGELOG
  • I have created a PR for the documentation about this change

To use Mvc\Router\Annotations, you have to add the resources that you want it to parse.
You can assign a prefix for every resource for faster handling.

$di->setShared(
     "router",
     function() {
         // Use the annotations router
         $router = new Annotations(false);

         // This will do the same as above but only if the handled uri starts with /robots
         $router->addResource("Robots", "/robots");

        return $router;
     }
);

Adding controllers with distinct prefixes will work great, even in a huge a project.

If you don't use prefixes, it will basicly need to check and parse every resource that you added. It will be a bit slow but will work good enough for small projects.

However, if you have a lot of controllers with prefixes that have parameters or placeholders in the beginning, you won't be able to use them.

For example if you have 75 controllers with prefixes like this:

  • /clients/{clientId:[0-9]+}/
  • /clients/{clientId:[0-9]+}/robots
  • /clients/{clientId:[0-9]+}/parts
  • ...

In this case, only the /clients prefix can be used for all the 75 controllers.

In my test runs, I used a total of 150 resources, 75 with the same prefix and the average route handling time was over 50ms. Without using any prefixes, it was around 90ms.

My idea would be to compile the prefixes as standard route patterns, so you could pass any type of pattern as a prefix along with your resource to the Router.

I repeated the same tests using this code, and my average route handling time was around 3ms which I find somewhat acceptable.

Thanks,
zsilbi

@ruudboon
Copy link
Member

ruudboon commented Aug 3, 2019

If you rebase probably your mysql will build again.

@zsilbi zsilbi force-pushed the annotations-router-pattern-prefix branch from e3ce6cd to 1b793a6 Compare August 14, 2019 10:37
composer.json Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Aug 14, 2019

Codecov Report

Merging #14259 into 4.0.x will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##            4.0.x   #14259   +/-   ##
=======================================
  Coverage   67.78%   67.78%           
=======================================
  Files         478      478           
  Lines      112862   112921   +59     
=======================================
+ Hits        76506    76546   +40     
- Misses      36356    36375   +19

@zsilbi zsilbi force-pushed the annotations-router-pattern-prefix branch from fa8ab72 to 46556c1 Compare September 10, 2019 13:36
@zsilbi zsilbi force-pushed the annotations-router-pattern-prefix branch from 46556c1 to 5faddd1 Compare September 10, 2019 13:44
@zsilbi zsilbi changed the title [WIP] Annotations Router to use patterns as prefix Annotations Router to use patterns as prefix Sep 10, 2019
@niden niden added 4.0 documentation Documentation required enhancement Enhancement to the framework labels Sep 10, 2019
@niden niden merged commit 852a5f7 into phalcon:4.0.x Sep 10, 2019
@niden
Copy link
Member

niden commented Sep 10, 2019

Thank you @zsilbi

@zsilbi zsilbi deleted the annotations-router-pattern-prefix branch September 11, 2019 10:22
@niden niden removed the documentation Documentation required label Dec 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to the framework
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants