Skip to content

Fix uniqid() performances #18232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions ext/standard/uniqid.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,22 @@ PHP_FUNCTION(uniqid)
ZEND_PARSE_PARAMETERS_END();

/* This implementation needs current microsecond to change,
* hence we poll time until it does. This is much faster than
* calling usleep(1) which may cause the kernel to schedule
* another process, causing a pause of around 10ms.
* If system clock lags and report the same time again,
* we just return the previous value +1us.
* This is much faster than calling usleep(1) which may
* cause the kernel to schedule another process, causing
* a pause of around 10ms.
*/
do {
(void)gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
} while (tv.tv_sec == prev_tv.tv_sec && tv.tv_usec == prev_tv.tv_usec);
(void)gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
if (tv.tv_sec < prev_tv.tv_sec ||
(tv.tv_sec == prev_tv.tv_sec && tv.tv_usec <= prev_tv.tv_usec)) {
tv.tv_sec = prev_tv.tv_sec;
tv.tv_usec = prev_tv.tv_usec + 1;
if (tv.tv_usec >= 1000000) {
tv.tv_sec++;
tv.tv_usec -= 1000000;
}
}

prev_tv.tv_sec = tv.tv_sec;
prev_tv.tv_usec = tv.tv_usec;
Expand Down
Loading