Skip to content

Commit 2834689

Browse files
committed
refactor: improve string encoding performance of non-lazy UUIDs
1 parent 4000e89 commit 2834689

File tree

4 files changed

+116
-14
lines changed

4 files changed

+116
-14
lines changed

src/Codec/GuidStringCodec.php

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Ramsey\Uuid\UuidInterface;
1919

2020
use function bin2hex;
21+
use function sprintf;
2122
use function substr;
2223

2324
/**
@@ -29,6 +30,26 @@
2930
*/
3031
class GuidStringCodec extends StringCodec
3132
{
33+
public function encode(UuidInterface $uuid): string
34+
{
35+
$hex = bin2hex($uuid->getFields()->getBytes());
36+
37+
/** @var non-empty-string */
38+
return sprintf(
39+
'%02s%02s%02s%02s-%02s%02s-%02s%02s-%04s-%012s',
40+
substr($hex, 6, 2),
41+
substr($hex, 4, 2),
42+
substr($hex, 2, 2),
43+
substr($hex, 0, 2),
44+
substr($hex, 10, 2),
45+
substr($hex, 8, 2),
46+
substr($hex, 14, 2),
47+
substr($hex, 12, 2),
48+
substr($hex, 16, 4),
49+
substr($hex, 20),
50+
);
51+
}
52+
3253
public function decode(string $encodedUuid): UuidInterface
3354
{
3455
$bytes = $this->getBytes($encodedUuid);

src/Codec/StringCodec.php

+13-14
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
use Ramsey\Uuid\Builder\UuidBuilderInterface;
1818
use Ramsey\Uuid\Exception\InvalidArgumentException;
1919
use Ramsey\Uuid\Exception\InvalidUuidStringException;
20-
use Ramsey\Uuid\Rfc4122\FieldsInterface;
2120
use Ramsey\Uuid\Uuid;
2221
use Ramsey\Uuid\UuidInterface;
2322

23+
use function bin2hex;
2424
use function hex2bin;
2525
use function implode;
26+
use function sprintf;
2627
use function str_replace;
2728
use function strlen;
2829
use function substr;
@@ -47,19 +48,17 @@ public function __construct(private UuidBuilderInterface $builder)
4748

4849
public function encode(UuidInterface $uuid): string
4950
{
50-
/** @var FieldsInterface $fields */
51-
$fields = $uuid->getFields();
52-
53-
return $fields->getTimeLow()->toString()
54-
. '-'
55-
. $fields->getTimeMid()->toString()
56-
. '-'
57-
. $fields->getTimeHiAndVersion()->toString()
58-
. '-'
59-
. $fields->getClockSeqHiAndReserved()->toString()
60-
. $fields->getClockSeqLow()->toString()
61-
. '-'
62-
. $fields->getNode()->toString();
51+
$hex = bin2hex($uuid->getFields()->getBytes());
52+
53+
/** @var non-empty-string */
54+
return sprintf(
55+
'%08s-%04s-%04s-%04s-%012s',
56+
substr($hex, 0, 8),
57+
substr($hex, 8, 4),
58+
substr($hex, 12, 4),
59+
substr($hex, 16, 4),
60+
substr($hex, 20),
61+
);
6362
}
6463

6564
/**
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the ramsey/uuid library
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
10+
* @license http://opensource.org/licenses/MIT MIT
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace Ramsey\Uuid\Benchmark;
16+
17+
use Ramsey\Uuid\FeatureSet;
18+
use Ramsey\Uuid\Guid\Guid;
19+
use Ramsey\Uuid\UuidFactory;
20+
use Ramsey\Uuid\UuidInterface;
21+
22+
final class GuidConversionBench
23+
{
24+
private const UUID_BYTES = [
25+
"\x1e\x94\x42\x33\x98\x10\x41\x38\x96\x22\x56\xe1\xf9\x0c\x56\xed",
26+
];
27+
28+
private UuidInterface $uuid;
29+
30+
public function __construct()
31+
{
32+
$factory = new UuidFactory(new FeatureSet(useGuids: true));
33+
34+
$this->uuid = $factory->fromBytes(self::UUID_BYTES[0]);
35+
36+
assert($this->uuid instanceof Guid);
37+
}
38+
39+
public function benchStringConversionOfGuid(): void
40+
{
41+
$this->uuid->toString();
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the ramsey/uuid library
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
10+
* @license http://opensource.org/licenses/MIT MIT
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace Ramsey\Uuid\Benchmark;
16+
17+
use Ramsey\Uuid\UuidFactory;
18+
use Ramsey\Uuid\UuidInterface;
19+
20+
final class NonLazyUuidConversionBench
21+
{
22+
private const UUID_BYTES = [
23+
"\x1e\x94\x42\x33\x98\x10\x41\x38\x96\x22\x56\xe1\xf9\x0c\x56\xed",
24+
];
25+
26+
private UuidInterface $uuid;
27+
28+
public function __construct()
29+
{
30+
$factory = new UuidFactory();
31+
32+
$this->uuid = $factory->fromBytes(self::UUID_BYTES[0]);
33+
}
34+
35+
public function benchStringConversionOfUuid(): void
36+
{
37+
$this->uuid->toString();
38+
}
39+
}

0 commit comments

Comments
 (0)