WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#35652 closed defect (bug) (fixed)

update_metadata() does twice as many loops through pre/post update actions as needed

Reported by: Shelob9 Owned by: boonebgorges
Milestone: 4.5 Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

Description

In update_metadata() WordPress iterates through all of the updated meta ids four times. Once each for the following actions:

  • update_postmeta
  • update_{$meta_type}_meta
  • updated_{$meta_type}_meta
  • updated_postmeta

Making separate loops for both update and then both updated actions is uneeded. Yes, it's a small thing, but if lots of meta ids are being updated for same key, or lots of keys are being updated, or lots of keys with many meta ids per each, it could make a difference.

My patch simplifies the code to two loops. I think it makes it more readable.

A case <em>might</em> be made that the having updated_postmata and updated_post_meta is also redundant, but not sure that's worth deprecating and sounds like a different ticket.

Attachments (1)

35652.diff (1.6 KB) - added by Shelob9 5 years ago.
Patch for 35652

Download all attachments as: .zip

Change History (4)

@Shelob9
5 years ago

Patch for 35652

#1 @boonebgorges
5 years ago

  • Milestone changed from Awaiting Review to 4.5
  • Owner set to boonebgorges
  • Status changed from new to assigned

Yeah, this seems like a good change. There's a slight possibility for breakage, because now if you have meta_ids [1, 2, 3], the order of actions fired will change.

Currently:

update_post_meta 1
update_post_meta 2
update_post_meta 3
update_postmeta 1
update_postmeta 2
update_postmeta 3

After the patch:

update_post_meta 1
update_postmeta 1
update_post_meta 2
update_postmeta 2
update_post_meta 3
update_postmeta 3

I'm having a hard time coming up with a scenario where this would cause a real problem (in what scenarios is one legitimately using both hooks? and in what percentage of those cases does order matter? sounds like a race condition waiting to happen, even without the proposed change). But I want to note it for the record.

#2 @boonebgorges
5 years ago

(PS: update_postmeta is for backward compatibility, from before update_post_meta() was a wrapper for update_metadata().)

#3 @boonebgorges
5 years ago

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

In 36420:

Simplify action placement in update_metadata().

By combining a number of foreach loops, we make the code more readable and
potentially faster in the case where many metadata rows are being updated.

Props Shelob9.
Fixes #35652.

Note: See TracTickets for help on using tickets.