Simple Usage Analysis of opcode Cache in PHP

  • 2021-09-16 06:31:54
  • OfStack

This article illustrates the simple usage of opcode cache in PHP. Share it for your reference, as follows:

1. What is opcode

After the interpreter analyzes the code, it generates intermediate code that can be run directly, which is called opcode, opcode

2. The difference between interpreter and compiler

The interpreter runs the intermediate code directly after generating the intermediate code, and the control of the runtime is still in the hands of the interpreter.

The compiler optimizes the code one step after generating the intermediate code, and generates the target program that can be run directly, but does not execute it, waiting for the user to trigger execution, and his control is in the target program, which has nothing to do with the compiler.

3. php is an interpretive language. Its principle is similar to compiling, including lexical analysis, syntax analysis and semantic analysis. The core engine of php interpreter is zend engine

4. php How do I view an opcode for 1 piece of code

Install the parsekit extension of php, and view the opcode of php through the extended api, as shown in parsekit_compile_string()


php -r "var_dump(parsekit_compile_string('print 1+1;'));"

5. opcode is generated

The first step is to go through lexical analysis. The script code can be seen as a series of word combinations. The interpreter classifies and marks these words

For example, print, we look at zend/zend_language_scanner. l in the source package of php. We can find the tag corresponding to pirnt in this file. T_PRINT

After finding the tag, there is parsing. In zend/zend_language_parser.y, we can find the corresponding function of T_PRINT

Then find the implementation code of this function in zend/zend_compile. c, which realizes the transformation of opcode. All opcode are represented by user integers.

6. Turn on opcode cache

Generating opcode requires system overhead, and every execution needs to generate opcode, which is considerable. Therefore, the optimization of php must open the cache of opcode to avoid repeated compilation.

The opcode cache of php has APC, eAccelerator, and XCache, all of which put opcode in shared memory.

Take APC as an example: Set in php. ini


apc.cache_by_default = on

Pass


<?php print_r(apc_cache_info());?>

You can view the cache,

7. opcode cache expires

opcode cache will expire. If it expires, it will be newly generated once. Of course, you can skip the expiration check mechanism and set it in php. ini


apc.stat=off

In this way, the modification of program code has to take effect by restarting the server.

8. Script tracing and analysis, can use xdebug to track, with xdebug can achieve performance tracker, find the bottleneck of the implementation of the program, so as to optimize the program.


xdebug.profiler_output_dir = /tmp/xdebug
xdebug.profiler_output_name = cachegrind.out.%p

Function tracing for xdebug:


xdebug.trace_output_dir = /tmp/xdebug
xdebug.trace_output_name = trace.%c

Under window, you can use wincachegrind to view the report file of xdebug.

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Cache Technology", "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this paper is helpful to everyone's PHP programming.


Related articles: