acrites opened a new pull request, #41692:
URL: https://github.com/apache/beam/pull/41692

   ### Summary
   
   Optimizes element byte-size sampling in the Java Fn Harness 
(`PCollectionConsumerRegistry` and `ElementByteSizeObserver`) to reduce object 
allocations, autoboxing, and RNG overhead during active sampling while 
improving reservoir skip-gap numerical precision and sustaining a `0.001%` 
minimum sampling floor on long-lived `BundleProcessor`s.
   
   ### Changes
   
   * **Primitive `update(long)` and `reset()` on `ElementByteSizeObserver`**:
     * Adds `ElementByteSizeObserver.update(long)` so 
`Coder.registerByteSizeObserver` (and primitive widening callers) accumulate 
byte sizes without `Long`/`Integer` autoboxing.
     * Adds `ElementByteSizeObserver.reset()` (and updates 
`CoderProperties.TestElementByteSizeObserver.reset()`) to clear accumulated 
size, `isLazy`, and `scalingFactor` (`1.0`) without reporting, enabling 
observer reuse across samples without reallocating `ElementByteSizeObserver` 
(whose `java.util.Observable` base class allocates a `Vector` per instance).
   * **Observer reuse & lazy-observer isolation in 
`SampleByteSizeDistribution`**:
     * Reuses a single `ByteSizeObserver` across eager samples 
(`!observer.getIsLazy()`).
     * When a coder marks an observer lazy (`setLazy()`), detaches it to 
`pendingLazyObserver` and allocates a fresh `reusableObserver` so late or 
escaped iterable notifications cannot corrupt subsequent elements; also 
detaches `reusableObserver` if a coder throws after calling `setLazy()`, and 
clears `pendingLazyObserver` via `discardPendingLazyUpdate()` if a downstream 
consumer throws.
   * **Reservoir gap-sampling optimizations & `1_000_000` token cap**:
     * Replaces per-instance `java.util.Random` (`AtomicLong` CAS on seed 
initialization and draws) with `ThreadLocalRandom.current()` by default while 
retaining an injectable `Random` constructor for deterministic tests.
     * Splits `shouldSampleElement()` and `tryUpdate()` into compact fast/slow 
paths to keep the steady-state non-sampled path inline-friendly.
     * Replaces `Math.log(1.0 - RESERVOIR_SIZE / (double) samplingToken)` and 
`(int) gap` with `Math.log1p(...)` and overflow-safe `long` arithmetic.
     * Caps `samplingToken` at `SAMPLING_TOKEN_UPPER_BOUND = 1_000_000` (`10 / 
1,000,000 = 0.001%` floor, matching `StateBackedIterable` and 
`ElementByteSizeCounter`) so cached `BundleProcessor` instances continue 
sampling byte sizes across long-running streaming bundles rather than decaying 
toward zero.
   * **Multiplexing consumer fix**:
     * Hoists `sampledByteSizeDistribution.finishLazyUpdate()` outside the 
consumer loop in `MultiplexingMetricTrackingFnDataReceiver.accept` so lazy 
byte-size observers advance once after all consumers run rather than on the 
first consumer iteration.
   
   ### Benchmarks
   
   Back-to-back A/B JMH runs on JDK `11.0.28` with `-prof gc` comparing this PR 
against `master` (`5508f743ec`):
   
   #### 1. `PCollectionConsumerRegistryBenchmark` (Targeted Microbenchmarks)
   
   Mode: `AverageTime` (`ns/op` — lower is better), 2 forks, 99.9% CI:
   
   | Benchmark | Baseline (`master`) | This PR | Time Delta | Alloc Baseline 
(`B/op`) | Alloc This PR (`B/op`) | Alloc Delta |
   |---|---:|---:|---:|---:|---:|---:|
   | `acceptActiveSamplingPhase` | `171.649 ± 28.483` | `133.595 ± 24.573` | 
**-22.17%** | `117.412 ± 15.514` | `73.414 ± 12.499` | **-37.47%** |
   | `acceptMultiplexingFixedWindowMix` | `123.728 ± 6.035` | `116.188 ± 8.956` 
| **-6.09%** | `18.003 ± 0.008` | `18.003 ± 0.007` | `+0.00%` |
   | `acceptSingleConsumerFixedWindowMix` | `47.284 ± 2.227` | `45.173 ± 1.258` 
| **-4.47%** | `18.001 ± 0.003` | `18.001 ± 0.003` | `+0.00%` |
   | `acceptSingleConsumerGlobalWindow` | `34.175 ± 4.570` | `33.562 ± 1.281` | 
**-1.79%** | `0.001 ± 0.002` | `0.001 ± 0.002` | `~0 B/op` |
   
   * During active reservoir sampling (`acceptActiveSamplingPhase`), reusing 
`ByteSizeObserver`, avoiding `Long` autoboxing, and using `ThreadLocalRandom` 
reduce latency by **22.2%** (`-38.1 ns/op`) and allocation by **44.0 B/op** 
(`-37.5%`).
   * Steady-state hot paths (while sustaining the `0.001%` sampling floor past 
`1,000,000` elements) show no regression in throughput or allocation.
   
   ### Testing
   
   * Added `ElementByteSizeObserverTest` covering `update(long)`, 
`scalingFactor` handling, `reset()`, and 
`CoderProperties.TestElementByteSizeObserver.reset()`.
   * Added unit tests in `PCollectionConsumerRegistryTest` covering eager 
observer reuse, eager vs. partial-lazy failure recovery, 
`discardPendingLazyUpdate()`, lazy observer detachment across single and 
multiplexing receivers, sublinear reservoir decay, `SAMPLING_TOKEN_UPPER_BOUND` 
(`1,000,000`) steady-state sampling, and `Long.MAX_VALUE` clamping.


-- 
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]

  • [PR] [Java] Optimize byte size... acrites (via GitHub) via github

Reply via email to