You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by yh...@apache.org on 2024/03/22 03:58:43 UTC

(beam) branch master updated: Use autovalue's @Memoized in ExponentialBuckets (#30676)

This is an automated email from the ASF dual-hosted git repository.

yhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 960a29e01a0 Use autovalue's @Memoized in ExponentialBuckets (#30676)
960a29e01a0 is described below

commit 960a29e01a07a3d61f0804bf159ea41db40ca75f
Author: JayajP <ja...@google.com>
AuthorDate: Thu Mar 21 20:58:37 2024 -0700

    Use autovalue's @Memoized in ExponentialBuckets (#30676)
---
 .../org/apache/beam/sdk/util/HistogramData.java    | 30 ++++++++++++++--------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java
index b235e774fed..65ccda06be6 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java
@@ -18,6 +18,7 @@
 package org.apache.beam.sdk.util;
 
 import com.google.auto.value.AutoValue;
+import com.google.auto.value.extension.memoized.Memoized;
 import java.io.Serializable;
 import java.math.RoundingMode;
 import java.util.Arrays;
@@ -382,22 +383,31 @@ public class HistogramData implements Serializable {
     // Maximum number of buckets that is supported when 'scale' is zero.
     private static final int ZERO_SCALE_MAX_NUM_BUCKETS = 32;
 
-    public abstract double getBase();
+    @Memoized
+    public double getBase() {
+      return Math.pow(2, Math.pow(2, -getScale()));
+    }
 
     public abstract int getScale();
 
     /**
-     * Set to 2**scale which is equivalent to 1/log_2(base). Precomputed to use in {@code
+     * Set to 2**scale which is equivalent to 1/log_2(base). Memoized to use in {@code
      * getBucketIndexPositiveScale}
      */
-    public abstract double getInvLog2GrowthFactor();
+    @Memoized
+    public double getInvLog2GrowthFactor() {
+      return Math.pow(2, getScale());
+    }
 
     @Override
     public abstract int getNumBuckets();
 
-    /* Precomputed since this value is used everytime a datapoint is recorded. */
+    /* Memoized since this value is used everytime a datapoint is recorded. */
+    @Memoized
     @Override
-    public abstract double getRangeTo();
+    public double getRangeTo() {
+      return Math.pow(getBase(), getNumBuckets());
+    }
 
     public static ExponentialBuckets of(int scale, int numBuckets) {
       if (scale < MINIMUM_SCALE) {
@@ -414,12 +424,8 @@ public class HistogramData implements Serializable {
             String.format("numBuckets should be positive: %d", numBuckets));
       }
 
-      double invLog2GrowthFactor = Math.pow(2, scale);
-      double base = Math.pow(2, Math.pow(2, -scale));
       int clippedNumBuckets = ExponentialBuckets.computeNumberOfBuckets(scale, numBuckets);
-      double rangeTo = Math.pow(base, clippedNumBuckets);
-      return new AutoValue_HistogramData_ExponentialBuckets(
-          base, scale, invLog2GrowthFactor, clippedNumBuckets, rangeTo);
+      return new AutoValue_HistogramData_ExponentialBuckets(scale, clippedNumBuckets);
     }
 
     /**
@@ -512,6 +518,10 @@ public class HistogramData implements Serializable {
     public double getRangeFrom() {
       return 0;
     }
+
+    @Memoized
+    @Override
+    public abstract int hashCode();
   }
 
   @AutoValue