Skip to content
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

Optimize RandomApi::nextString #52

Merged
merged 6 commits into from
Sep 14, 2024
Merged

Conversation

isaacl
Copy link
Member

@isaacl isaacl commented Sep 12, 2024

Avoid boxing of int (for comprehension) and char (with the scala StringBuilder). Use an Array instead of capacity specified StringBuilder.

Generally it's faster to use java StringBuilder instead of the scala StringBuilder wrapper. But Arrays are even faster when you know the length in advance.

Side note: Random.nextInt is twice as fast for powers of 2, so if we expanded characters to 64 instead of 62, this method could be even faster.

@isaacl isaacl force-pushed the fasterNextString branch 3 times, most recently from d95654a to 6589ed2 Compare September 13, 2024 20:15
The test isn't meaningful anymore.  ThreadLocalRandom uses a singleton interface
so it's not possible to check that different threads are using a different instance.
The test isn't meaningful anymore.  ThreadLocalRandom uses a singleton interface
so it's not possible to check that different threads are using a different instance.
Avoid boxing of int (for comprehension) and char (with the scala
StringBuilder).  Use an Array instead of capacity specified StringBuilder.

Generally it's faster to use java StringBuilder instead of the scala
StringBuilder wrapper. But Arrays are even faster when you know the
length in advance.

Side note: Random.nextInt is twice as fast for powers of 2, so if we expanded
characters to 64 instead of 62, this method could be even faster.

Also:
- Clean up oneOf
- Optimize odds
@ornicar ornicar merged commit f6aa9b4 into lichess-org:master Sep 14, 2024
1 check passed
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.

2 participants