Make WordPress Core

Opened 18 months ago

Closed 16 months ago

Last modified 16 months ago

#58532 closed enhancement (fixed)

Improve performance and simplify usage of `block_has_support()` by supporting a string feature and avoiding `_wp_array_get()` for them

Reported by: flixos90's profile flixos90 Owned by: thekt12's profile thekt12
Milestone: 6.4 Priority: normal
Severity: normal Version: 5.8
Component: Editor Keywords: has-patch has-unit-tests commit
Focuses: performance Cc:

Description

Follow up to #58376: As mentioned in https://core.trac.wordpress.org/ticket/58376#comment:20, block_has_support() is one of the most notable functions that perform a ton of calls to _wp_array_get(). While that function can be useful, specifically for block_has_support() it is very much overkill, since almost every feature is just a simple string, i.e. uses a single-dimension array.

Additionally, passing an array for the $feature parameter is a bit confusing. Even the initial documentation accidentally defined a string, which suggests that was simply assumed to be correct as it is more intuitive (see #56307).

I am proposing the following:

  • Add support for the $feature parameter of block_has_support() to be an array or string.
  • If a string is provided, don't use _wp_array_get( $block_type->supports, $feature, $default_value ), but simply isset( $block_type->supports[ $feature ] ) ? $block_type->supports[ $feature ] : $default_value.
  • Update all usages of block_has_support() that provide an array with just one item to just pass that item as a string.

Of course we could also count the items in the array and use isset() if it is just 1. But the approach of providing a string brings a similar performance improvement and is a more intuitive API.

Attachments (2)

58532-1.patch (639 bytes) - added by nihar007 18 months ago.
Patch added
58532-2.patch (702 bytes) - added by nihar007 18 months ago.
One change in code

Download all attachments as: .zip

Change History (12)

#1 @flixos90
18 months ago

cc @aristath @spacedmonkey @soean

@nihar007
18 months ago

Patch added

@nihar007
18 months ago

One change in code

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


16 months ago
#2

  • Keywords has-patch has-unit-tests added

#3 @mukesh27
16 months ago

  • Keywords changes-requested added
  • Milestone changed from Awaiting Review to 6.4

Thanks @thekt12 for the PR.

Left some feedback on the PR.

Moving to 6.4 milestone for consideration.

#4 @swissspidy
16 months ago

Of course we could also count the items in the array and use isset() if it is just 1. But the approach of providing a string brings a similar performance improvement and is a more intuitive API.

I'd suggest doing both, so that plugins and themes doing that will automatically benefit from such an enhancement too without having to do any code changes.

#5 @flixos90
16 months ago

  • Owner set to thekt12
  • Status changed from new to assigned

#6 @flixos90
16 months ago

  • Keywords commit added; changes-requested removed

After some quick performance benchmarks, there is no notable performance impact from this change, which is in line what I had seen earlier. This change is mostly about improving the API: Given most features to check for are simple strings, it is more intuitive to check for them like that, e.g. that way is in line with other feature check functions in WP core such as for post type support.

In terms of performance, the impact is negligible. It is safe to assume that the string check is marginally faster than using _wp_array_get(), but the change is certainly more about API clarity than about performance.

#7 @mukesh27
16 months ago

  • Version set to 5.8

The function is introduced in 5.8, so set the version accordingly.

#8 @flixos90
16 months ago

  • Component changed from General to Editor

#9 @flixos90
16 months ago

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

In 56382:

Editor: Simplify usage of block_has_support() function by supporting a string.

Most block feature checks are for a single feature string, and for such cases it is not intuitive to require an array for the $feature parameter of the block_has_support() function.

This changeset brings it in line with other functions like post_type_supports(), allowing to pass a string for the $feature. An array is still supported for more complex cases where support for sub-features needs to be determined. This change furthermore includes a very minor performance tweak by avoiding calls to the _wp_array_get() function if a single feature string is being checked for.

Props thekt12, nihar007, mukesh27, swissspidy.
Fixes #58532.

Note: See TracTickets for help on using tickets.