Make WordPress Core

Opened 10 months ago

Closed 3 months ago

#63211 closed defect (bug) (fixed)

Optimize wp_slash() and Prevent Unnecessary Code Execution

Reported by: dilipbheda's profile dilipbheda Owned by: audrasjb's profile audrasjb
Milestone: 6.9 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: Cc:

Description

Add a return statement in wp_slash() to prevent unnecessary execution.

Optimization Changes:

  • Directly return the processed array using array_map() to avoid redundant execution.
  • Ensured early returns to prevent unnecessary checks for string values.

Performance Impact:

  • Eliminates redundant variable assignments.
  • Prevents unnecessary execution of string checks when $value is an array.
  • Improves function efficiency while maintaining expected behavior.

Change History (8)

This ticket was mentioned in PR #8630 on WordPress/wordpress-develop by @dilipbheda.


10 months ago
#1

#2 @audrasjb
10 months ago

  • Milestone changed from Awaiting Review to 6.9
  • Owner set to audrasjb
  • Status changed from new to accepted

#3 @staurand
10 months ago

Maybe we should move the string test condition first.

<?php
if ( is_string( $value ) ) {

There will always be more strings as param than array?

#4 @dilipbheda
10 months ago

@staurand From my point of view, we can't always predict what type of value a user might pass into wp_slash(), so it's important to structure the function defensively.

The best approach is to check for arrays first using array_map() to ensure recursive processing happens before applying addslashes() to string values. This way, nested arrays are properly handled without skipping over any values.

That said, the current implementation looks good to me. Let’s wait for feedback from others as well.

#5 @rollybueno
5 months ago

  • Keywords needs-testing added

Honestly, I agree and this looks good. Returning early for arrays makes the function a bit cleaner, and I don’t see any downside with this change, though I'm adding needs-testing just in case.

This ticket was mentioned in Slack in #core-test by jon_bossenger. View the logs.


3 months ago

#7 @psykro
3 months ago

  • Keywords needs-testing removed

#8 @SergeyBiryukov
3 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 61103:

General: Return processed array values early in wp_slash().

This is a micro-optimization to avoid an unnecessary is_string() check.

Follow-up to [23555], [48433].

Props dilipbheda, audrasjb, staurand, rollybueno, psykro, westonruter, SergeyBiryukov.
Fixes #63211.

Note: See TracTickets for help on using tickets.