PHP opcode Cache

Source: Internet
Author: User
Tags apc

1. What is opcode

After the interpreter parses the code, generates an intermediate code that can be run directly, called the opcode, opcode

2. The difference between interpreter and compiler

The interpreter runs the intermediate code directly after the intermediate code is generated, and the control of the runtime is still in the interpreter's hand.

The compiler then further optimizes the code after generating the intermediate code, generating a target program that can run directly, but not executing, waiting for the user to trigger execution, his control over the target program, and compiler-independent.

3.php is an interpretive language, and his principles are a little similar to compilation, including lexical analysis, grammatical analysis, semantic analysis ... the core engine of the PHP interpreter is Zend.

4.php How to view the opcode of a piece of code

Install PHP parsekit extension, through the extended API can see PHP opcode, such as parsekit_compile_string ()

Php-r "Var_dump (parsekit_compile_string (' Print +; '));"

5.opcode has a Build

First, through lexical analysis, the script code can be seen as a series of word combinations, the interpreter to classify these words, and marked

For example, print, we look at the PHP source package zend/zend_language_scanner.l In this file we can find the pirnt corresponding to the tag. T_print

After the tag is found, then there is the parsing, and in Zend/zend_language_parser.y we can find the corresponding function of T_print.

Then find this function implementation code in ZEND/ZEND_COMPILE.C, this function is to implement the opcode transformation. All opcode are represented by a user integer.

6. Turn on opcode cache

Generating opcode is a system overhead, and every execution is generated once opcode, so the overhead is considerable, so PHP's optimizations must turn on the opcode cache to avoid duplication of compilation.

PHP's opcode cache has apc,eaccelerator,xcache, all of which put opcode in shared memory.

Take APC For example: Set Apc.cache_by_default = on in php.ini

by <?php Print_r (Apc_cache_info ());? > can view the cache situation,

7.opcode Cache Expiration

OpCode cache will expire, if the expiration is to be reborn once, of course, can also skip the mechanism of expiration check, set in PHP.ini Apc.stat=off

This changes the program code to take effect by restarting the server.

8. Script tracking and analysis, can use Xdebug to track, with Xdebug can implement performance tracker, find the program execution bottleneck, thus optimizing the program.

Xdebug.profiler_output_dir =/tmp/xdebug

Xdebug.profiler_output_name = cachegrind.out.%p

function tracking for Xdebug:

Xdebug.trace_output_dir =/tmp/xdebug

Xdebug.trace_output_name = trace.%c

Under Window you can view the Xdebug report file with Wincachegrind.

PHP opcode Cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.