Skip to content

random: Embed the Mt19937 and CombinedLCG state within the module globals #13579

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

Merged
merged 1 commit into from
Mar 4, 2024

Conversation

TimWolla
Copy link
Member

@TimWolla TimWolla commented Mar 2, 2024

Somewhat related to #13577


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.

…bals

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.
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

Copy link
Contributor

@zeriyoshi zeriyoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lacked this perspective at the time, very good!

@TimWolla TimWolla merged commit 650a8fb into php:master Mar 4, 2024
@TimWolla TimWolla deleted the random-globals-embed-state branch March 4, 2024 16:55
@TimWolla
Copy link
Member Author

TimWolla commented Mar 6, 2024

For reference: This fails with MSAN: https://github.com/php/php-src/actions/runs/8165488968/job/22322762350. I'll fix this tonight.

TimWolla added a commit to TimWolla/php-src that referenced this pull request Mar 6, 2024
…lt_status()`

This is a follow-up fix for phpGH-13579. The issue was detected in the nightly
MSAN build.
TimWolla added a commit that referenced this pull request Mar 6, 2024
…lt_status()` (#13608)

This is a follow-up fix for GH-13579. The issue was detected in the nightly
MSAN build.
TimWolla added a commit to TimWolla/php-src that referenced this pull request Mar 12, 2024
…lt_status()` (php#13608)

This is not just an issue due to missing initialization since moving the state
struct directly into the module globals. In earlier versions changing the mode
to `MT_RAND_PHP` within a single request would also affect the mode for
subsequent requests.

Original commit message follows:

This is a follow-up fix for phpGH-13579. The issue was detected in the nightly
MSAN build.

(cherry picked from commit bf0abd1)
TimWolla added a commit that referenced this pull request Mar 13, 2024
…lt_status()` (#13690)

This is not just an issue due to missing initialization since moving the state
struct directly into the module globals. In earlier versions changing the mode
to `MT_RAND_PHP` within a single request would also affect the mode for
subsequent requests.

Original commit message follows:

This is a follow-up fix for GH-13579. The issue was detected in the nightly
MSAN build.

(cherry picked from commit bf0abd1)
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.

3 participants