2
\$\begingroup\$

I was just reviewing the source code of Nifi, see explanation and public version of the source. This got me wondering whether there are any obvious improvements here to increase the performance of the random generation.

As there are many people using this, the size of the random array could be anything, but probably it usually be somewhere in the range of 100b to 10mb. The message generation frequency will typically be between 1 and 1000 times per second.

private static final char[] TEXT_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-_=+/?.,';:\"?<>\n\t ".toCharArray();
private byte[] generateData(final ProcessContext context) {
    final int byteCount = context.getProperty(FILE_SIZE).asDataSize(DataUnit.B).intValue();

    final Random random = new Random();
    final byte[] array = new byte[byteCount];
    if (context.getProperty(DATA_FORMAT).getValue().equals(DATA_FORMAT_BINARY)) {
        random.nextBytes(array);
    } else {
        for (int i = 0; i < array.length; i++) {
            final int index = random.nextInt(TEXT_CHARS.length);
            array[i] = (byte) TEXT_CHARS[index];
        }
    }

    return array;
}

Just looking at this with my experience in other languages, I would think the inside of the loop is the most interesting. E.g. by avoiding to generate all numbes one by one, or indexing so many individual times into the set of characters.

I am looking for tips that may/will improve the speed without changing the behavior of the code too much, though some compromise on quality of randomness should be acceptable. If it looks great, then that would be also good to know of course.

----g

Update

Though I did not write these lines I do understand them. Furthermore I believe I am a maintainer of the code. Based on the suggestions here I will test the options presented and aim to improve the code and submit a pull request. (I have submitted earlier improvements to the Nifi project, so on a project level I should definitely be considered a maintainer). - Of course it would be more practical if I wrote the code myself, but based on the help section the context in which I ask this question should be OK.

\$\endgroup\$
  • 3
    \$\begingroup\$ If you are not the author of the code, you should not ask for it to be reviewed. Please see the help center for more info. codereview.stackexchange.com/help/on-topic \$\endgroup\$ – TorbenPutkonen Jun 1 at 6:54
  • \$\begingroup\$ Though I did not write these lines I do understand them. Furthermore I believe I am a maintainer of the code. Based on the suggestions here I will test the options presented and aim to improve the code and submit a pull request. (I have submitted earlier improvements to the Nifi project, so on a project level I should definitely be considered a maintainer). - Of course it would be more practical if I wrote the code myself, but based on the help section the context in which I ask this question should be OK. Hence my request to re-open the question. \$\endgroup\$ – Dennis Jaheruddin Jun 2 at 14:23
  • 1
    \$\begingroup\$ Please edit your post to include such information about how you intend to maintain/extend the code. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Jun 2 at 15:39

Browse other questions tagged or ask your own question.