Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for uuidv4 generation in the 19th character #54027

Closed

Conversation

rafageist
Copy link

Q A
Branch? 7.1
Bug fix? no
New feature? no
Deprecations? no
Issues Fix #
License MIT

Description of Changes

This pull request enhances the generation of the 19th character in UUIDv4 in the Symfony UuidV4 class. The Process ID (PID) has been implemented as a sufficiently random and efficient source.

Implementation Details

  • A Singleton has been introduced to store the PID and avoid its repeated retrieval during script execution.
  • The character at position 19 is now set using the remainder of the PID divided by 4, ensuring a uniform distribution among ['8','9','a','b'].

Reasons for the Improvement

  • The generation of the 19th character is done more efficiently by using the PID as a random source.
  • The use of the Singleton ensures the PID is obtained only once during script execution.

Tested Variants

  • Variants 1 to 6 have been tested, and the PID-based Variant 7 has shown improvements in efficiency.

Tests Performed

Performance tests have been conducted, and the results indicate improvements in efficiency with this implementation.

It was determined that the third approach provided the best performance. Additional testing and validation are encouraged.

Benchmark & details

https://github.com/rafageist/sf-uuidv4-bench

@carsonbot carsonbot added this to the 7.1 milestone Feb 21, 2024
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@derrabus
Copy link
Member

Don't we lose randomness with this change? Let's say I run an importer that writes 10k new records and generate a UUID for each of them, they'll all have the same character at position 19, right? Is that desirable?

@rafageist
Copy link
Author

Don't we lose randomness with this change? Let's say I run an importer that writes 10k new records and generate a UUID for each of them, they'll all have the same character at position 19, right? Is that desirable?

You've raised a valid point, and I appreciate your attention to detail. The current implementation indeed could result in the same character at position 19 for UUIDs generated in the same batch. I hadn't considered scenarios where multiple UUIDs are generated simultaneously, potentially leading to less randomness in that specific position.

To address this, I'm exploring ways to introduce an additional level of randomness by incorporating a seed or another source of randomness. This should ensure that even within the same batch, the character at position 19 is not consistently the same. Your input has been invaluable in refining this approach, and I'll make sure to account for such scenarios in the final implementation. Thank you for bringing this up!

@rafageist rafageist closed this Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants