Skip to content

Commit 00aab67

Browse files
committed
minor #36078 [Uid] work around slow generation of v4 UUIDs (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Uid] work around slow generation of v4 UUIDs | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This is more than 4 times faster than `uuid_create()` for v4. As described in https://jolicode.com/blog/uuid-generation-in-php Commits ------- a0e8d24 [Uid] work around slow generation of v4 UUIDs
2 parents 7dc6da6 + a0e8d24 commit 00aab67

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/Symfony/Component/Uid/UuidV4.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ class UuidV4 extends Uuid
2525
public function __construct(string $uuid = null)
2626
{
2727
if (null === $uuid) {
28-
$this->uuid = uuid_create(static::TYPE);
28+
$uuid = random_bytes(16);
29+
$uuid[6] = $uuid[6] & "\x0F" | "\x4F";
30+
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
31+
$uuid = bin2hex($uuid);
32+
33+
$this->uuid = substr($uuid, 0, 8).'-'.substr($uuid, 8, 4).'-'.substr($uuid, 12, 4).'-'.substr($uuid, 16, 4).'-'.substr($uuid, 20, 12);
2934
} else {
3035
parent::__construct($uuid);
3136
}

0 commit comments

Comments
 (0)