mdvorsky commented on a change in pull request #4143: Faster implementation of
nexmark.Generator.nextExactString().
URL: https://github.com/apache/beam/pull/4143#discussion_r153266699
##########
File path:
sdks/java/nexmark/src/main/java/org/apache/beam/sdk/nexmark/sources/Generator.java
##########
@@ -353,8 +353,16 @@ private static String nextString(Random random, int
maxLength) {
/** Return a random string of exactly {@code length}. */
private static String nextExactString(Random random, int length) {
StringBuilder sb = new StringBuilder();
+ int rnd = 0;
+ int n = 0; // number of random characters left in rnd
while (length-- > 0) {
- sb.append((char) ('a' + random.nextInt(26)));
+ if (n == 0) {
+ rnd = random.nextInt();
+ n = 6; // log_26(2^31)
+ }
+ sb.append((char) ('a' + rnd % 26));
+ rnd /= 26;
Review comment:
The way I imagine it is that we're generating a number in base 26. We used
to generate it one "digit" at a time, now we generate 6 digits at the same
time, so the probability of a given resulting string should be roughly the
same. E.g., now generating rnd=0 should be the same as previously generating 0
six times in a row.
To make it exactly the same, I'd need to use 26^6 as the upper value for
rnd, instead of Integer.MAX_VALUE. I could make that change if you'd like.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services