Page 1 of 1
Best practices with lots of functions
Posted: Sat Dec 18, 2010 12:29 pm
by jabbaonthedais
I am writing a game in PHP and I have a really generic best practices question. Every time the user sends the server a command (could happen every few seconds to every few minutes per user), an ajax request hits my "send.php" which decides what to do. I have/will have functions for every command the user can enter, and what to do for each command. There could be dozens and dozens of commands, and some commands will have a lot of code associated with them.
If I break my functions up into seperate include files, and include them based on IF or SWITCH would that speed up PHP?
For example, if user sends command: "attack player1", my IF statement would see "attack" and load functions-attack.php.
I've tried to find specifics about how PHP parses code with functions and includes within IF statements, but having a hard time finding this specific thing.
Any advice would be appreciated!
Re: Best practices with lots of functions
Posted: Sat Dec 18, 2010 12:47 pm
by Jonah Bron
Put the Ajax request into the form of a JSON object like this:
Code: Select all
{
"command":"attack",
"data":{
"victim":"user2"
}
}
Use the command parameter to load the appropriate file. For example, the command "attack" would load /commands/attack.php. But be very careful about it... you don't want malicious users sending commands like "../../someotherfile". That could be very bad.
Re: Best practices with lots of functions
Posted: Tue Dec 21, 2010 6:35 am
by dejan
jabbaonthedais wrote:I am writing a game in PHP and I have a really generic best practices question. Every time the user sends the server a command (could happen every few seconds to every few minutes per user), an ajax request hits my "send.php" which decides what to do. I have/will have functions for every command the user can enter, and what to do for each command. There could be dozens and dozens of commands, and some commands will have a lot of code associated with them.
A while ago, I had a similar dilemma, so I devised this test:
I created a file together.php, where I put 10000 functions. When the script is executed, it randomly calls one of those functions. I also created file separate.php and 10000 separateN.php files, each containing a single function. separate.php includes the file needed and then calls the function.
This it time result for together.php
real 0m0.055s
user 0m0.030s
sys 0m0.030s
This is time result for separate.php
real 0m0.012s
user 0m0.000s
sys 0m0.020s
Memory used by together.php (memory_get_usage):
7664272
Memory used by separate.php
632768
Clearly, the separate approach with including files is faster and consumes less memory. However, "dozens upon dozens" isn't that much to make a big difference. Still, it would make for clearer code to separate into logical groups. Also, since this is AJAX, it is probably helpful to have as quick of a response as possible.
Hope that helps.
Re: Best practices with lots of functions
Posted: Wed Dec 22, 2010 7:16 pm
by josh
Breaking the big file into multiple classes will slow down PHP, as it has to queue more disk Input/Output operations to load all your classes. It will however speed up your development experience.
(As someone else pointed out though, you may or may not actually be including all those other files at once)
Re: Best practices with lots of functions
Posted: Wed Dec 22, 2010 8:27 pm
by Christopher
jabbaonthedais wrote:I've tried to find specifics about how PHP parses code with functions and includes within IF statements, but having a hard time finding this specific thing.
Premature optimization alert. If you are focused on how PHP parses code and not on how to create infectious game play then you're doing it wrong. Code it in a well designed manner and save optimization later. If your game is a hit you can hire a team of programmers to rewrite it.
josh wrote:Breaking the big file into multiple classes will slow down PHP, as it has to queue more disk Input/Output operations to load all your classes. It will however speed up your development experience.
However most modern file system cache files, so even trying to guess which would be faster may be a waste of time given different serve loads. Plus he should be using APC anyway.
Re: Best practices with lots of functions
Posted: Wed Dec 22, 2010 9:06 pm
by josh
All I know is Magento is really slow due to the # of files, their 'compilation' module just cats all the text into one big file, and it makes it worlds faster
Re: Best practices with lots of functions
Posted: Thu Dec 23, 2010 4:03 am
by dejan
josh wrote:Breaking the big file into multiple classes will slow down PHP, as it has to queue more disk Input/Output operations to load all your classes. It will however speed up your development experience.
(As someone else pointed out though, you may or may not actually be including all those other files at once)
jabbaonthedais pointed out that he will be including required files based on need. So, if he needs function X, he will include X.php. That makes sense if there really is a lot of code which is loosely coupled, or not related at all. E.g. it makes sense with plugin / extension development. The way I understand it, jabbaonthedais will have a single dispatch controller for ajax, which will then call required files with controllers in them based on the ajax request. That can make a lot of sense, especially if the use of ajax is intensive.
Re: Best practices with lots of functions
Posted: Thu Dec 23, 2010 1:19 pm
by josh
Unless functions from some files depend on functions in others....
Re: Best practices with lots of functions
Posted: Thu Dec 23, 2010 5:13 pm
by Christopher
josh wrote:All I know is Magento is really slow due to the # of files, their 'compilation' module just cats all the text into one big file, and it makes it worlds faster
I agree that including lots of file can hurt performance. My point was that, given the information in to OP, it is difficult to say what will help or hurt performance. And as dejan points out, a Front Controller can help performance by always loading the same number of files -- but the files loaded are smaller because the code is divided into Actions.