WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 9 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 9 years ago.
17110.diff (2.6 KB) - added by ryan 9 years ago.
Extend to apply_filters* and do_action*

Download all attachments as: .zip

Change History (9)

@ryan
9 years ago

#1 @ryan
9 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.

#2 @scribu
9 years ago

Nice catch.

Could this be applied to do_action() as well?

#3 @andrewryno
9 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)?

#4 @ocean90
9 years ago

  • Cc ocean90 added

@ryan
9 years ago

Extend to apply_filters* and do_action*

#5 @ryan
9 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.

#6 @ryan
9 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.

#7 @ryan
9 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.