[wp-trac] [WordPress Trac] #48082: Replace `dirname(__FILE__)` with `__DIR__` (Performance improvement, massive patch)

WordPress Trac noreply at wordpress.org
Wed May 4 19:35:15 UTC 2022

#48082: Replace `dirname(__FILE__)` with `__DIR__` (Performance improvement,
massive patch)
 Reporter:  ayeshrajans       |       Owner:  SergeyBiryukov
     Type:  enhancement       |      Status:  closed
 Priority:  normal            |   Milestone:  5.4
Component:  General           |     Version:
 Severity:  minor             |  Resolution:  fixed
 Keywords:  has-patch needs-  |     Focuses:  performance, coding-standards
  testing                     |

Comment (by SergeyBiryukov):

 Replying to [comment:19 azaozz]:
 > Did a quick test to see how much faster `include 'file.php';` is
 compared to `include( 'file.php' );` as that was the reason to reduce code
 readability. Running this 10k times in a loop did not produce any
 measurable difference, seems the speed up is mostly theoretical.

 Thanks for the feedback! Just noting that `include( 'file.php' );`
 triggers two WPCS warnings:
 | WARNING | [x] "include" is a statement not a function; no parentheses
 are required
 |         |     (PEAR.Files.IncludingFile.BracketsNotRequired)
 | WARNING | [x] File is being unconditionally included; use "require"
 |         |     (PEAR.Files.IncludingFile.UseRequire)
 > The other change that was "attached" later seems to have a bit of
 negative effect. Replacing `include` with `require once` seems to slow
 down PHP by about 0.5% on my test server. Frankly I'm not sure why this
 was done without any tests and why on this ticket.

 I think this part was suggested in comment:6 and supported in comment:7
 not necessarily for performance, but rather for consistency: `include`
 implies that the file is optional and execution can continue even if it's
 missing, while `require` implies a hard requirement, which was the case in
 pretty much all of the instances here.

Ticket URL: <https://core.trac.wordpress.org/ticket/48082#comment:20>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list