Skip to content

Commit 650a8fb

Browse files
authored
random: Embed the Mt19937 and CombinedLCG state within the module globals (#13579)
These are always dynamically allocated in GINIT, thus always take up memory. By embedding them here we can avoid the dynamic allocation and additional pointer indirection accessing them. The test script: <?php for ($i = 0; $i < 9999999; $i++) mt_rand(1, 100); Appears to run slightly faster with this change applied: Before this change it always ran in just over 3 seconds, after this change I was also seeing times below 3 seconds. Howver results are too close and too jittery to state this performance improvement as a fact.
1 parent ad1dfa3 commit 650a8fb

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

ext/random/php_random.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ PHP_MSHUTDOWN_FUNCTION(random);
203203
PHP_RINIT_FUNCTION(random);
204204

205205
ZEND_BEGIN_MODULE_GLOBALS(random)
206-
php_random_status_state_combinedlcg *combined_lcg;
206+
int random_fd;
207207
bool combined_lcg_seeded;
208-
php_random_status_state_mt19937 *mt19937;
209208
bool mt19937_seeded;
210-
int random_fd;
209+
php_random_status_state_combinedlcg combined_lcg;
210+
php_random_status_state_mt19937 mt19937;
211211
ZEND_END_MODULE_GLOBALS(random)
212212

213213
PHPAPI ZEND_EXTERN_MODULE_GLOBALS(random)

ext/random/random.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ PHPAPI const php_random_algo *php_random_default_algo(void)
309309
/* {{{ php_random_default_status */
310310
PHPAPI void *php_random_default_status(void)
311311
{
312-
php_random_status_state_mt19937 *state = RANDOM_G(mt19937);
312+
php_random_status_state_mt19937 *state = &RANDOM_G(mt19937);
313313

314314
if (!RANDOM_G(mt19937_seeded)) {
315315
php_random_mt19937_seed_default(state);
@@ -390,7 +390,7 @@ PHPAPI bool php_random_hex2bin_le(zend_string *hexstr, void *dest)
390390
/* {{{ php_combined_lcg */
391391
PHPAPI double php_combined_lcg(void)
392392
{
393-
php_random_status_state_combinedlcg *state = RANDOM_G(combined_lcg);
393+
php_random_status_state_combinedlcg *state = &RANDOM_G(combined_lcg);
394394

395395
if (!RANDOM_G(combined_lcg_seeded)) {
396396
php_random_combinedlcg_seed_default(state);
@@ -472,7 +472,7 @@ PHP_FUNCTION(mt_srand)
472472
zend_long seed = 0;
473473
bool seed_is_null = true;
474474
zend_long mode = MT_RAND_MT19937;
475-
php_random_status_state_mt19937 *state = RANDOM_G(mt19937);
475+
php_random_status_state_mt19937 *state = &RANDOM_G(mt19937);
476476

477477
ZEND_PARSE_PARAMETERS_START(0, 2)
478478
Z_PARAM_OPTIONAL
@@ -615,12 +615,6 @@ PHP_FUNCTION(random_int)
615615
static PHP_GINIT_FUNCTION(random)
616616
{
617617
random_globals->random_fd = -1;
618-
619-
random_globals->combined_lcg = php_random_status_alloc(&php_random_algo_combinedlcg, true);
620-
random_globals->combined_lcg_seeded = false;
621-
622-
random_globals->mt19937 = php_random_status_alloc(&php_random_algo_mt19937, true);
623-
random_globals->mt19937_seeded = false;
624618
}
625619
/* }}} */
626620

@@ -631,12 +625,6 @@ static PHP_GSHUTDOWN_FUNCTION(random)
631625
close(random_globals->random_fd);
632626
random_globals->random_fd = -1;
633627
}
634-
635-
php_random_status_free(random_globals->combined_lcg, true);
636-
random_globals->combined_lcg = NULL;
637-
638-
php_random_status_free(random_globals->mt19937, true);
639-
random_globals->mt19937 = NULL;
640628
}
641629
/* }}} */
642630

0 commit comments

Comments
 (0)