Jump to content

MVC and speed


Stefano

Recommended Posts

Hi everyone, I just joined this forum and I hope you can help me with my problem.

I've been messing around with php for a while, building websites with frameworks and recently i decided to write my own framework. I got it done pretty quickly and it works fine but i'm starting to think that maybe MVC isn't the best design choice for a webapp.

 

My main concern is speed, my code right now is quite clean and mantainable but there are a few problems.

I redirect every request to index.php using an .htaccess file (and i think that slows down my site) and also i'm using url routing, so for each request to the server my code will loop over all the regexps and check if he can find a match... Oh and... then i load the controller wich loads the models and view etc.. etc..

 

I think all those operations slow down the website a lot and I would like to know if you have any solutions to my problem, my english is pretty bad I know, I hope you'll understand what i wrote anyway :shy:

 

Edit: I'm also considering the idea of writing only procedural php from now on, i want superfast websites. The code will probably be hard to mantain but as i said, all i want is speed, what do you think?

Link to comment
Share on other sites

I redirect every request to index.php using an .htaccess file

 

Are you sure? A redirect would be time consuming, but that's not the typical method. Typically you just tell the server to pass all requests to an index.php file. This costs no time at all.

 

Routing can be an expensive operation in most MVC's that I've seen. This really is the part of the application that needs allot of optimization, and youv'e just got to be careful how you go about things. We would need to see some relevant code before we could understand exactly how your routing works and be able to offer much assistance. Though there are a few pointers we can probably through at you.

 

  • Avoid routing via regex where possible. If you don't need to match a pattern, and can simply match a static url to a controller / action, then do so.
  • Put your simplest routes first. This way you don't have to waste time processing the more complex ones unless they are needed.

 

That's probably all I could offer without seeing your code.

Link to comment
Share on other sites

Thank you for your reply, I can surely optimize routing, remove the htaccess and let the server handle that but is the design wrong? OOP in general, it's clean and maintainable but also much slower than procedural... but i'm not sure if i'll be able to handle a big project using procedural programming... it's really hard to choose...

 

Edit: what i really wanted to ask is: if you gotta write Youtube, what would you use? OOP+MVC? Procedural? i'm not writing youtube but it's just an example, i want to make the fastest possible website wich is also possible to maintain... hard choice

Link to comment
Share on other sites

I can surely optimize routing, remove the htaccess and let the server handle that

 

The .htaccess file is a server config file. So you are letting the server handle it already.

 

If your application is noticeably slow your likely doing something wrong somewhere or over complicating things. The MVC pattern lends itself very well to web applications, hence it's popularity. I've written a couple of frameworks in my time (all have since been superseded by the next and I'm currently in the process of again another complete rewrite) but have never had any issues with speed. Most of my issues are maintainability, but that's just because I can have a tendency to over complicate things.

 

Try and keep your design as simple as possible (KISS).

Link to comment
Share on other sites

I know that .htaccess overrides the server configuration but it's good to remove it and instead edit the server configuration file, i heard it's faster that way.

My application is not slow, but i think it would be very slow if i had a lot of users online, and hm yea i think i tend to over complicate things too  ;)

Link to comment
Share on other sites

Stop fixating on .htaccess. That is so trivial compared to what thorpe mentioned it's not even worth thinking about. Pass all queries through the index.php file and go from there.

 

Stop worrying about .htaccess. Oh, and no I wouldn't advise trying to find an alternative to using an app specific .htaccess. You want your code to be somewhat portable too. Editing server configuration files is host-dependent. mod_rewrite is largely supported and enabled.

 

I haven't written any MVC-based frameworks so can't advise on that but I can tell you  along with optimizing your code, you will want to look into caching. You should also note the major bottleneck in most web apps is the database so focus on optimizing that.

Link to comment
Share on other sites

I redirect every request to index.php using an .htaccess file (and i think that slows down my site)

 

Like Anti-Moronic already mentioned it isn't. Plus it has nothing to do with OOP

 

i'm using url routing, so for each request to the server my code will loop over all the regexps and check if he can find a match...

 

RegEx are slow, the manual advises to use normal string functions before moving to RegEx.

Again has nothing to do with OOP.

 

What are the URI's in your app? If you don't have many URI's you may aswell just store them in an array:

 

/* $route[ {REQUEST_URI} ] = array( {CONTROLLER}[, {METHOD}[, {PARAM}, .. ] ] ); */
$route['/about'] = array('about', 'index');
$route['/jobs'] = array('about', 'jobs');

Link to comment
Share on other sites

Thanks for your help, I was thinking about cache and I think i will build a database query cache system, so that you execute queries only once an hour or something like that, because query results would be cached i think that would be pretty cool, also i could cache anything else basically... for the htaccess, probably i'm too worried about that, and hm... for url routing i'll test a few things and i'll update you :P

Still... OOP looks kinda slow to me, and PHP has a garbage collector, so it's even worse speed-wise. But yea... i'll probably use it anyway, takes too much time to write and maintain procedural code.

And as i said, i'll be using cache so i'll be fine with OOP.

I think i'll just post some code later, so we can optimize it, if you are nerd enough  :P

Link to comment
Share on other sites

OOP looks kinda slow to me, and PHP has a garbage collector

 

When it comes to garbage collection OOP is your friend, every-time your code exits a method or you set $class = null it's garbage collected, the same applies for a function. Where as if you have a page with all your code it's only garbage collected at the end of your script or if you called unset() so it's wise to put as much code into functions.

 

takes too much time to write and maintain procedural code

 

There's a topic on this forum about OOP vs Procedural in which I and Anti-Moronic among others have actively participated ;) Their are lots of open-source procedural projects that proof to be maintainable like: PHP, MySQL, Linux, Apache, .. so it all boils down to how you generally organize things.

 

Without speaking for everybody else, we are all nerd enough; every one of us.

 

Yup. We are all certified: 99% nerd inside.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.