php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76178 Class constants are slow: they should be inlined at runtime
Submitted: 2018-04-02 19:35 UTC Modified: 2018-05-03 11:45 UTC
Votes:53
Avg. Score:4.5 ± 0.8
Reproduced:36 of 40 (90.0%)
Same Version:31 (86.1%)
Same OS:17 (47.2%)
From: nicolas dot grekas+php at gmail dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.2.4 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nicolas dot grekas+php at gmail dot com
New email:
PHP Version: OS:

 

 [2018-04-02 19:35 UTC] nicolas dot grekas+php at gmail dot com
Description:
------------
On both Twig and Symfony, we noticed that in some performance-critical code paths, the most significant change we could do to improve performance was replacing class constants by their values.

See:
- https://github.com/twigphp/Twig/pull/2636/files
- https://github.com/symfony/symfony/pull/26161/files

While I understand that the reason for this is that opcode arrays are per-file, this could be optimized at runtime, by repalcing the const-fetch by its value on first access (similarly to what is done for non-namespaced functions.)

Test script:
---------------
function foo($a = SomeClassInAnotherFile::FOO)
{
}

function bar($a = 123)
{
}

Expected result:
----------------
Calling foo() and bar() without any arguments should be as fast for both functions.

Actual result:
--------------
Calling foo() is measurably slower than calling bar()

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-04-02 19:36 UTC] nicolas dot grekas+php at gmail dot com
Oups, bad link for Symfony, here is the correct one:
https://github.com/symfony/symfony/pull/25474/files
 [2018-04-05 16:43 UTC] kelunik@php.net
Class constants should already be inlined in case they're from the same class they're used in: https://3v4l.org/jM5BA/vld#output
 [2018-04-05 16:46 UTC] nicolas dot grekas+php at gmail dot com
@kelunik sure, but that's not true anymore when taking inheritance into account (and the parent class is in another file.)
 [2018-05-03 11:45 UTC] dmitry@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: dmitry
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed May 07 23:01:27 2025 UTC