-
Notifications
You must be signed in to change notification settings - Fork 8k
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
base: master
Are you sure you want to change the base?
Fix uniqid() performances #18232
Changes from 1 commit
7cdadcd
1877950
8e15cf4
81adbda
eb40c48
5471a5a
f03c2bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -588,6 +588,7 @@ AC_CHECK_FUNCS(m4_normalize([ | |
| unsetenv | ||
| usleep | ||
| utime | ||
| uuidgen | ||
| vasprintf | ||
| ])) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,10 +31,44 @@ | |
| #include <sys/time.h> | ||
| #endif | ||
|
|
||
| #ifdef HAVE_UUIDGEN | ||
| #include <sys/types.h> | ||
| #include <sys/uuid.h> | ||
| #endif | ||
|
|
||
| #include "ext/random/php_random.h" | ||
| #include "ext/random/php_random_csprng.h" | ||
|
|
||
| #ifdef HAVE_GETTIMEOFDAY | ||
| #ifdef HAVE_UUIDGEN | ||
| /* {{{ Generates a unique ID */ | ||
| PHP_FUNCTION(uniqid) | ||
| { | ||
| char *prefix = ""; | ||
| bool more_entropy = 0; | ||
| zend_string *uniqid; | ||
| size_t prefix_len = 0; | ||
| struct uuid uuid; | ||
| int *n = (int *)&uuid; | ||
|
|
||
| ZEND_PARSE_PARAMETERS_START(0, 2) | ||
| Z_PARAM_OPTIONAL | ||
| Z_PARAM_STRING(prefix, prefix_len) | ||
| Z_PARAM_BOOL(more_entropy) | ||
| ZEND_PARSE_PARAMETERS_END(); | ||
|
|
||
| (void)uuidgen(&uuid, 1); | ||
|
|
||
| if (more_entropy) { | ||
| n[1] &= 0xffffff; | ||
| uniqid = strpprintf(0, "%s%08x%06x.%08x", prefix, n[0], n[1], n[2]); | ||
| } else { | ||
| n[1] &= 0xfffff; | ||
| uniqid = strpprintf(0, "%s%08x%05x", prefix, n[0], n[1]); | ||
| } | ||
|
|
||
| RETURN_STR(uniqid); | ||
| } | ||
| #elif HAVE_GETTIMEOFDAY | ||
| ZEND_TLS struct timeval prev_tv = { 0, 0 }; | ||
|
|
||
| /* {{{ Generates a unique ID */ | ||
|
|
@@ -53,6 +87,24 @@ PHP_FUNCTION(uniqid) | |
| Z_PARAM_BOOL(more_entropy) | ||
| ZEND_PARSE_PARAMETERS_END(); | ||
|
|
||
| #ifdef __NetBSD__ | ||
| struct uuid uuid; | ||
| int *n = (int *)&uuid; | ||
|
|
||
| /* Use faster uuidgen() if available */ | ||
| (void)uuidgen(&uuid, 1); | ||
|
|
||
| if (more_entropy) { | ||
| n[1] &= 0xffffff; | ||
| uniqid = strpprintf(0, "%s%08x%06x.%08x", prefix, n[0], n[1], n[2]); | ||
| } else { | ||
|
||
| n[1] &= 0xfffff; | ||
| uniqid = strpprintf(0, "%s%08x%05x", prefix, n[0], n[1]); | ||
| } | ||
|
|
||
| RETURN_STR(uniqid); | ||
| #endif | ||
|
|
||
| /* 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit confused here. uuidgen is on FreeBSD/NetBSD (other oses have different apis) which you detect at configure time. Why the particular NetBSD code path is needed ? we should fall into the code above I think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I do not really get the code duplication. But not sure it s worth the efforts to be honest considering later comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can this be even executed - it's in
elifsection. Basically:That doesn't make any sense to me. Am I missing anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I committed it a bit too early, I should I removed that section that was used in earlier tests. It is fixed now.