DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Trending

  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  1. DZone
  2. Coding
  3. Languages
  4. Introduction to JIT in PHP 8

Introduction to JIT in PHP 8

The Just-In-Time compiler (JIT) has opened up new avenues for PHP. This article explores how PHP’s recent implementation of JIT improves its performance.

By 
Atul Jindal user avatar
Atul Jindal
·
Jan. 14, 22 · Analysis
Likes (7)
Comment
Save
Tweet
Share
7.8K Views

Join the DZone community and get the full member experience.

Join For Free

PHP is the most popular web scripting language and has been available for many years. With the release of the major version of PHP 5, it has been quite stable and is a popular choice among web developers. There are almost 244 million sites built using PHP as a serverside language. 

In 2020, PHP 8 was released, introducing many syntactically visible features such as attributes, named argument, and union types. All of these features are not so apparent, and according to many PHP developers, the Just-In-Time compiler (JIT) is the most important milestone. This has opened up new avenues for PHP in comparison to other languages in the market. In this article, we are going to explore how the PHP language is executed and how PHP’s recent implementation of JIT improves its performance.

Why JIT?

As stated earlier, an interpreted language like PHP is a popular choice for developing web apps due to its flexibility and ease of coding. It does not require recompilation every time you change your code, and developers do not need to worry about the nitty-gritty details of memory management. 

However, this flexibility comes at the cost of execution speed. Because the interpreter has to analyze, parse and compile the source code on the fly, it adds an extra overhead before every execution, which results in slow code execution. JIT is a solution to this problem. It is a hybrid way of using an interpreter and ahead-of-time compilation at the same time by compiling the portions of code to machine code and then caching it on runtime.

Zend Engine and OPCode

Zend Engine is a set of components that comprises the core of PHP runtime. At the heart of Zend Engine is Zend VM, which is further divided into Zend Compiler and Zend Executor. The role of Zend Compiler is to compile the human-readable PHP script into an intermediary bytecode called OPCode. Every OPCode is a command with operands that the Zend Executor can understand and execute. For each OPCode, there is a C function that runs on the CPU. This is how classic PHP was interpreted.

PHP Interpreter without OPCache

OPCache Extension and Preloading

The first performance optimization in the PHP interpretation flow came through the use of the OPCache extension which was introduced in version 5.4. This extension caches the OPCode of PHP script into shared memory. The subsequent calls will not parse and compile the script if its OPCode representation is present in the cache.

In order to eliminate recompiling overhead further, in PHP version 7.4 OPCache preload was added. This requires specifying a preload file with the opcache.preload directive of the php.ini file, which can further add other files using the opcache_compile_file() function.

PHP Interpreter with OPCache

JIT: The New Frontier

Since PHP 5.x, its maintainers had already applied every possible technique to optimize PHP runtime, including dead code elimination and optimization while generating the OPCode. Therefore, implementing JIT was the next logical step and the team has been working on it since 2011.

PHP’s JIT is part of its OPCache layer. The OPCache analyzes "hot code," or the code that is executed repeatedly. Then it selects the hot OPCode block for JIT compilation. The selected OPCodes are compiled into machine code and cached into OPCaches shared memory for later use. From the next execution of the code block, it uses the cached machine code directly bypassing the Zend VM completely. For CPU-intensive use cases, it has resulted in a significant performance gain.

PHP Interpreter with JIT

Configuring JIT

In PHP8, the JIT is enabled out of the box, but it is turned off. To turn it on we need to set some directives in our php.ini file:

 
opcache.enable=1

opcache.enable_cli=1

opcache.jit_buffer_size=256M

The important directive to keep an eye on here is opcache.jit_buffer_size, which is set to 0 by default, resulting in JIT being turned off.

Final Thoughts

It is important to understand that the JIT feature, as of now, will not significantly increase the performance of different framework-based web applications such as Laravel or WordPress. However, for CPU-intensive calculations-based applications, it has demonstrated an immense performance boost that is on par with compiled languages. This has opened new doors of possibility for newer use cases for PHP. Moreover, it might soon be possible to replace binaries and extensions written with C using PHP code. Therefore, in my opinion, it is indeed an important milestone for PHP.

PHP

Opinions expressed by DZone contributors are their own.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!