optimization tricks & mistakes to avoid - iliaopcode cache • each php script is interpreted...
TRANSCRIPT
![Page 1: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/1.jpg)
Optimization Tricks & Mistakes to Avoid
http://joind.in/4026
Ilia Alshanetsky@iliaa
Wednesday, 19 October, 11
![Page 2: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/2.jpg)
PrematureOptimization =
Wednesday, 19 October, 11
![Page 3: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/3.jpg)
PrematureOptimization =
Solve the business case, before optimizing the solution
Wednesday, 19 October, 11
![Page 4: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/4.jpg)
Don’t Over Engineer
• Understand your audience
• Estimate the scale and growth of your application (based on facts, not marketing fiction)
• Keep timelines in mind when setting the project scope
Wednesday, 19 October, 11
![Page 5: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/5.jpg)
Simplify, Simplify & Simplify!
• Break complex tasks into simpler sub-components
• Don’t be afraid to modularize the code
• More code does not translate to slower code (common misconception)
Wednesday, 19 October, 11
![Page 6: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/6.jpg)
Hardware is Cheaper!
VS
In most cases applications can gain vast performance gains by improving hardware,
quickly rather than through slow, error prone code optimization efforts.
Wednesday, 19 October, 11
![Page 7: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/7.jpg)
Hardware
• In many application speed is limited by Disk IO. SSDs provide a rapid solution to that problem.
SSD
SCSI
Wednesday, 19 October, 11
![Page 8: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/8.jpg)
Hardware Caveat• While quick to give results, in some
situations it will not help for long:
• Database saturation
• Non-scalable code base
• Network bound bottleneck
• Extremely low number sessions per server
Wednesday, 19 October, 11
![Page 9: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/9.jpg)
Optimize, but don’t touch the code
Wednesday, 19 October, 11
![Page 10: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/10.jpg)
• This cycle happens for every include file, not just for the "main" script.
• Compilation can easily consume more time than execution.
PHP Script
Zend Compile
Zend Execute
@includ
e/require
methodfunction
call
How PHP works in 30 seconds
Wednesday, 19 October, 11
![Page 11: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/11.jpg)
Opcode Cache
• Each PHP script is interpreted only once for each revision
• Reduced File IO, opcodes are being read from memory instead of being parsed from disk
• Opcodes can optimized for faster execution
• Yields a minimum 20-30% speed improvement and often as much as 200-400%
http://pecl.php.net/apcWednesday, 19 October, 11
![Page 12: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/12.jpg)
Quick Comparison
0
50
100
150
200
FUDforum Smarty phpMyAdmin
Regular PHPZend ServerAPCX-Cache
Wednesday, 19 October, 11
![Page 13: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/13.jpg)
Use In-Memory Caches• In-memory session storage is MUCH
faster than disk or database equivalents
• Very simple via Memcached extension
session.save_path = “localhost:11211”session.save_handler = “memcached”
https://github.com/php-memcached-devWednesday, 19 October, 11
![Page 14: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/14.jpg)
Everything has to beReal-time
Wednesday, 19 October, 11
![Page 15: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/15.jpg)
Complete Page Caching
• Caching Proxy ala NginX
• Page pre-generation
• On-demand caching
Wednesday, 19 October, 11
![Page 16: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/16.jpg)
Partial Cache - SQL
• In most applications the primary bottleneck can often be traced to “database work”
• Caching of SQL can drastically reduce the load caused by unavoidable, complex queries
Wednesday, 19 October, 11
![Page 17: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/17.jpg)
SQL Caching Example
$key = md5(“some sort of sql query”);if (!($result = cache_fetch($key))) {
$result = $pdo->query($qry)->fetchAll(); // cache query result for 1 hour
cache_fetch($key, $result, NULL, 3600);}
Wednesday, 19 October, 11
![Page 18: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/18.jpg)
Partial Cache - Code
• Rather than optimizing complex PHP operations, it is often better to simply cache their output for a period of time
• Faster payoff
• Lower chance of breaking the code
• Faster then any “code optimization”
Wednesday, 19 October, 11
![Page 19: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/19.jpg)
Code Caching Examplefunction myFunction($a, $b, $c) {
$key = __FUNCTION__ . serialize(func_get_args());if (!($result = cache_get($key))) {
$result = // function code// cache query result for 1 hourcache_set($key, $result, NULL, 3600);
}return $result;
}
Wednesday, 19 October, 11
![Page 20: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/20.jpg)
Output Buffering
Don’t fear output buffering because it uses RAM, RAM is cheap. IO, not so much.
Wednesday, 19 October, 11
![Page 21: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/21.jpg)
• The goal is to pass off as much work to the kernel as efficiently as possible.
• Optimizes PHP to OS Communication
• Reduces Number Of System Calls
Matching Your IO Sizes
PHP Apache OS Client
Wednesday, 19 October, 11
![Page 22: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/22.jpg)
• Efficient
• Flexible
• In your script, with ob_start()
• Everywhere, with output_buffering = Xkb
• Improves browser’s rendering speed
PHP: Output Control
PHP Apache
Wednesday, 19 October, 11
![Page 23: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/23.jpg)
Apache: Output Control
The idea is to hand off entire page to the kernel without blocking.
Set SendBufferSize = PageSize
Apache OS
Wednesday, 19 October, 11
![Page 24: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/24.jpg)
OS: Output ControlOS (Linux)
/proc/sys/net/ipv4/tcp_wmem
4096 16384 maxcontentsize
min default max
/proc/sys/net/ipv4/tcp_mem
(maxcontentsize * maxclients) / pagesize
✴ Be careful on low memory systems!
OS Client
Wednesday, 19 October, 11
![Page 25: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/25.jpg)
Upgrade Your PHPBefore “upgrading” your code, upgrade your PHP. Newer versions are typically faster!
0
37.5
75
112.5
150
PHP 4.4 PHP 5.0 PHP 5.1 PHP 5.2 PHP 5.3 PHP-SVNWednesday, 19 October, 11
![Page 26: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/26.jpg)
Database before code
• One of the most common mistakes people make is optimizing code before even looking at the database
• Vast majority of applications have the bottleneck in the database not the code!
Wednesday, 19 October, 11
![Page 27: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/27.jpg)
Watch Your Errors
• Excessive, non-critical errors, such as E_NOTICE or E_STRICT can only be detected via error-logging
• PHP code that generates any errors is going to impact performance!
Not Easily Detectable by Profilers
Wednesday, 19 October, 11
![Page 28: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/28.jpg)
Micro Optimization
• Takes a long time
• Won’t solve your performance issues
• Almost guaranteed to break something
• Cost > Reward
Wednesday, 19 October, 11
![Page 29: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/29.jpg)
Speed vs Scale
• If you are planning for growth, scale is far more important than speed!
• Focus on scalability rather than speed, you can always increase scalable app, by simply adding more hardware.
Wednesday, 19 October, 11
![Page 30: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/30.jpg)
Don’t Re-invent the Wheel
• Most attempts to make “faster” versions of native PHP functions using PHP code are silly exercises in futility.
Wednesday, 19 October, 11
![Page 31: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/31.jpg)
Write Only Code
• Removing comments won’t make code faster
• Neither will removal of whitespace
• Remember, you may need to debug that mess at some point ;-)
• Shorter code != Faster Code
Wednesday, 19 October, 11
![Page 32: Optimization Tricks & Mistakes to Avoid - IliaOpcode Cache • Each PHP script is interpreted only once for each revision • Reduced File IO, opcodes are being read from memory instead](https://reader034.vdocument.in/reader034/viewer/2022042312/5eda3f73b3745412b5710588/html5/thumbnails/32.jpg)
Thank You!Any Questions?
Slides @ www.ilia.wsComments @ joind.in/4026
Wednesday, 19 October, 11