random: Optimize Randomizer::getBytes() (#15228)

This patch greatly improves the performance for the common case of using a
64-bit engine and requesting a length that is a multiple of 8.

It does so by providing a fast path that will just `memcpy()` (which will be
optimized out) the returned uint64_t directly into the output buffer,
byteswapping it for big endian architectures.

The existing byte-wise copying logic was mostly left alone. It only received an
optimization of the shifting and masking that was previously applied to
`Randomizer::getBytesFromString()` in 1fc2ddc996.

Co-authored-by: Saki Takamachi <saki@php.net>
This commit is contained in:
Tim Düsterhus 2024-08-05 19:12:29 +02:00 committed by GitHub
parent 2f27e0b2ab
commit 31e2d2b86c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 5 deletions

View file

@ -81,8 +81,8 @@ typedef struct _php_random_status_state_user {
} php_random_status_state_user;
typedef struct _php_random_result {
const uint64_t result;
const size_t size;
uint64_t result;
size_t size;
} php_random_result;
typedef struct _php_random_algo {