Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#31259 closed defect (bug) (fixed)

Replace array_shift() with current() where appropriate

Reported by: sergeybiryukov's profile SergeyBiryukov Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.2 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch needs-testing
Focuses: performance Cc:


Background: #31182

array_shift() recalculates array indexes, and can be slow on large arrays. Most of the time, this is unnecessary, as we only use it to get the first element of the array, and the array is never used later.

Attachments (1)

31259.patch (8.2 KB) - added by SergeyBiryukov 9 years ago.

Download all attachments as: .zip

Change History (8)

#2 @wonderboymusic
9 years ago

Yes, array_shift() does a lot:

I tend to prefer reset() over current(), because reset() is guaranteed to be the first item, while current() is based on the pointer at calltime. Internally, they are basically the same.



Last edited 9 years ago by wonderboymusic (previous) (diff)

#3 @nacin
9 years ago

Strongly agree on not depending on the pointer being the first item.

This is definitely not the first time we've converted array modification functions to reset() and such. They sneak in over time.

#4 @wonderboymusic
9 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 31829:

Replace array_shift() with reset() where appropriate for performance.

Props SergeyBiryukov.
Fixes #31259.

#5 @SergeyBiryukov
9 years ago

In 31841:

After [31730], replace one more instance of array_shift() with reset() for better performance.

see #31259, #15459.

#6 @SergeyBiryukov
9 years ago

In 31842:

Bundled themes: After [31453], replace current() with reset(), which is guaranteed to be the first item.

see #31259, #31260.

This ticket was mentioned in Slack in #core by sergey. View the logs.

9 years ago

Note: See TracTickets for help on using tickets.