reuvenlax commented on code in PR #41723:
URL: https://github.com/apache/beam/pull/41723#discussion_r4747169912


##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/Caches.java:
##########
@@ -68,23 +73,90 @@ public final class Caches {
 
   private static final MemoryMeter MEMORY_METER = 
MemoryMeter.builder().build();
 
+  /**
+   * Exact {@code LiteralByteString} class ({@link ByteString#EMPTY}). 
Restricting the fast path to
+   * this concrete class ensures sliced {@code BoundedByteString}, {@code 
RopeByteString}, and
+   * {@code NioByteString} instances fall back to deep measurement so retained 
backing buffers are
+   * not under-counted.
+   */
+  private static final Class<?> LITERAL_BYTE_STRING_CLASS = 
ByteString.EMPTY.getClass();
+
+  private static final AtomicBoolean LOGGED_JAMM_WARNING = new 
AtomicBoolean(false);
+
   /** The size of a reference. */
   public static final long REFERENCE_SIZE = 8;
 
-  /** Returns the amount of memory in bytes the provided object consumes. */
+  private static long alignObjectSize(long bytes) {
+    return (bytes + 7L) & ~7L;
+  }
+
+  /**
+   * Returns an estimate of the amount of memory in bytes the provided object 
consumes.
+   *
+   * <p>Uses heuristic fast paths (assuming 64-bit compressed OOPs, 8-byte 
alignment) for common
+   * immutable/primitive types ({@link String}, {@code byte[]}, literal {@link 
ByteString}, boxed
+   * primitives, primitive arrays, and {@link Enum}) and delegates other 
objects (including sliced
+   * {@link ByteString} subclasses) to {@link MemoryMeter#measureDeep}. 
Objects implementing {@link
+   * Weighted} return their reported weight plus reference/header overhead.
+   */
   public static long weigh(@Nullable Object o) {
     if (o == null) {
       return REFERENCE_SIZE;
     }
+    // Check common final/array types via exact class equality before the 
`instanceof Weighted`

Review Comment:
   added a bounded-depth fast path. The BagUserState suggestion was already 
implemented in pr/41540



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to