WordPress.org

Make WordPress Core

Opened 2 months ago

Closed 2 months ago

#48083 closed enhancement (fixed)

Use `**` operator to replace`pow()` function calls

Reported by: ayeshrajans Owned by: SergeyBiryukov
Milestone: 5.3 Priority: normal
Severity: trivial Version: 5.3
Component: General Keywords: has-patch commit
Focuses: performance, coding-standards Cc:
PR Number:

Description

Another ticket for a PHP 5.6+ enhancement. With PHP 5.6, we can use the ** operator instead of pow() function. Doc page of pow() function also suggests to use the operator.

Using the ** operator is about 4 times faster too.

  1. ** operator: https://www.php.net/manual/en/language.operators.arithmetic.php
  2. pow() function: https://www.php.net/manual/en/function.pow.php
  3. Simple benchmark: https://3v4l.org/LMRcp

Attached patch below will fix the only 2 instances we have in WordPress code base.

SimplePie and GetID3 libraries have this pattern more frequently. I see @jrf has created an issue in SimplePie about a new release, so I will open a PR there to see if we can get this in SimplePie upstream. GetID3 library requires PHP 5.3, so I don't think we can get this improvement to upstream.

Thank you.

Attachments (2)

48083-1.patch (1.4 KB) - added by ayeshrajans 2 months ago.
Improvements in the WP code base only, no third party libraries.
48083-2.patch (1.4 KB) - added by ayeshrajans 2 months ago.
Thanks a lot @jrf - I uploaded a modified patch with the code style changes as well.

Download all attachments as: .zip

Change History (7)

@ayeshrajans
2 months ago

Improvements in the WP code base only, no third party libraries.

#1 @jrf
2 months ago

Hi @ayeshrajans Yet another good idea ;-)

Reviewed & the changes themselves look ok, the patch just needs one space on the inside of the parentheses on both changed lines.

<?php
if ( 8 < $size / ( 1024 ** 2 ) ) {

$size  = KB_IN_BYTES ** ( $log - $power )

Also saw you opened the PR to SimplePie 👍🏻.
Adding the link here for reference: https://github.com/simplepie/simplepie/pull/622

@ayeshrajans
2 months ago

Thanks a lot @jrf - I uploaded a modified patch with the code style changes as well.

#2 @jrf
2 months ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 5.3

Thanks @ayeshrajans

@SergeyBiryukov This is a very small change which doesn't change functionality at all, just improves performance. Think it can still go into WP 5.3 ?

#3 @SergeyBiryukov
2 months ago

Sure, was already on my list :)

#4 @jrf
2 months ago

@SergeyBiryukov 💕

#5 @SergeyBiryukov
2 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 46225:

Code Modernization: Use the ** operator instead of the pow() function in wp_convert_bytes_to_hr() for better performance.

Use the MB_IN_BYTES constant instead of the pow() function in WP_Customize_Manager::_validate_header_video() for better readability.

Props ayeshrajans, jrf.
Fixes #48083.

Note: See TracTickets for help on using tickets.