#31259 closed defect (bug) (fixed)
Replace array_shift() with current() where appropriate
Reported by: | SergeyBiryukov | Owned by: | wonderboymusic |
---|---|---|---|
Milestone: | 4.2 | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | has-patch needs-testing |
Focuses: | performance | Cc: |
Description
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)
Change History (8)
#2
@
10 years ago
Yes, array_shift()
does a lot:
https://github.com/php/php-src/blob/745504ea2a7066588af9714f5e32523a3fff4db7/ext/standard/array.c#L2199
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.
#3
@
10 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
@
10 years ago
- Owner set to wonderboymusic
- Resolution set to fixed
- Status changed from new to closed
In 31829:
Related: #31260