Req #74851 [Opn]: uniqid performances
| From: | cmb@php.net | Date: | Tue, 04 Jul 2017 21:38:14 +0000 |
| Subject: | Req #74851 [Opn]: uniqid performances | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-209810@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74851&edit=1
ID: 74851
Updated by: cmb@php.net
Reported by: manu at netbsd dot org
Summary: uniqid performances
Status: Open
Type: Feature/Change Request
Package: Performance problem
Operating System: NetBSD
PHP Version: 7.1.6
Block user comment: N
Private report: N
New Comment:
Note, that there is already a related RFC[1] including a
discussion on internals[2].
[1] <https://wiki.php.net/rfc/uniqid>
[2] <http://marc.info/?t=147339817200002&r=1&w=2>
Previous Comments:
------------------------------------------------------------------------
[2017-07-04 03:19:50] manu at netbsd dot org
Description:
------------
uniqid() uses system microsecond-precise clock to produce an unique identifier. In order to avoid
producing the same value, it will always call usleep() to wait for the next microsecond.
The problem here is that usleep() may wait for much longer. According to Opengroup's POSIX,
"The suspension time may be longer than requested due to the scheduling of other activity by
the system". Indeed tests on NetBSD show that the kernel will schedule another process during
the usleep() call, resulting in a typical uniqid() duration around 16 ms, which is around 16000 time
slower than intended.
I suggest to address the problem by using uuidgen() system call if available. This system call is
not in POSIX standard, but will be available on most modern systems. The call costs around a
microsecond, and it produce a much better unique identifier than what uniqid() currently does.
Attached is a patch against PHP 7.1.6 that cause PHP's configure script to look up uuidgen(),
and that uses it in uniqid() if it is available.
The same problem was reported at lease in bugs #65626, #37106, #37840 and #14248, but with no
satisfying fix proposed, in my opinion.
Test script:
---------------
Test to compare uniqid() performance across systems:
time php -r 'for ($i = 0; $i < 1000; $i++) uniqid();'
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74851&edit=1