Hello,
It is strange that Habrahabr does not allow this message as a comment on the topic of mocksoul about PHP optimization:
www.habrahabr.ru/blog/webdev/19129.html . Probably too long. :)
"
All that I wanted was written to you by letter, but I think that the community may also be interesting and useful, and I will find some mistakes for myself and fill in the gaps in knowledge that are sure to exist.
')
1. “In general, when I was doing a new project, there was a goal — at least 50 requests per second on a run-down Celeron 2.6GHz.” A serious programmer should hide such goals from employers far, far away. :) The goal is to implement business requirements. The number of requests per second is a matter of system requirements, but not the most important one. It is much easier for companies to spend 5-10 thousand dollars for a couple of servers than (for a long time) to search, (with something) to keep and overpay 1-2 thousand every month to a programmer who is capable of such miracles. :) Unfortunately.
2. “LigHTTPd. Under Linux. With sys-epoll on. ”The epoll has serious flaws in front of kqueue. This and the lack of process, AIO and timer watching, and support only atomic events. As I understand it, Linux was chosen based on the fact that "it happened"?
3. "PHP5. Via FastCGI. ”Unfortunately, PHP is not fully FCGI-compatible, and if all the fcgi processes are busy all at once and the backlog ends, and there is no additional pool launch manager layer on top of it, then it will simply shut up - until a hard restart. You can check it by running ab -n 100000 -c (PHP_FCGI_CHILDREN + 1)
localhost . I admit that there are implementations that do not suffer from the like. If you know, I will be grateful.
4. "'--with-gdbm'". Against the background of others --without the inclusion of gdbm looks more like an error than an idea. Or use? This is a very good solution for storing data that has become obsolete in shm.
5. "'--without-mysqli'". >: o And what about prepared statements, multiquery?
6. "PHP_FCGI_CHILDREN" => "32" I had a long experience with the legacy system, where the code was so terrible that a rather expensive server was formed on 10 parallel clients. Saved the quick purchase of additional memory plank :) and an increase in PHP_FCGI_CHILDREN to the maximum that could be allowed there - 256. Later, the system was redesigned, rewritten, and the pool was reduced to 32.
7. “we ask to kill the stream and create a new one through a certain number of requests” - this value is quite easy to calculate If we take into account that affinity takes place for FCGI (at polling), then the most successful option would be to fully serve one client within its keep-alive, and then restart. We take the number of hits by php, divide by the number of hosts and get the average value of php starts / visit, increase it to the "magic" number of times - I take 1.618 to absorb the first deviation. Usually it turns out something about 600-1100.
8. "We write and twist in my head thoughts about how to do something more reasonable and fast right away." No, no, no! We write, check what works, then run through APD / Xdebug and treat only what needs treatment! Similarly, indexes in tables, caching, etc. are assigned.
9. “You probably almost will not need to use require and include. Basically - require_once and include_once. "They are about 3-11 times slower. The best option is a well-documented connection map. Later, of course, opcode-caster collects all the files in one, but for the first run is not bad.
10. “learn to use array_ * functions in php. Especially lambda functions. Cons of lambda functions: deterioration of readability, storing each instance in a global namespace (your example is exactly the moment when the count ($ arr) instances are created), the impossibility of catching errors on a compile-time, etc. This is already enough.
11. "foreach ($ arr as $ key => & $ val) {...}". This is no longer relevant. You can verify this by inserting memory_get_usage () into each iteration.
12. "is_null () - invented by an idiot." And
www.php.net/manual/en/types.comparisons.php was invented by PHP developers.
13. “It's simple - if you own a server - use persistent connections!” And do you use ignore_user_abort () / register_shutdown_function ()? Permanent connections are very, very dangerous! Very easy to get lockout. Instead, it is better to use either SQL Relay / pgpoll, or some other third-party connection pool manager.
14. "The script is absolutely unstable and sharpened for one project." Once I tried to implement a build manager who pulled out all dependencies, knocked everything down into a single file based on a switch, immediately passed it through the encoder and saved it in production. It was fun. Thank you for suggesting to use it again. :)
15. "In my.cnf, we write query_cache_size = 100M". Yes, we read docks. Especially
www.mysqlperformanceblog.com/2007/03/23/beware-large-query_cache-sizes . In principle, in 100M with an average load will be a relatively normal trimming, but reducing to 32-48M, you can reduce the final overhead and save memory for more important settings.
16. "Keep everything in it that is possible." Yes, we kept the time () for interest there with ttl 1 second - when the density of requests is more than 600 per second, the gain is obtained. :)
17. "Do not use IO in the file system for this - better memory." Quite often, the limit of the system is just memory. When everything that is already optimized is optimized, the code simply starts to rest on the number of processes. Then you start to struggle for memory, and a good NAS can save the situation - this is ~ 2000 Mbps (250M per second), which, of course, is incomparable with memory, but not bad, but memory is usually needed right here and right now.
18. “Nevertheless, many other functions of array_ * in php are generally difficult to implement with foreach skills without dexterity. Sorting, for example. ”There are precedents for overtaking the speed of the built-in sort () - a function written in PHP! :) Just sort () implements quicksort as the most common solution, and for particular cases there are faster sorts. For example, if you want to sort the numbers, or natural case.
I hope for an interesting answer.
Thank you for the article! :)
"