Abacn commented on code in PR #41723:
URL: https://github.com/apache/beam/pull/41723#discussion_r4744454965
##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/Caches.java:
##########
Review Comment:
While the commit notes "Optimize Fn Harness Caches (fast-path weigh,
multi-segment concurrency, ClearableCache key tracking)", .concurrencyLevel(1)
is unchanged here
(Meanwhile,
CachesBenchmark.java:230-252
includes @Threads(4) contended benchmarks that are still testing a
single-segment lock.)
Recommendation: add a TODO here. In the long term, consider solve the
per-segment max-entry-size issue (e.g., switching to Caffeine, which has no
per-segment weight cap).
##########
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:
Caches.weigh() fast paths miss Beam's primary cached value types (Row, KV,
WindowedValue, List, ReadableInstant), leaving 30% CPU in jamm and 2.8× weight
inflation
In streaming pipelines using GroupIntoBatches / IcebergIO / Beam SQL /
Schema transforms, BagState<Row> calls Caches.weigh(row) on every element via
StateFetchingIterators.java:314
.
Every Row misses all fast paths, hits MEMORY_METER.measureDeep(row) (24.9
µs/row, 30.2% of total worker CPU), and double-counts the shared immutable
Row.schema (2,128 B) on every single row.
Recommendation:
- **Recommendation:**
- Add O(1) / shallow recursive fast paths in `Caches.weigh()` (with a
recursion depth cap to prevent cycles/stack overflow) for:
- `Row` (weigh `row.getValues()` elements + row/list overhead,
**excluding** shared `Schema` or amortizing it!)
- `KV<?, ?>` (`24L + weigh(k) + weigh(v)`)
- `org.joda.time.Instant` (final class, `24L`) and remaining
primitive arrays (`char[]`, `double[]`, `float[]`, `short[]`, `boolean[]`)
- Optionally `ArrayList` / `ImmutableList` (non-self-referential,
bounded depth).
- Alternatively (or additionally, in a follow-up),
[`BagUserState.asyncClose()`](beam/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/BagUserState.java#L135-L170)
already encodes every `newValue` into `ByteStringOutputStream` right before
calling `oldValues.append(newValues)`—passing a `WeightedList` using the
already-known encoded byte size (plus small object overhead) would completely
avoid re-weighing every element via `jamm`.
- Add `Row` and `KV` cases to
[`CachesBenchmark.java`](beam/sdks/java/harness/jmh/src/main/java/org/apache/beam/fn/harness/jmh/CachesBenchmark.java)
so benchmarks reflect real state-caching workloads rather than mostly
primitive/String hits.
--
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]