PHP Opcache accelerates access
Tags: php
Published: 2021-09-16 04:43:04

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxpk5gdxzjv7b0w28fat.png)


## What is OPcache

Opcache is a way to avoid the overhead of loading and parsing the PHP script each time by storing the pre-compiled bytecode (Operate Code) of the parsed PHP script in the shared memory. The parser can directly read the cached words from the shared memory. Operate Code, which greatly improves the execution efficiency of PHP

Languages ​​like Java must be packaged and compiled before they can be run online, for example, packaged into a jar package. C++ or C# can be packaged into a .dll or .exe. These packaged files are compiled files. After they are run, they will generally remain running, that is, they will become a resident process, and their code will enter the memory. When the program is running, there is no need to interpret or compile it, and naturally the speed will be much faster. OPcache also plays a similar role. It’s just that it’s not a complete set of compilation processes. We still rely on PHP-FPM to run scripts, but after opening OPcache, PHP-FPM will first look for relevant cached bytecodes in memory. If it is in memory, it will be used directly if there is any. If it is not, it will be interpreted and compiled again and then cached. In addition, OPcache is for files, that is, if a file is newly added, it will only be cached if it has been run. If it has not been run, it is not in the current shared memory.

## Opcache Principle

The normal execution flow of PHP is as follows

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qeihr4ixm88snln46sgo.png)

request request (nginx, apache, cli, etc.) -> Zend engine reads the .php file -> scans its dictionary and expressions -> parses the file -> creates the computer code to be executed (called Opcode) -> finally executes the Opcode- > response back

Each time a PHP script is requested, the above steps will be executed. If the PHP source code does not change, then Opcode will not change. Obviously there is no need to regenerate Opcode every time. Combined with the ubiquitous caching mechanism in the Web, we can cache Opcode. , Wouldn’t it be faster to directly access the cached Opcode in the future? The flowchart after enabling Opcode cache is as follows:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wluktfqzme7rfk3wga3v.png)

The purpose of Opcode cache is to avoid repeated compilation and reduce CPU and memory overhead

## Opcache configuration

`php.ini`

```
// Load opcache (need to confirm that the opcache extension has been installed)
zend_extension=opcache.so
// Turn on opcache
opcache.enable = 1
// OPcache shared memory storage size, unit MB
opcache.memory_consumption=1024 // 1G
// PHP uses something called string resident, the default is 4MB
opcache.interned_strings_buffer=32
// This option is used to control the maximum number of PHP files that can be cached in memory. This option must be set to be large enough, greater than the sum of all PHP files in your project
opcache.max_accelerated_files=80000
// Set the expiration time of the cache (in seconds), if it is 0, it must be checked every time
opcache.revalidate_freq=3
// Literally means "allow faster closing"
opcache.fast_shutdown=1

## HugePage

### What is HugePage

Simply put, the default memory is paged by 4KB, and the virtual address and memory address need to be converted, and this conversion is to look up the table, and the CPU will build in TLB (Translation Lookaside Buffer) in order to speed up the table lookup process. Obviously, if the virtual page is smaller, the number of entries in the table is also more, and the TLB size is limited, the more the number of entries, the higher the cache miss of TLB, so if we can enable large memory pages, we can indirectly Lower this TLB Cache Miss

### Configure HugePage (`php.ini`)

This new feature is made in Opcache, so you must also enable this feature through Opcache

```
opcache.huge_code_pages=1
```

Configure the OS, allocate some hugepages

```bash
#Don’t be bigger, the better, the president takes up memory
sudo sysctl vm.nr_hugepages=128
```

Check memory information

```
$ cat /proc/meminfo | grep Huge
AnonHugePages: 444416 kB
HugePages_Total: 128
HugePages_Free: 128
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
```

Seeing that the 128 Hugepages we allocated are ready

## Opcache file cache

### Principle

Use opcache to store the compiled php file as a file, realize php source code protection and script acceleration, there will be a significant performance improvement

### Configuration (`php.ini`)

In this way, PHP will cache some Opcode binary export files in the /tmp directory, which can exist across the PHP life cycle

```
opcache.file_cache=/tmp
```
// In CLI environment, PHP enables OPcache
opcache.enable_cli=1
```