Skip to content

[random] split getInt() with no arguments into nextInt() #9057

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

Closed
wants to merge 7 commits into from

Conversation

zeriyoshi
Copy link
Contributor

The Random Extension 5.x and Random Extension Improvement proposals passed, but with the problem of argument overloading.

#8094

As noted by some, argument overloading is now discouraged and should not be implemented anew.

https://externals.io/message/118163#118269
https://github.com/php/php-src/pull/8094/files#r919693108

cc / @TimWolla @kocsismate
I am sorry for the review you gave us.
I have split the PR and corrected the points you pointed out.

Comment on lines 35 to 37
if ($e->getMessage() !== 'Generated value exceeds size of int') {
die($engine::class . ': nextInt: failure: {$e->getMesasge()}');
throw $e;
Copy link
Contributor

@guilliamxavier guilliamxavier Jul 20, 2022

Choose a reason for hiding this comment

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

  • before this if, other similar tests (compatibility_user.phpt) first do a
    if (\PHP_INT_SIZE >= 8) {
        throw $e;
    }
  • typo in the die: $e->getMesasge() should be $e->getMessage()
  • bug in the die: single-quotes should be double-quotes, or (maybe better) replace string interpolation with concatenation
  • this throw is unreachable (after a die)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My brain was shutting down.
I will fix it. I am ashamed. 😭

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed: d78c647

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the case of nextInt(), the method is checked for exception messages, since conditional branching by PHP_INT_SIZE would completely prevent the method from being tested

Copy link
Contributor

@guilliamxavier guilliamxavier Jul 20, 2022

Choose a reason for hiding this comment

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

OK for the fixes (of my last 3 points),
but I don't understand your last comment (about my first point) 😕 I saw

} catch (\RuntimeException $e) {
if (\PHP_INT_SIZE >= 8) {
throw $e;
}
if ($e->getMessage() !== 'Generated value exceeds size of int') {
throw $e;
}
}
(2 occurrences in that file) so was wondering if here you should do something like

         } catch (\RuntimeException $e) {
+            if (\PHP_INT_SIZE >= 8) {
+                die($engine::class . ": nextInt: failure: {$e->getMessage()}");
+            }
             if ($e->getMessage() !== 'Generated value exceeds size of int') {
                 die($engine::class . ": nextInt: failure: {$e->getMessage()}");
             }
        }

? or maybe merge the two ifs into one if ($e->getMessage() !== 'Generated value exceeds size of int' || \PHP_INT_SIZE >= 8) (in both files)? (just questions BTW, not blocking this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry. I was confused again.
This is completely unnecessary and should be removed. If it is a good thing to modify at the same time as this PR, I will do so now. 😭

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry. I was confused again.

No big deal 😄

This is completely unnecessary and should be removed.

Well, I thought that it was at least testing that nextInt() doesn't throw a "Generated value exceeds size of int" exception on 64-bit systems, but you know better 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed: 7c41dd9

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine (I guess, given

result = randomizer->algo->generate(randomizer->status);
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
and
/* Guard for over 64-bit results */
if (size > sizeof(uint64_t)) {
size = sizeof(uint64_t);
}
status->last_generated_size = size;
and that sizeof(zend_long) is equal to sizeof(uint64_t) on 64-bit systems), but as I said, there were 2 occurrences in that file (compatibility_user.phpt), but you only removed the first one; did you "forget" to also remove the second one (
if (\PHP_INT_SIZE >= 8) {
throw $e;
}
) or is it somewhat useful for Xoshiro256StarStar (although it apparently wasn't for PcgOneseq128XslRr64)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your calm and measured point of view. I was totally confused. From what I see, the guard test is unnecessary as it is tested in get_bytes.phpt. I have removed the if completely.

fixed: 5144026

@zeriyoshi zeriyoshi force-pushed the upstream/split_randomizer_method branch from 53e82e4 to d78c647 Compare July 20, 2022 13:47
@TimWolla TimWolla self-requested a review July 20, 2022 15:58
@zeriyoshi zeriyoshi force-pushed the upstream/split_randomizer_method branch from 7c41dd9 to 5144026 Compare July 21, 2022 14:21
Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

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

Thank you! I'm very much in favor of splitting the method in two. The patch looks good to me.

Copy link
Member

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

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

The diff looks reasonable to me. Just one remark to clarify that nextInt() will return positive numbers only, as the most-significant bit is shifted out.

I guess the release managers should decide on this one?

@cmb69
Copy link
Member

cmb69 commented Jul 22, 2022

I guess the release managers should decide on this one?

@ramsey, @saundefined, @adoy, any objections?

If this gets merged, the RFC should probably be amended, so that the doc team won't put outdated information in the migration guide.

@ramsey
Copy link
Member

ramsey commented Jul 22, 2022

Will this change the RFC in any way that would have caused any voters to vote "no" had this been part of the original RFC?

@zeriyoshi
Copy link
Contributor Author

@ramsey

Will this change the RFC in any way that would have caused any voters to vote "no" had this been part of the original RFC?

Since the method is only separated, I don't think anyone would be negative about it, or that it would be so negative that it would be rejected if a vote were actually taken.

Nevertheless, this is a very exceptional measure and may set a bad precedent. As PHP 8.2 Release Manager, can you hearing the ML if they have any objections to this change?

I apologize for the extra hassle this has caused.

@zeriyoshi zeriyoshi force-pushed the upstream/split_randomizer_method branch from 8885bad to a92af01 Compare July 23, 2022 15:23
@guilliamxavier
Copy link
Contributor

guilliamxavier commented Jul 25, 2022

Specific ML thread (created shortly after this PR): https://externals.io/message/118290
(only positive feedback so far, in that thread and the previous one)

Nevertheless, this is a very exceptional measure and may set a bad precedent

There has already been precedents, e.g. https://externals.io/message/117136, not necessarily "bad" ;)

@TimWolla
Copy link
Member

It might be useful to get a decision here, so that this can go into Beta 2 (or not).

Since the method is only separated, I don't think anyone would be negative about it, or that it would be so negative that it would be rejected if a vote were actually taken.

I agree with zeriyoshi's argument here. The same functionality is still available, just separated into two independent methods with a cleaner method signature for each of them.

@cmb69
Copy link
Member

cmb69 commented Jul 30, 2022

If there are no objections, I'll merge this on Monday.

@zeriyoshi, could you please fix the merge conflicts?

@zeriyoshi zeriyoshi force-pushed the upstream/split_randomizer_method branch from a92af01 to 908cb0b Compare July 30, 2022 12:00
@cmb69 cmb69 closed this in 4e92c74 Aug 1, 2022
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.

6 participants