8000 [Uid] fix performance and prevent collisions with the real clock_seq by nicolas-grekas · Pull Request #41693 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Uid] fix performance and prevent collisions with the real clock_seq #41693

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

Merged
merged 1 commit into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
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
[Uid] fix performance and prevent collisions with the real clock_seq
  • Loading branch information
nicolas-grekas committed Jun 17, 2021
commit 7e547fdd3cef07bbbe8d9db5b19310c82fa04a29
5 changes: 1 addition & 4 deletions src/Symfony/Component/Uid/Factory/UuidFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ public function timeBased($node = null): TimeBasedUuidFactory
{
$node ?? $node = $this->timeBasedNode;

if (null === $node) {
$class = $this->timeBasedClass;
$node = $this->timeBasedNode = new $class();
} elseif (!$node instanceof Uuid) {
if (null !== $node && !$node instanceof Uuid) {
$node = Uuid::fromString($node);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Uid/NilUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class NilUuid extends Uuid

public function __construct()
{
$this->uid = '00000000-0000-0000-0000-000000000000';
$this->uid = parent::NIL;
}
}
7 changes: 4 additions & 3 deletions src/Symfony/Component/Uid/Tests/Factory/UuidFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public function testCreateTimed()
$this->assertSame('c10ab929ae84', $uuid1->getNode());

// Test default node override
$uuid2 = $uuidFactory->timeBased('7c1ede70-3586-48ed-a984-23c8018d9174')->create();
$this->assertInstanceOf(UuidV6::class, $uuid2);
$this->assertSame('23c8018d9174', $uuid2->getNode());
$uuid2Factory = $uuidFactory->timeBased('7c1ede70-3586-48ed-a984-23c8018d9174');
$this->assertSame('1eb5a7ae-17e1-62d0-a984-23c8018d9174', (string) $uuid2Factory->create(new \DateTime('@1611076938.057800')));
$this->assertSame('23c8018d9174', substr($uuid2Factory->create(), 24));
$this->assertNotSame('a984', substr($uuid2Factory->create(), 19, 4));

// Test version override
$uuid3 = (new UuidFactory(6, 1))->timeBased()->create();
Expand Down
13 changes: 12 additions & 1 deletion src/Symfony/Component/Uid/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
class Ulid extends AbstractUid
{
private const NIL = '00000000000000000000000000';

private static $time = '';
private static $rand = [];

Expand All @@ -31,6 +33,12 @@ public function __construct(string $ulid = null)
return;
}

if (self::NIL === $ulid) {
$this->uid = $ulid;

return;
}

if (!self::isValid($ulid)) {
throw new \InvalidArgumentException(sprintf('Invalid ULID: "%s".', $ulid));
}
Expand Down Expand Up @@ -77,7 +85,10 @@ public static function fromString(string $ulid): parent
base_convert(substr($ulid, 27, 5), 16, 32)
);

return new static(strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ'));
$u = new static(self::NIL);
$u->uid = strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');

return $u;
}

public function toBinary(): string
Expand Down
20 changes: 10 additions & 10 deletions src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ class Uuid extends AbstractUid
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';

protected const TYPE = 0;
protected const NIL = '00000000-0000-0000-0000-000000000000';

public function __construct(string $uuid)
{
$type = uuid_is_valid($uuid) ? uuid_type($uuid) : false;
$type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;

if (false === $type || \UUID_TYPE_INVALID === $type || (static::TYPE ?: $type) !== $type) {
if (false === $type || (static::TYPE ?: $type) !== $type) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
}

$this->uid = strtr($uuid, 'ABCDEF', 'abcdef');
$this->uid = strtolower($uuid);
}

/**
Expand All @@ -60,17 +61,16 @@ public static function fromString(string $uuid): parent
return new static($uuid);
}

if (!uuid_is_valid($uuid)) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
if (self::NIL === $uuid) {
return new NilUuid();
}

switch (uuid_type($uuid)) {
switch ($uuid[14]) {
case UuidV1::TYPE: return new UuidV1($uuid);
case UuidV3::TYPE: return new UuidV3($uuid);
case UuidV4::TYPE: return new UuidV4($uuid);
case UuidV5::TYPE: return new UuidV5($uuid);
case UuidV6::TYPE: return new UuidV6($uuid);
case NilUuid::TYPE: return new NilUuid();
}

return new self($uuid);
Expand Down Expand Up @@ -109,11 +109,11 @@ final public static function v6(): UuidV6

public static function isValid(string $uuid): bool
{
if (__CLASS__ === static::class) {
return uuid_is_valid($uuid);
if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid)) {
return false;
}

return uuid_is_valid($uuid) && static::TYPE === uuid_type($uuid);
return __CLASS__ === static::class || static::TYPE === (int) $uuid[14];
}

public function toBinary(): string
Expand Down
22 changes: 19 additions & 3 deletions src/Symfony/Component/Uid/UuidV1.php
A92E
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class UuidV1 extends Uuid
{
protected const TYPE = 1;

private static $clockSeq;

public function __construct(string $uuid = null)
{
if (null === $uuid) {
Expand All @@ -41,11 +43,25 @@ public function getNode(): string

public static function generate(\DateTimeInterface $time = null, Uuid $node = null): string
{
$uuid = uuid_create(static::TYPE);
$uuid = !$time || !$node ? uuid_create(static::TYPE) : parent::NIL;

if ($time) {
if ($node) {
// use clock_seq from the node
$seq = substr($node->uid, 19, 4);
} else {
// generate a static random clock_seq to prevent any collisions with the real one
$seq = substr($uuid, 19, 4);

while (null === self::$clockSeq || $seq === self::$clockSeq) {
self::$clockSeq = sprintf('%04x', random_int(0, 0x3fff) | 0x8000);
}

$seq = self::$clockSeq;
}

if (null !== $time) {
$time = BinaryUtil::dateTimeToHex($time);
$uuid = substr($time, 8).'-'.substr($time, 4, 4).'-1'.substr($time, 1, 3).substr($uuid, 18);
$uuid = substr($time, 8).'-'.substr($time, 4, 4).'-1'.substr($time, 1, 3).'-'.$seq.substr($uuid, 23);
}

if ($node) {
Expand Down
15 changes: 6 additions & 9 deletions src/Symfony/Component/Uid/UuidV6.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UuidV6 extends Uuid
{
protected const TYPE = 6;

private static $seed;
private static $node;

public function __construct(string $uuid = null)
{
Expand Down Expand Up @@ -55,15 +55,12 @@ public static function generate(\DateTimeInterface $time = null, Uuid $node = nu
// uuid_create() returns a stable "node" that can leak the MAC of the host, but
// UUIDv6 prefers a truly random number here, let's XOR both to preserve the entropy

if (null === self::$seed) {
self::$seed = [random_int(0, 0xffffff), random_int(0, 0xffffff)];
if (null === self::$node) {
$seed = [random_int(0, 0xffffff), random_int(0, 0xffffff)];
$node = unpack('N2', hex2bin('00'.substr($uuidV1, 24, 6)).hex2bin('00'.substr($uuidV1, 30)));
self::$node = sprintf('%06x%06x', ($seed[0] ^ $node[1]) | 0x010000, $seed[1] ^ $node[2]);
}

$node = unpack('N2', hex2bin('00'.substr($uuidV1, 24, 6)).hex2bin('00'.substr($uuidV1, 30)));

4708 return $uuid.sprintf('%06x%06x',
(self::$seed[0] ^ $node[1]) | 0x010000,
self::$seed[1] ^ $node[2]
);
return $uuid.self::$node;
}
}
0