Skip to content

Commit 4c7e0ec

Browse files
authored
Optimize uuid6() and uuid7() generation (#31)
1 parent 0ff54cc commit 4c7e0ec

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

src/UUID.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,8 @@ public static function uuid6(): string
186186
{
187187
[$unixts, $subsec] = self::getUnixTime();
188188
$timestamp = $unixts * 10 ** 7 + $subsec;
189-
$timehex = str_pad(dechex($timestamp + self::TIME_OFFSET_INT), 16, '0', \STR_PAD_LEFT);
190-
$uhex = sprintf(
191-
'%012s6%03s',
192-
substr($timehex, -15, 12),
193-
substr($timehex, -3)
194-
);
189+
$timehex = str_pad(dechex($timestamp + self::TIME_OFFSET_INT), 15, '0', \STR_PAD_LEFT);
190+
$uhex = substr_replace(substr($timehex, -15), '6', -3, 0);
195191
$uhex .= bin2hex(random_bytes(8));
196192
return self::uuidFromHex($uhex, 6);
197193
}
@@ -206,12 +202,7 @@ public static function uuid7(): string
206202
{
207203
[$unixts, $subsec] = self::getUnixTime();
208204
$uhex = substr(str_pad(dechex($unixts), 9, '0', \STR_PAD_LEFT), -9);
209-
$shex = str_pad(dechex($subsec), 6, '0', \STR_PAD_LEFT);
210-
$uhex .= sprintf(
211-
'%03s7%03s',
212-
substr($shex, 0, 3),
213-
substr($shex, 3, 3)
214-
);
205+
$uhex .= substr_replace(str_pad(dechex($subsec), 6, '0', \STR_PAD_LEFT), '7', -3, 0);
215206
$uhex .= bin2hex(random_bytes(8));
216207
return self::uuidFromHex($uhex, 7);
217208
}

0 commit comments

Comments
 (0)