The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Route callbacks - performance

prybaltowski's Avatar

prybaltowski

19 Jun, 2012 01:29 PM

I just stumbled upon Slim framework 2 days ago and I already really like it. I like the minimalistic approach and coming from node.js and expressjs I really feel at home with routes and callbacks (via Closures).

I am about to build rather big application and I want to give the Slim a try.

My concern is about a performance of the application having to create a lot of closures on each request. In environments like node.js they are created only once on app start-up and are only called on requests. Here, in PHP, they are all created each and every time there's a new request. From your (more experienced) perspective - does it make any big impact on a performance or I can just go for it and don't wonder?

Thanks!

  1. Support Staff 1 Posted by Brian Nesbitt on 19 Jun, 2012 02:30 PM

    Brian Nesbitt's Avatar

    Well you are right about the same PHP code being parsed and executed for each request. The first step is using a PHP opcode cache. When running with PHPFPM that will at least provide opcode caching so re-parsing unchanged files isn't necessary.

    Depending on the site and its data/real-time needs output caching can make your site really responsive and help bypass all of the rendering code no matter what tech you are using.

    The old debate of performance vs productivity. Can you build a large site and scale it for "awhile" using a "slow" language. Ask Facebook and Twitter and they would say yes (PHP, ruby). Then at scale each did their own thing like building hiphop and rewriting parts in Scala. Is what your building going to be that big. Doubt it :-)

    Just benchmark it and see if its "fast enough" for you for your project. Generally its other parts that are slower rather than the "simple" routing framework.

  2. 2 Posted by prybaltowski on 19 Jun, 2012 06:28 PM

    prybaltowski's Avatar

    Thank you for your answer Brian!

    I am aware of ways to optimize the application but they apply to other types of frameworks as well. I were just asking about this step of creating all callbacks as this is the first place I see something like this in PHP. But it looks great! :)

    I will give it a shot anyway. This is going to be a type of social media service and I hope there will be huge traffic one day ;)

    PS. Sorry for writing here but I don't want start new topic just for this - is Slim going to be rewritten for namespaces?

  3. Support Staff 3 Posted by Brian Nesbitt on 19 Jun, 2012 06:44 PM

    Brian Nesbitt's Avatar

    I saw this awhile back. Slim v1.5.0 did pretty good compared to some of the others if it makes you feel any better :-)
    http://www.ruilog.com/blog/view/b6f0e42cf705.html

    Namespaces: no idea!

  4. Support Staff 4 Posted by Brian Nesbitt on 24 Jun, 2012 03:10 AM

    Brian Nesbitt's Avatar

    Had another thought about this today. As suggested in How to organize a large Slim Framework application you can keep your index.php very small and clean and require each file defining routes like so:

    <?php
    $app = new Slim();
    require '../app/routes/static.php';
    require '../app/routes/user.php';
    require '../app/routes/shopping.php';
    require '../app/routes/admin.php';
    $app->run();
    ?>
    

    The thought I had was to conditionally require the routes depending on the URI requested, thus saving the required time to setup/parse/match all of the unnecessary routes.

    For example with the included routes above, if the URI started with /user/... then you could just require '../app/routes/user.php'; and not bother with the others. Limiting the files required would limit the amount of code/routes to parse/execute/match and thus help performance.

    Notes:

    1. This is an early over optimization and should only be looked at when real performance and scalability is a concern... or on a rainy day when some free time is to be taken up with some fun benchmarking :-)

    2. This needs to be benchmarked to compare the difference between parsing of the URI to including and parsing/matching those extra routes.

    3. Running with an opcode negates some of the benefit here

  5. 5 Posted by prybaltowski on 24 Jun, 2012 10:29 AM

    prybaltowski's Avatar

    About the first link - the comparison made me have mixed feelings. It's faster than "huge" frameworks but rather on the slow side of those smaller. :) Anyway, I will probably use SF as a framework and ZF2 as a library - it has some nice tools.

    For routing I've made a router class which defines all URLs but doesn't use closures but "array" callbacks instead to static methods. This way the classes are loaded on demand and no other closures are being created every time.

    I am also thinking about checking the first part of the URL and configuring only the routes needed but this one is for future.

    When I come more familiar with all this and program some more things I will write a post about how I structure my applications. It will be in Polish though.

  6. 6 Posted by wardprogrammer on 25 Jun, 2012 01:15 PM

    wardprogrammer's Avatar

    How to use ZF2 with Sf? Can i see a demo anywhere, I am new to Sf and not
    sure how to work with multiple components.

    Please help.

    Thanks,
    Amit

  7. 7 Posted by prybaltowski on 25 Jun, 2012 02:36 PM

    prybaltowski's Avatar

    I use Composer for my application. I have Slim and Zend frameworks installed with it and I can use all classes as if they were loaded. The autoloader is provided by Composer.

  8. 8 Posted by wardprogrammer on 25 Jun, 2012 02:57 PM

    wardprogrammer's Avatar

    Hi,

    Thanks for quick reply, I have gone through the url and not sure where to
    get started with integration of Composer in Slim project. I am new to Slim
    and Zf, not sure how to get started? Could you please provide me any sample
    project, if possible.

    I appreciate your help.

    Thanks,
    Amit

  9. Support Staff 9 Posted by Brian Nesbitt on 25 Jun, 2012 03:02 PM

    Brian Nesbitt's Avatar

    You can see how to use composer and Slim here:
    http://nesbot.com/2012/6/8/PHP-on-a-diet-up-and-running-with-slim

    Looks like zf2 can then be included via composer as well... its on http://packagist.org/

  10. 10 Posted by wardprogrammer on 25 Jun, 2012 03:15 PM

    wardprogrammer's Avatar

    Wow Brian!

    Sounds great thanks. For now i started working with Slim + Twig (for
    template), Do you think Smarty would work better then Twig in Slim?

    I am not sure which one i should go with,

    I will go through links that you sent me here. I am also interested to know
    in what way we can use zend framework in Slim? I also did some DB work with
    Idioms and Paris (ORM) but not sure if that's the better one.

    Please provide some suggestions. Thanks again for your help.

    regards,
    Amit

  11. Support Staff 11 Posted by Brian Nesbitt on 29 Jun, 2012 01:06 PM

    Brian Nesbitt's Avatar

    Both Smarty and Twig are integrated into Slim so its really your choice.

    https://github.com/codeguy/Slim-Extras/tree/master/Views

    Not sure about zend... I have never used (luckily avoided?) the zend framework.

  12. 12 Posted by wardprogrammer on 29 Jun, 2012 01:26 PM

    wardprogrammer's Avatar

    Thanks Brian!

  13. Brian Nesbitt closed this discussion on 29 Jun, 2012 06:19 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac