Home  >  Article  >  Backend Development  >  What are the differences between opcode cache and JIT

What are the differences between opcode cache and JIT

little bottle
little bottleforward
2019-04-26 16:30:402264browse

This article mainly talks about the difference between opcode cache and JIT. It has certain learning value. Interested friends can learn about it. I hope it can be helpful to you.

To explain the difference between opcode cache and JIT, we must first understand the difference between bytecode, also called intermediate code and machine code.

Operation code (opcode)

A machine instruction. For example, an operation statement written in our assembly language.

Machine code

Scientific name for machine language instructions, sometimes also called native code, is data that can be directly interpreted by the computer's CPU .

Machine code is the machine instruction that the computer CPU directly reads and runs. It runs the fastest, but it is very obscure and difficult to understand and difficult to write. It is not accessible to ordinary practitioners.

And the machine code does not support cross-platform. To put it simply, the machine code used by different CPUs is different.

Bytecode (bytecode)

is a binary file that contains an executable program and consists of a sequence of op code/data pairs. Bytecode is a kind of intermediate code, which is more abstract than machine code and needs to be translated by an interpreter before it can become the intermediate code of machine code.

Bytecode is mainly used to implement specific software operation and software environment, regardless of the hardware environment. The way bytecode is implemented is through a compiler and a virtual machine. The compiler compiles the source code into bytecode, and the virtual machine on a specific platform translates the bytecode into instructions that can be directly executed. The typical application of bytecode is Java bytecode, and PHP is a binary file composed of a series of opcodes.

The bytecode is converted through a virtual machine (JAVA's JVM, PHP's Zend virtual machine) during runtime to generate machine instructions, so it can run better across platforms.

Bytecode is a binary code (file) in an intermediate state (intermediate code). It needs an interpreter to translate it into machine code.

Through the introduction, we can see that the CPU can only execute machine code, but in order to realize the application across hardware platforms, we implemented a virtual machine for different programming languages, and this virtual machine converts the code we wrote Compiled into binary code (file), this binary code is called bytecode, also called intermediate code. The bytecode compiled by the Zend virtual machine is called opcode (actually a series of opcodes).

Next we will introduce opcode cache and JIT.

JIT

Currently, PHP has not introduced JIT technology, but Brother Niao said that the next major version of PHP may bring new JIT features. Let’s wait and see! However, JIT is a very mature technology in the JAVA ecosystem, so let’s talk about JAVA’s JIT.

JIT is the abbreviation of just in time, which is

Just-in-time compilation compiler. Using just-in-time compiler technology can speed up the execution of Java programs.

Usually javac is used to compile the program source code and convert it into java bytecode. The JVM translates the bytecode into corresponding machine instructions (machine code), reads them in one by one, and interprets the translation one by one. Obviously, after interpretation and execution, its execution speed will inevitably be much slower than the executable binary bytecode program. To increase execution speed, JIT technology is introduced.

JIT saves the translated machine code for next time use (there must be an algorithm similar to LRU). It can be seen that what JIT has to do is very simple, which is to temporarily save the machine code translated from the intermediate code (for how long, how to choose is not introduced here), so that when the machine code is used again, there will be one less translation.

opcode cache

Just by listening to the name, you will know that it caches the intermediate code (a binary file composed of a series of opcodes). To quote the official website: OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

So tell me why opcode cache is needed?

The life cycle of PHP code

From The PHP parser executes a PHP script and outputs the script content. It mainly goes through five steps: Zend engine reads the file, lexical analysis, syntax analysis and semantic analysis, creates the intermediate code, and executes the intermediate code, as shown below

Every time the PHP script is requested, the above steps will be executed.

If the PHP code does not change, then the opcode will not change either. Obviously there is no need to generate opcode every time, so we can cache the compiled opcode. If the PHP code does not change in the future, we can directly access the cache. compiled opcode.

The flow chart after enabling opcode caching is as follows:

Summary

Simple description: JAVA's JIT is used to cache the machine code executed by the CPU, and the opcode cache is used to cache the Zend virtual machine. Intermediate code.

Related tutorials: PHP video tutorial

The above is the detailed content of What are the differences between opcode cache and JIT. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete