[buddypress-trac] [BuddyPress Trac] #6864: `bp_sort_by_key()` is slow

buddypress-trac noreply at wordpress.org
Tue Feb 2 03:38:12 UTC 2016

#6864: `bp_sort_by_key()` is slow
 Reporter:  boonebgorges  |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  2.5
Component:  API           |    Version:
 Severity:  normal        |   Keywords:  2nd-opinion
 `bp_sort_by_key()` uses `create_function()` to create a `usort()`
 callback. This process is very slow, especially when called hundreds of
 times on a pageload.

 See, eg, when pulling up large numbers of activity items ->
 `bp_activity_generate_action_string()` -> `bp_activity_get_actions()`. In
 my specific case, 70 activity items are being pulled up, which results in
 `bp_activity_get_actions()` being called 70 times. Each time the latter
 function is called, it causes each component's actions to be sorted
 (another performance issue - I'll open a separate ticket). With 12
 components, this results in `bp_sort_by_key()` being called 840 times.

 In the above situation, with the current `create_function()`
 implementation, the total wall time for `bp_sort_by_key()` is about
 1,101,000 microseconds - more than 1 second.

 Switching to an anonymous function brings the wall time down to about
 27,000 microseconds, an improvement of over 400x. But anonymous functions
 are only available in PHP 5.3. So I'd like to suggest one of the

 1. We use anonymous functions when available (eg, `if ( class_exists(
 'Closure' ) )`)
 2. We break the callback out into its own named function. But then we'll
 need to put the `$key` and `$type` values into a global variable so
 they're accessible in that function.
 3. We fake PHP 5.3 callbacks using a technique like the last one on this
 page http://codeblow.com/questions/pass-extra-parameters-to-usort-

 Item 1 seems the most straightforward, but seeing as we don't do this
 anywhere else in BP, I want to be sure that no one has a problem with the

Ticket URL: <https://buddypress.trac.wordpress.org/ticket/6864>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac

More information about the buddypress-trac mailing list