Make WordPress Core

Opened 13 years ago

Closed 13 years ago

#17110 closed defect (bug) (fixed)

apply_filters() performance

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


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

Download all attachments as: .zip

Change History (9)

13 years ago

#1 @ryan
13 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
13 years ago

Nice catch.

Could this be applied to do_action() as well?

#3 @andrewryno
13 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
13 years ago

  • Cc ocean90 added

13 years ago

Extend to apply_filters* and do_action*

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