Skip to content

u/Sentient_Blade

In this archive from Feb 2017 to Mar 2022

23
posts
1.1k
comments
4
media
7.5k
score
4 saved before deletion

Top posts

[RFC] Opcache Preloading (Dmitry Stogov)

Summary Removes significant overhead in PHP request-process-response cycle by allowing a developer to pre-compile common library / framework / application code when the PHP process first starts. These functions / classes would then be always-available without include(...)'ing them. Limitations Would...

89 points11 comments
Open on Reddit

Top comments

The answer is... it depends.

Under normal circumstances, when a file is compiled it is converted into a series of opcodes which can represent not only instructions, but things like how to define classes as well. Those opcodes are all stored in an array.

When the file is executed, it runs that array of opcodes, and when it encounters an opcode which declares a class, it creates the internal definition for it. It's only once those opcodes have run that the class (or function etc) becomes available to use in userland.

Because the process of converting source code into opcodes is quite an expensive one, there's a core extension called opcache. The main purpose of opcache is to store that opcode array in shared memory, so that the next time that particular file needs to be run, it can skip over the parsing / compiling, and just copy over the opcodes into active memory, speeding the whole process up considerably.

There was still one extra element, which was that even though opcodes define how to construct a class and its functions, that's not the whole story, as the classes which have been created might rely on other classes, interfaces, traits etc, so it's the job of another piece of code, the linker, to look up all of those external dependencies, and connect them up, and do any additional checks (such as making sure method signatures match).

Importantly, the linking process has to be done for every request... because of the way PHP works, you might define the same named class in two different files, and even though it's named the same, it may be significantly different... it's very bad practice, but it's possible... so the linked results get dumped after each request.

Now that used to be the end of it... until PHP 7.4.

The latest version of PHP introduced a new tech called "preloading" and its job is to effectively eliminate that last section of linking things together.

With preloading, the first time the PHP executable is run, you can compile all of your classes (and those belonging to frameworks etc) and then run the linker over them before it ever processes the start of a single request.

Once all of this linking has been performed and everything is connected up, the entire block of memory, including all of its inter-connected class definitions, is effectively cloned into each new process, completely skipping the need to run opcodes to define or link classes, because they already exist in memory (so autoloaders never get called).

This offers a small but meaningful performance boost when using code and libraries with lots of classes. It moves all the work to the start of the process, doing it only once, rather than every time a new request comes in.

76 points

PHP is an actively developed language that gets powerful new features on a yearly basis. It supports modern design philosophies and provided you're disciplined in your coding, it can have excellent scalability and performance.

It's lacking some powerful features for sure (generics) but the talent pool to add them is quite small, there's probably only 3 or 4 people in the world who have the skills and time to add that.

Really it's all about what's the right tool for the job, and often times PHP is the right tool for server-side and background tasks. Sometimes other tools are more suited, node for long-running web processes, react or angular for the front end, even a bit of C every now and then when it's really necessary.

I will say the Apache in LAMP has been widely replaced by NGINX + PHP-FPM nowadays.

It's definitely worth learning platform options, as mentioned in other posts, docker and k8s are huge and are only going to get more important.

74 points

Subreddits

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.