[ 
https://issues.apache.org/jira/browse/MATH-1306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15072260#comment-15072260
 ] 

Rostislav Krasny commented on MATH-1306:
----------------------------------------

{quote}Since the new feature is about reusing the same array to fill different 
regions of it at different times, why not also keep the small array for 
reuse?{quote}
This is not about reusing the same array but about having a direct access to 
the destination array. And reusing the same intermediate array is not always 
possible or practical. But even without temporary array allocation in each loop 
iteration the {{System.arraycopy()}} also takes time, although it is a native 
method.

{quote}Could you please provide a use-case of such a simplification?{quote}
Cryptography. I had seen a cryptographic software that uses a method with a 
signature similar to the proposed.

{quote}I only see that the signature is more complex; the purpose of Commons 
Math is not to cover all hypothetical cases.{quote}
This is very common to have methods that works with regions of arrays or 
collections. The mentioned here {{System.arraycopy()}} or methods of 
{{java.util.Arrays}} class. And I'm sure this list could be much longer.

I just found the proposed nextByte() method signature already exists in 
{{gnu.crypto.util.PRNG}} pseudo-random number generator.
[http://www.gnu.org/software/gnu-crypto/manual/api/gnu/crypto/util/PRNG.html]

Also there is a not exact but very similar method signature in 
{{RandomData.OneShot}} of javacard
[https://docs.oracle.com/javacard/3.0.5/api/javacard/security/RandomData.OneShot.html]#nextBytes%28byte[],%20short,%20short%29

Apache Tomcat has a tricky method with similar functionality just because 
{{java.lang.Random}} doesn't have the proposed method
[https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/tribes/util/UUIDGenerator.html]#nextBytes%28byte[],%20int,%20int,%20java.util.Random%29

So why not to add the proposed method signature into CM as well?


P.S. why URLs with # are not supported by Jira?
[https://docs.oracle.com/javacard/3.0.5/api/javacard/security/RandomData.OneShot.html#nextBytes%28byte[],%20short,%20short%29]

> Add public void nextBytes(byte[] bytes, int position, int length) method into 
> the RandomGenerator interface
> -----------------------------------------------------------------------------------------------------------
>
>                 Key: MATH-1306
>                 URL: https://issues.apache.org/jira/browse/MATH-1306
>             Project: Commons Math
>          Issue Type: Improvement
>    Affects Versions: 3.5
>            Reporter: Rostislav Krasny
>         Attachments: MersenneTwister1305.java, MersenneTwister1306.java
>
>
> I propose to add {{public void nextBytes(byte[] bytes, int position, int 
> length)}} method into the {{RandomGenerator}} interface.
> Rationality: to improve performance and memory usage in cases when one needs 
> to fill only a specified region of a byte array. Today to do that you need to 
> use a temporary byte array and copy it by yourself into the specified region 
> of the destination byte array.
> I propose the following code, based on the code of {{BitsStreamGenerator}} 
> commited in MATH-1305
> {code:java}
>       @Override
>       public void nextBytes(byte[] bytes) {
>               nextBytesFill(bytes, 0, bytes.length);
>       }
>       // TODO add this method into RandomGenerator interface
>       //@Override
>       public void nextBytes(byte[] bytes, int position, int length) {
>               if (position < 0 || position > bytes.length - 1) {
>                       throw new 
> OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, position, 0, 
> bytes.length - 1);
>               }
>               if (length < 0 || length > bytes.length - position) {
>                       throw new 
> OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, length, 0, 
> bytes.length - position);
>               }
>               nextBytesFill(bytes, position, length);
>       }
>       private void nextBytesFill(byte[] bytes, int position, int length) {
>               int index = position;
>               // Position plus multiple 4 part of length (i.e. length with 
> two least significant bits unset).
>               final int indexLoopLimit = position + (length & 0x7ffffffc);
>               // Start filling in the byte array, 4 bytes at a time.
>               while (index < indexLoopLimit) {
>                       final int random = next(32);
>                       bytes[index++] = (byte) random;
>                       bytes[index++] = (byte) (random >>> 8);
>                       bytes[index++] = (byte) (random >>> 16);
>                       bytes[index++] = (byte) (random >>> 24);
>               }
>               final int indexLimit = position + length;
>               
>               // Fill in the remaining bytes.
>               if (index < indexLimit) {
>                       int random = next(32);
>                       while (true) {
>                               bytes[index++] = (byte) random;
>                               if (index < indexLimit) {
>                                       random >>>= 8;
>                               } else {
>                                       break;
>                               }
>                       }
>               }
>       }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to