WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#17110 closed defect (bug) (fixed)

apply_filters() performance

Reported by: ryan Owned by:
Milestone: 3.2 Priority: normal
Severity: normal Version: 3.1
Component: Performance Keywords: has-patch
Focuses: Cc:

Description

Pushing and popping to the wp_current_filter array is not necessary if there is not a function hooked to the filter.

Attachments (2)

apply_filters.diff (793 bytes) - added by ryan 3 years ago.
17110.diff (2.6 KB) - added by ryan 3 years ago.
Extend to apply_filters* and do_action*

Download all attachments as: .zip

Change History (9)

ryan3 years ago

comment:1 ryan3 years ago

On my test setup, that patch eliminates 2886 calls to array_pop() as well as the corresponding assignments and reduces time spent in apply_filters() by almost 50%. Most filters, like the often called gettext one, have nothing hooked to them so the pushing and popping is a waste of time. For cases where there is hook on the filter, the patch adds one isset check.

comment:2 scribu3 years ago

Nice catch.

Could this be applied to do_action() as well?

comment:3 andrewryno3 years ago

Above my head to do it myself, but since apply_filters_ref_array() is almost identical, could this patch be used in that function as well (but slightly modified if needed)?

comment:4 ocean903 years ago

  • Cc ocean90 added

ryan3 years ago

Extend to apply_filters* and do_action*

comment:5 ryan3 years ago

On a front page load with 5 posts and default theme, that reduces the number of array_pop() calls from 3272 to 250.

comment:6 ryan3 years ago

With a scratch install having just the default post, benchmarks suggest a 2.4% performance improvement. With more posts (and more filters without attached functions being called) the improvement becomes more notable. With 10 simple posts I see a 3% improvement.

comment:7 ryan3 years ago

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

(In [17638]) Avoid array push/pops for empty filters and actions. fixes #17110

Note: See TracTickets for help on using tickets.