Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize preloading method in PHP7.4

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to realize the preloading method in PHP7.4. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Preface

PHP 7.4 adds preload support which can significantly improve the performance of your code.

This is a simple preload:

In order to preload files, you need to write a custom PHP script that executes once when the server starts all preloaded files are available in memory for all requests before restarting the server, changes made to the source files will not have any impact

Let's take a closer look at it.

Opcache, but more

Although preloading is built on top-level operation opcache, it is not exactly the same. Opcache will take your PHP source files, compile them into opcodes, and store these compiled files on disk.

You can think of the opcode as a low-level representation of the code that can be easily interpreted at run time. Therefore, opcache skips the conversion steps between source files and what the PHP interpreter actually needs at run time. A great victory!

But there is more to be gained. The Opcached file does not know about other files. If you have a class B that extends A from the class, you still need to link them together at run time. In addition, opcache performs a check to see if the source file has been modified and will invalidate its cache based on this file.

So this is where preloading works: it not only compiles source files into opcodes, but also links related classes, features, and interfaces together. It then keeps the "compiled" runnable code blob-that is, the code available to the PHP interpreter-in memory.

When the request arrives at the server, it can now use part of the code base that has been loaded into memory without any overhead.

So, what parts of the code base are we talking about?

Preload in practice

In order for preloading to work, the developer must tell the server which files to load. This is done through a simple PHP script, so there's nothing to be afraid of.

The rules are simple:

You provide a preloaded script and use your php.ini file to link to it opcache.preload every PHP file you want to preload should opcache_compile_file () be passed from the preload script to

Suppose you want to preload a framework, such as Laravel. Your script must iterate through all the PHP files in the directory, vendor/laravel, and include them one by one.

Here is how you link to this script in php.ini:

Opcache.preload=/path/to/project/preload.php

This is a virtual implementation:

Files = / * An array of files you want to preload * /; foreach ($files as $file) {opcache_compile_file ($file);}

Note opcache_compile_file, or you can use this file instead of include. Although there seems to be a bug, it doesn't seem to work at the time of writing.

Warning: unlinked classes cannot be preloaded

Hang on, there's a warning! In order to preload files, you must also preload their dependencies-interfaces, features, and parent classes.

If there is any problem with the class dependency, you will notice it when the server starts:

Can't preload unlinked class

Illuminate\ Database\ Query\ JoinClause:

Unknown parent

Illuminate\ Database\ Query\ Builder

See the opcache_compile_file () parsing file, but do not execute it. This means that if a class has dependencies that are not preloaded, it cannot be preloaded by itself.

This is not a fatal problem and your server will work properly; but you won't have all the pre-installed files you really want.

This is why you should pay attention to which files are preloaded to ensure that all dependencies are resolved. Doing this manually may seem like a chore, so it's natural that people are already developing automation solutions.

Composer support

The most promising automation solution comes from composer and is now used by most modern PHP projects.

People are trying to add the preload configuration option composer.json to generate preload files for you! Just like preloading, this feature is still in progress, but it can be done here.

Fortunately, if you don't want to, you won't need to configure preloaded files manually, and composer will be able to do this for you.

Server requirement

There are two more important things to mention about using devops when preloaded.

You already know that you need to specify an entry in php.ini for preloading to work. This means that if you use a shared host, you cannot freely configure PHP as needed.

In fact, you need a dedicated (virtual) server to optimize preloaded files for a single project. So remember that.

Also remember that php-fpm needs to restart the server every time you reload a file in memory (if you are using it is sufficient). This may seem obvious to most people, but it is still worth mentioning.

Performance

Now to the most important question: does preloading actually improve performance?

The answer is yes, of course: Ben Morel shares some benchmarks, which can be found in the same composer question as before.

Interestingly, you can decide to preload only "hot doors": classes that are often used in the code library. Ben's benchmark shows that loading only about 100 hot classes actually produces a better performance improvement than preloading all. This is the difference between a 13% performance improvement and a 17% performance improvement.

Which classes should be preloaded depends on your specific project. It is wise to preload as easily as possible at the beginning. If you do need to add a few percentage points, you must monitor the code at run time.

All of this can of course be automated and may be done in the future.

Now, the most important thing to remember is that comopser adds support so you don't have to make your own preinstalled files, and this feature is easy to set up on the server because you have complete control over it.

On how to achieve preloading method in PHP7.4 to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report