[wp-trac] [WordPress Trac] #50223: Performance improvement: Avoid using array_unique() where possible

WordPress Trac noreply at wordpress.org
Fri May 22 21:39:04 UTC 2020


#50223: Performance improvement: Avoid using array_unique() where possible
-------------------------------+------------------------------
 Reporter:  aristath           |       Owner:  (none)
     Type:  enhancement        |      Status:  new
 Priority:  lowest             |   Milestone:  Awaiting Review
Component:  General            |     Version:
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:  performance
-------------------------------+------------------------------

Comment (by joyously):

 I'm not sure it's relevant to WP's data, but the result of flip-flip is
 not always the same as array_unique.
 I see two ways that they can differ.
 1) array_unique preserves the keys, retaining the first one for a
 duplicate value, whereas array_flip uses the last one of the keys of the
 duplicate values.
 2) array_unique will compare as string by default (although there is a
 flag). array_flip() does not retain the data type of values, and they must
 be either integer or string (no boolean or array or object, although you
 can encode them first).

 And it appears that version 7.2 changed the internals, so that
 array_unique could be giving slightly different keys as it used to. From
 https://www.php.net/manual/en/function.array-unique :
 > If sort_flags is SORT_STRING, formerly array has been copied and non-
 unique elements have been removed (without packing the array afterwards),
 but now a new array is built by adding the unique elements. This can
 result in different numeric indexes.

 Perhaps a test can be done on the different sort_flags, to see if
 SORT_REGULAR is faster than the default SORT_STRING.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50223#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list