Skip to content

Conversation

fabrice102
Copy link

This commit improves the performance of RandomStringUtils:

  • Reduces the number of random bytes generated and the number of calls to the random number generator, by using a cache system AmortizedRandomBits.
  • Optimizes the case of alphanumerical strings, reducing the number of rejections in the rejection sampling.

See comments in code for details.

This commit improves the performance of RandomStringUtils:

* Reduces the number of random bytes generated and the number of calls to the random number generator, by using a cache system `AmortizedRandomBits`.
* Optimizes the case of alphanumerical strings, reducing the number of rejections in the rejection sampling.

See comments in code for details.
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 85.41667% with 7 lines in your changes missing coverage. Please review.

Please upload report for BASE (Secure_RandomStringUtils@23cb811). Learn more about missing BASE report.

Files Patch % Lines
...va/org/apache/commons/lang3/RandomStringUtils.java 82.60% 2 Missing and 2 partials ⚠️
.../org/apache/commons/lang3/AmortizedRandomBits.java 88.00% 1 Missing and 2 partials ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@                     Coverage Diff                     @@
##             Secure_RandomStringUtils       #2   +/-   ##
===========================================================
  Coverage                            ?   92.50%           
  Complexity                          ?     7773           
===========================================================
  Files                               ?      201           
  Lines                               ?    15940           
  Branches                            ?     2830           
===========================================================
  Hits                                ?    14746           
  Misses                              ?      652           
  Partials                            ?      542           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

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

Hello @fabrice102 !
Thank you for your contribution.
I'm OK with the PR, but, I'd like to resolve the discussion around the change in behavior this PR addresses first before making changes geared toward performance claims.
I've made some low-level comments.
Also, note the build failures due to checkstyle issues.
In the future, run mvn by itself to run all build checks before pushing.
TY! 😄

@fabrice102
Copy link
Author

Hi @garydgregory! Thanks for the feedback! I'll submit a new version on Monday.

* Fix 2 checkstyle errors.
* Apply suggestions from review for garydgregory#2
* Improve comments in new (non-public) class `AmortizedRandomBits` to match comments in other classes.
@garydgregory garydgregory merged commit 0ee8592 into garydgregory:Secure_RandomStringUtils Jul 3, 2024
garydgregory added a commit to apache/commons-lang that referenced this pull request Jul 6, 2024
…g() (#1235)

* Reimplement RandomStringUtils on top of SecureRandom#getInstanceStrong()

The previous implementation used  ThreadLocalRandom#current()

* Performance optimizations for RandomStringUtils

This commit improves the performance of RandomStringUtils:

* Reduces the number of random bytes generated and the number of calls to the random number generator, by using a cache system `AmortizedRandomBits`.
* Optimizes the case of alphanumerical strings, reducing the number of rejections in the rejection sampling.

See comments in code for details.

* Code style and comment improvements

* Fix 2 checkstyle errors.
* Apply suggestions from review for garydgregory#2
* Improve comments in new (non-public) class `AmortizedRandomBits` to match comments in other classes.

* Make class final

- Rename package-private class
- Whitespace
- Add null check
- Add serialVersionUID
- Remove redunant type cast
- Throw IllegalStateException, not RuntimeException
- nextBytes() should throw NullPointerException per contract
- Javadoc: Use longer lines

* Apply comments by aherbert in
55a70c8#r143834333

---------

Co-authored-by: Gary Gregory <gardgregory@gmail.com>
Co-authored-by: Fabrice Benhamouda <yfabrice@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants