Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8035561

Optimize the average operation if the pipeline is sized

    XMLWordPrintable

Details

    Description

      If a pipeline is of a known size then the average operation need not maintain a count of the elements.

      For example, given the following pipeline:

        double[] d =
        DoubleStream ds = DoubleStream.of(d);
        double a = ds.average().getOrElse(0.0);

      The size of the pipeline is the size of the array.

      The implementation of average is currently:

              double[] avg = collect(() -> new double[4],
                                     (ll, d) -> {
                                         ll[2]++;
                                         Collectors.sumWithCompensation(ll, d);
                                         ll[3] += d;
                                     },
                                     (ll, rr) -> {
                                         Collectors.sumWithCompensation(ll, rr[0]);
                                         Collectors.sumWithCompensation(ll, rr[1]);
                                         ll[2] += rr[2];
                                         ll[3] += rr[3];
                                     });

      and it can be implemented as follows when the size is known:

        sum() / pipeline_size

      (Assuming it is easy to surface the size of the pipeline, otherwise a special op is required to get access to that information).

      Attachments

        Issue Links

          Activity

            People

              psandoz Paul Sandoz
              psandoz Paul Sandoz
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: