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
Contributor

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

This comment has been minimized.

Copy link
Member

ruudboon commented Aug 3, 2019

If you rebase probably your mysql will build again.

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

This comment has been minimized.

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 zsilbi:annotations-router-pattern-prefix branch from fa8ab72 to 46556c1 Sep 10, 2019
@zsilbi zsilbi force-pushed the zsilbi:annotations-router-pattern-prefix branch from 46556c1 to 5faddd1 Sep 10, 2019
@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 approved these changes Sep 10, 2019
@niden niden added this to In progress in 4.0 Release via automation Sep 10, 2019
@niden niden merged commit 852a5f7 into phalcon:4.0.x Sep 10, 2019
4 checks passed
4 checks passed
codecov/patch Coverage not affected when comparing 1483618...7738d66
Details
codecov/project 67.78% remains the same compared to 1483618
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
4.0 Release automation moved this from In progress to Done Sep 10, 2019
@niden

This comment has been minimized.

Copy link
Member

niden commented Sep 10, 2019

Thank you @zsilbi

@zsilbi zsilbi deleted the zsilbi:annotations-router-pattern-prefix branch Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
4.0 Release
  
Done
4 participants
You can’t perform that action at this time.