You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex D Herbert (JIRA)" <ji...@apache.org> on 2019/04/15 19:19:00 UTC

[jira] [Commented] (RNG-77) Update the NumberFactory conversion to and from byte arrays

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

Alex D Herbert commented on RNG-77:
-----------------------------------

I have updated the {{NumberFactory}} array conversion methods to allow the {{int/long}} primitive to be directly written to the {{byte[]}} and vice versa.

The difference is essentially a change from:
{code:java}
public static byte[] makeByteArray(int[] input) {
    final int size = input.length * INT_SIZE;
    final byte[] b = new byte[size];

    for (int i = 0; i < input.length; i++) {
        final byte[] current = makeByteArray(input[i]);
        System.arraycopy(current, 0, b, i * INT_SIZE, INT_SIZE);
    }

    return b;
}
{code}
to:
{code:java}
public static byte[] makeByteArray(int[] input) {
    final int size = input.length * INT_SIZE;
    final byte[] b = new byte[size];

    for (int i = 0; i < input.length; i++) {
        putInt(input[i], b, i * INT_SIZE);
    }

    return b;
}
{code}
Here are some JMH benchmarks.

For the first set of methods convert to/from a single primitive. These have been baselined using simple methods that returned the equivalent data zero'd. The makeByteArray4/8 methods basically just allocate the byte array (zero filled). The makeInt/Long methods return a primitive value.

Here there is no difference between the new and old methods (as expected): 
||Method||Score||Error||Median||Error/Score||
|makeByteArray4|2798.981|53.639|2788.429|0.019|
|makeByteArrayFromInt|3591.130|36.824|3599.385|0.010|
|makeByteArrayFromIntOld|3574.251|52.643|3562.749|0.015|
|makeByteArray8|2878.393|104.431|2917.518|0.036|
|makeByteArrayFromLong|4171.389|32.935|4159.844|0.008|
|makeByteArrayFromLongOld|4168.477|10.231|4169.648|0.002|
|makeInt|1036.742|29.883|1027.138|0.029|
|makeIntFromByteArray|2057.354|28.005|2055.322|0.014|
|makeIntFromByteArrayOld|2102.580|14.697|2102.595|0.007|
|makeLong|1034.500|20.796|1031.875|0.020|
|makeLongFromByteArray|2951.849|59.795|2938.596|0.020|
|makeLongFromByteArrayOld|2855.122|47.336|2850.938|0.017|

The second set of methods convert arrays to arrays. Here the method with the suffix Size is the baseline where the same size array is allocated (zero filled). Here the new methods are much faster as they do not allocate an intermediate array for each primitive conversion:
||size||Method||Score||Error||Median||Error/Score||
|100|makeByteArrayFromIntSize|21933.905|385.209|21959.856|0.018|
|100|makeByteArrayFromIntArray|84473.502|1201.574|84217.821|0.014|
|100|makeByteArrayFromIntArrayOld|491590.157|1957.236|491654.783|0.004|
|100|makeByteArrayFromLongSize|46332.235|1437.031|46452.021|0.031|
|100|makeByteArrayFromLongArray|313242.844|2479.052|313355.264|0.008|
|100|makeByteArrayFromLongArrayOld|642929.047|3651.751|642326.375|0.006|
|100|makeIntArrayFromByteSize|22137.734|340.330|22200.830|0.015|
|100|makeIntArrayFromByteArray|78503.159|929.517|78294.294|0.012|
|100|makeIntArrayFromByteArrayOld|396802.537|6344.709|395586.001|0.016|
|100|makeLongArrayFromByteSize|46352.226|1006.821|46459.565|0.022|
|100|makeLongArrayFromByteArray|252091.748|3414.340|251823.096|0.014|
|100|makeLongArrayFromByteArrayOld|477807.154|6813.987|476325.225|0.014|

Conversion to/from int arrays is over 5x faster and to/from long arrays is over 2x faster.

New code is submitted in [PR 37|https://github.com/apache/commons-rng/pull/37].

> Update the NumberFactory conversion to and from byte arrays
> -----------------------------------------------------------
>
>                 Key: RNG-77
>                 URL: https://issues.apache.org/jira/browse/RNG-77
>             Project: Commons RNG
>          Issue Type: Improvement
>          Components: core
>    Affects Versions: 1.3
>            Reporter: Alex D Herbert
>            Assignee: Alex D Herbert
>            Priority: Minor
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> The {{ConstructionBenchmark}} in RNG-72 shows that constructing with a pre-computed native seed array but converted to a {{byte[]}} has a measurable slow down on the construction performance.
> This is a placeholder for an investigation into speeding up the methods for {{byte[]}} conversion in the {{NumberFactory}}.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)