UUID generation in PHP
We have been using UUID for years and different ways to generate UUID exist.
I prefer the PECL extension over the ramsey/uuid because it’s simpler and more straightforward. Few weeks ago, with Nicolas Grekas we had the idea to port the PECL extension to plain PHP as a Symfony Polyfill. Then, we wanted to blog about FFI, a new PHP 7.4 feature, so with Baptiste Leduc we bound the libuuid
to PHP.
So now, there are many ways to generate UUID in PHP, at least 4. Let’s compare them:
- The PECL extension;
- A polyfill for the PECL extension;
- An abstraction layer on top of many generator and an UUID component: ramsey/uuid;
- A FFI binding for PHP 7.4+.
I created a benchmark to see how theses implementations compare:
Section intitulée uuid-v1-time-basedUUID V1 (time based)
+----------------------+---------+-------+
| subject | mean | diff |
+----------------------+---------+-------+
| benchPecl | 0.637μs | 1.00x |
| benchSymfonyPolyfill | 1.736μs | 2.72x |
| benchJoliCodeFFI | 1.349μs | 2.12x |
| benchRamsey | 4.295μs | 6.74x |
+----------------------+---------+-------+
Section intitulée uuid-v4-random-valuesUUID V4 (random values)
+----------------------+---------+-------+
| subject | mean | diff |
+----------------------+---------+-------+
| benchPecl | 4.546μs | 2.87x |
| benchSymfonyPolyfill | 1.583μs | 1.00x |
| benchJoliCodeFFI | 5.416μs | 3.42x |
| benchRamsey | 2.103μs | 1.33x |
+----------------------+---------+-------+
Section intitulée how-to-explain-these-numbersHow to explain these numbers?
Section intitulée for-the-v1For the V1
V1 is time based. It means there are not so much random data. But each UUID must be unique, so an internal counter is required.
- The PECL is the fastest here because it uses some features of the kernel to get incremental data,
- The FFI binding is slower because there is a significant overhead with FFI for things that are really fast in the underlying library. This overhead comes from the conversion type between PHP and the underlying library,
- The Polyfill is slower because it must deal with the counter manually,
- The Rasmey implementation is the slowest because the way it deals with counter could be improved.
Section intitulée for-the-v4For the V4
V4 is full of random data. It means it is really expensive for the CPU to generate one compared to a V1.
- The Polyfill is the fastest because of PHP. PHP uses
sys_getrandom()
a faster way to get random data. - The Rasmey implementation is also very fast for the same reason,
- The PECL is much slower due to the underlying library (
libuuid
) opening a new file descriptor on/dev/urandom
each time we calluuid_create()
from PHP Code, - The FFI binding is the slowest since it relies on the same
libuuid
library, and it adds the FFI overhead.
Section intitulée conclusionConclusion
Now you now know everything about uuid, so… just make the right choice. ;)
Keep in mind that the FFI binding works only with PHP 7.4+, so it’s likely unusable for many projects (for now).
ramsey/uuid is an UUID component that leverage many uuid generator (it can use the PECL internally). It could be interesting to use the polyfill in the ramsey/uuid implementation.
And since the polyfill is the same thing than the PECL extension, the final choice is about the API:
- something simple: the PECL (or the polyfill if you can’t install an extension),
- something more feature-rich: the ramsey/uuid component,
- something hype: the FFI binding.
Now you know everything about the ways to generate UUID in PHP, just use the right one for you.
Commentaires et discussions
PHP 7.4 FFI: What you need to know
(🇫🇷 Lire la version en Français ici) PHP Foreign Function Interface, or FFI for fans, is a PHP extension that allows you to include with ease some externals libraries into your PHP code. That means it’s possible to use C, Go, Rust, etc. shared library directly in PHP without writing…
Lire la suite de l’article PHP 7.4 FFI: What you need to know
PHP 7.4 et FFI, ce qu’il faut retenir
(🇬🇧 Read the english version here) PHP Foreign Function Interface, ou PHP FFI pour les intimes, ou FFI pour les fans, est une extension PHP qui permet d’inclure facilement des bibliothèques externes au sein de PHP. Autrement dit, il est possible d’utiliser directement des librairies…
Lire la suite de l’article PHP 7.4 et FFI, ce qu’il faut retenir
Nos articles sur le même sujet
Nos formations sur ce sujet
Notre expertise est aussi disponible sous forme de formations professionnelles !
Symfony avancée
Découvrez les fonctionnalités et concepts avancés de Symfony
Ces clients ont profité de notre expertise
JoliCode continue d’accompagner les équipes web d’Afflelou en assurant la maintenance des différentes applications constituant la plateforme Web B2C. Nous mettons en place des bonnes pratiques avec PHPStan et Rector, procédons à la montée de version de PHP et Symfony, optimisons le code grâce au profiling, et collaborons avec l’infogéreur pour les…
À l’occasion de la 12e édition du concours Europan Europe, JoliCode a conçu la plateforme technique du concours. Ce site permet la présentation des différents sites pour lesquels il y a un appel à projets, et encadre le processus de recueil des projets soumis par des milliers d’architectes candidats. L’application gère également toute la partie post-concours…
Nous avons entrepris une refonte complète du site, initialement développé sur Drupal, dans le but de le consolider et de jeter les bases d’un avenir solide en adoptant Symfony. La plateforme est hautement sophistiquée et propose une pléthore de fonctionnalités, telles que la gestion des abonnements avec Stripe et Paypal, une API pour l’application…