[wp-trac] [WordPress Trac] #21120: Optimize get_user_by()

WordPress Trac wp-trac at lists.automattic.com
Fri Jul 13 16:29:29 UTC 2012


#21120: Optimize get_user_by()
------------------------------------+------------------------------
 Reporter:  kurtpayne               |       Owner:
     Type:  enhancement             |      Status:  new
 Priority:  normal                  |   Milestone:  Awaiting Review
Component:  Performance             |     Version:
 Severity:  normal                  |  Resolution:
 Keywords:  dev-feedback has-patch  |
------------------------------------+------------------------------

Comment (by nacin):

 I don't think it's a simple matter of optimizing the calls to
 get_user_by(). Rather, we should consider (''consider'') a
 WP_User::get_instance(), which is essentially get_user_by() but it instead
 returns a reference to an already existing WP_User object, if it has spun
 one up before. This is somewhat similar to how WP_Screen works.

 Alternatively, we can look to make WP_User instantiation faster. Looking
 at some profiling output, that doesn't look simple — the bulk of the time
 spent is on unserializing metadata, initing caps/roles, and pulling the
 user's data from cache. Lazy-loading roles/caps won't help us either, as
 the bulk of our WP_User instantiations are for cap checks.

 What we could do is make map_meta_cap() a bit smarter as well. Rather than
 calling get_userdata(), check if we're dealing with the current user, and
 if so, call wp_get_current_user().

 Additionally, is_super_admin() is almost always passed a user ID in the
 capabilities API, but it is also almost always referring to the current
 user. When we do get a user_id in is_super_admin(), we should check
 against get_current_user_id() to see if we can get away with
 wp_get_current_user().

 Again, this is where WP_User::get_instance() could come in — for when
 you're not sure if you need a new WP_User object, or if you can use
 wp_get_current_user(). Even if it doesn't keep a reference of *all*
 WP_Users it has spun up, just short-circuiting and calling
 wp_get_current_user() could make a huge performance difference.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21120#comment:21>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list