FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-118392: Update genrand_uint32 to use atomic operation by corona10 · Pull Request #118393 · python/cpython · GitHub

/ cpython Public

gh-118392: Update genrand_uint32 to use atomic operation - #118393

Closed
corona10 wants to merge 1 commit into
python:mainfrom
corona10:gh-118392
Closed

gh-118392: Update genrand_uint32 to use atomic operation#118393
corona10 wants to merge 1 commit into
python:mainfrom
corona10:gh-118392

Conversation

corona10 commented Apr 29, 2024
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

corona10 commented Apr 29, 2024
edited
Loading

Copy link
Copy Markdown
Member Author

@pitrou @colesbury cc @rhettinger
I am not sure if this change will be safe(or even acceptable) or if it needs some changes for a better, safe random algorithm.

Comment thread Modules/_randommodule.c
Comment on lines +143 to +144
int index = FT_ATOMIC_LOAD_INT(self->index);
if (index >= N) { /* generate N words at one time */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't think this will work well. This can end up returning the same generated numbers to different threads, which we don't want.

corona10 Apr 29, 2024
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Hmm in that case, should we change the algorithm(thread-safe one) or fine-grained locking for this use case?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't think there's a simple fix to the random module.

I commented on the issue. It'd be helpful to document the behavior and provide alternatives. For example, Java's java.util.Random includes the following comment:

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I commented on the issue. It'd be helpful to document the behavior and provide alternatives. For example, Java's java.util.Random includes the following comment:

Okay sounds acceptable :)

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

2 participants


Back | FazBrowse Home | New Git URL