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
Dans le cadre d’une refonte complète de son architecture Web, le journal en ligne Mediapart a sollicité l’expertise de JoliCode afin d’accompagner ses équipes. Mediapart.fr est un des rares journaux 100% en ligne qui n’appartient qu’à ses lecteurs qui amène un fort traffic authentifiés et donc difficilement cachable. Pour effectuer cette migration, …
JoliCode accompagne l’équipe technique Dayuse dans l’optimisation des performances de sa plateforme. Nous sommes intervenus sur différents sujets : La fonctionnalité de recherche d’hôtels, en remplaçant MongoDB et Algolia par Redis et Elasticsearch. La mise en place d’un workflow de réservation, la migration d’un site en Twig vers une SPA à base de…
Nous avons développé une plateforme de site génériques autour de l’API Phraseanet. À l’aide de Silex de composants Symfony2, nous avons accompagné Alchemy dans la réalisation d’un site déclinable pour leurs clients. Le produit est intégralement configurable et supporte de nombreux systèmes d’authentification (Ldap, OAuth2, Doctrine ou anonyme).