Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-1305

Improve performance of nextBytes() method of BitsStreamGenerator and AbstractRandomGenerator

Rank to TopRank to BottomBulk Copy AttachmentsBulk Move AttachmentsVotersWatch issueWatchersConvert to sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.0, 3.5, 3.6
    • 3.6
    • None
    • None
    • Patch, Important

    Description

      I propose to use this code in BitsStreamGenerator

      	@Override
      	public void nextBytes(byte[] bytes) {
      		int index = 0;
      
      		// multiple 4 part of length, i.e. length with two least significant bits unset
      		int max = bytes.length & 0x7ffffffc;
      
      		// start filling the byte array with tetrads of bytes
      		while (index < max) {
      			int random = next(32);
      			bytes[index++] = (byte) random;
      			bytes[index++] = (byte) (random >>> 8);
      			bytes[index++] = (byte) (random >>> 16);
      			bytes[index++] = (byte) (random >>> 24);
      		}
      
      		// fill the remains bytes
      		if (index < bytes.length) {
      			int random = next(32);
      			while (true) {
      				bytes[index++] = (byte) random;
      				if (index < bytes.length) {
      					random >>>= 8;
      				} else {
      					break;
      				}
      			}
      		}
      	}
      

      I also propose to use the same code but with nextInt() calls instead of next(32) in the AbstractRandomGenerator. This implementation improves performance and fixes inconsistency bugs in those two classes discussed in the MATH-1300. It is also quite simple and well commented.

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            rosti.bsd Rostislav Krasny
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment