Introduction to opcode cache usage in PHP

Source: Internet
Author: User
Tags apc
This article mainly and everybody introduced the PHP opcode cache simple usage, combined with the example form analysis opcode's concept, the principle, the simple opening and the use method, needs the friend may refer, hoped can help to everybody.

The example in this paper describes the simple usage of opcode cache in PHP. Share to everyone for your reference, as follows:

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 asparsekit_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 in PHP.ini


Apc.cache_by_default = On

Pass


<?php Print_r (Apc_cache_info ());? >

You can view the cache situation,

7.opcode Cache Expiration

OpCode cache will expire, if the expiration is to be reborn once, of course, you can 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/xdebugxdebug.profiler_output_name = cachegrind.out.%p

function tracking for Xdebug:


Xdebug.trace_output_dir =/tmp/xdebugxdebug.trace_output_name = trace.%c

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

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.