Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57e7a1f

Browse files
[Uid] fix performance
1 parent d583b4f commit 57e7a1f

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

src/Symfony/Component/Uid/NilUuid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ class NilUuid extends Uuid
2020

2121
public function __construct()
2222
{
23-
$this->uid = '00000000-0000-0000-0000-000000000000';
23+
$this->uid = parent::NIL;
2424
}
2525
}

src/Symfony/Component/Uid/Tests/Factory/UuidFactoryTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ public function testCreateTimed()
6565
$this->assertSame('c10ab929ae84', $uuid1->getNode());
6666

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

7273
// Test version override
7374
$uuid3 = (new UuidFactory(6, 1))->timeBased()->create();

src/Symfony/Component/Uid/Ulid.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
class Ulid extends AbstractUid
2222
{
23+
private const NIL = '00000000000000000000000000';
24+
2325
private static $time = '';
2426
private static $rand = [];
2527

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

36+
if (self::NIL === $ulid) {
37+
$this->uid = $ulid;
38+
39+
return;
40+
}
41+
3442
if (!self::isValid($ulid)) {
3543
throw new \InvalidArgumentException(sprintf('Invalid ULID: "%s".', $ulid));
3644
}
@@ -77,7 +85,10 @@ public static function fromString(string $ulid): parent
7785
base_convert(substr($ulid, 27, 5), 16, 32)
7886
);
7987

80-
return new static(strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ'));
88+
$u = new static(self::NIL);
89+
$u->uid = strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
90+
91+
return $u;
8192
}
8293

8394
public function toBinary(): string

src/Symfony/Component/Uid/Uuid.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ class Uuid extends AbstractUid
2424
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
2525

2626
protected const TYPE = 0;
27+
protected const NIL = '00000000-0000-0000-0000-000000000000';
2728

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

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

36-
$this->uid = strtr($uuid, 'ABCDEF', 'abcdef');
37+
$this->uid = strtolower($uuid);
3738
}
3839

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

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

67-
switch (uuid_type($uuid)) {
68+
switch ($uuid[14]) {
6869
case UuidV1::TYPE: return new UuidV1($uuid);
6970
case UuidV3::TYPE: return new UuidV3($uuid);
7071
case UuidV4::TYPE: return new UuidV4($uuid);
7172
case UuidV5::TYPE: return new UuidV5($uuid);
7273
case UuidV6::TYPE: return new UuidV6($uuid);
73-
case NilUuid::TYPE: return new NilUuid();
7474
}
7575

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

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

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

119119
public function toBinary(): string

src/Symfony/Component/Uid/UuidV1.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ public function getNode(): string
4141

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

46-
if (null !== $time) {
46+
if ($time) {
4747
$time = BinaryUtil::dateTimeToHex($time);
4848
$uuid = substr($time, 8).'-'.substr($time, 4, 4).'-1'.substr($time, 1, 3).substr($uuid, 18);
4949
}
5050

5151
if ($node) {
52-
$uuid = substr($uuid, 0, 24).substr($node->uid, 24);
52+
if ($time) {
53+
// use clock_seq from the node when a time is provided
54+
$uuid = substr($uuid, 0, 19).substr($node->uid, 19);
55+
} else {
56+
$uuid = substr($uuid, 0, 24).substr($node->uid, 24);
57+
}
5358
}
5459

5560
return $uuid;

0 commit comments

Comments
 (0)