scwhittle commented on code in PR #36742: URL: https://github.com/apache/beam/pull/36742#discussion_r2498367603
########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/ThreadLocalByteStringOutputStream.java: ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.runners.dataflow.worker.util; + +import java.lang.ref.SoftReference; +import java.util.function.Function; +import javax.annotation.concurrent.ThreadSafe; +import org.apache.beam.sdk.annotations.Internal; +import org.apache.beam.sdk.util.ByteStringOutputStream; +import org.checkerframework.checker.nullness.qual.Nullable; + +@Internal +@ThreadSafe +/* + * A utility class for caching a thread-local {@link ByteStringOutputStream}. + */ +public class ThreadLocalByteStringOutputStream { + + private static final ThreadLocal<@Nullable RefHolder> threadLocalRefHolder = new ThreadLocal<>(); + + // Private constructor to prevent instantiations from outside. + private ThreadLocalByteStringOutputStream() {} + + /** + * Executes the given function with a thread-local {@link ByteStringOutputStream}. If the thread + * local stream is already in use, a new one is used. The streams are cached and reused across + * calls. Callers should not keep a reference to the stream after the function returns. + */ + public static <T> T withThreadLocalStream(Function<ByteStringOutputStream, T> function) { + RefHolder refHolder = getRefHolderFromThreadLocal(); + ByteStringOutputStream stream; + boolean releaseThreadLocal; + if (refHolder.inUse) { + // If the thread local stream is already in use, create a new one Review Comment: should we have some non-thread-local pool we use in this case? ########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/state/WindmillBag.java: ########## @@ -165,18 +166,27 @@ public Windmill.WorkItemCommitRequest persistDirectly(WindmillStateCache.ForKeyA if (bagUpdatesBuilder == null) { bagUpdatesBuilder = commitBuilder.addBagUpdatesBuilder(); } - for (T value : localAdditions) { - ByteStringOutputStream stream = new ByteStringOutputStream(); - // Encode the value - elemCoder.encode(value, stream, Coder.Context.OUTER); - ByteString encoded = stream.toByteString(); - if (cachedValues != null) { - // We'll capture this value in the cache below. - // Capture the value's size now since we have it. - encodedSize += encoded.size(); - } - bagUpdatesBuilder.addValues(encoded); - } + final Windmill.TagBag.Builder finalBagUpdatesBuilder = bagUpdatesBuilder; + @Nullable + Object unused = + ThreadLocalByteStringOutputStream.withThreadLocalStream( Review Comment: what about some AutoCloseable instead of taking a lambda? ``` try (RefHolder s = ThreadLocalByteStringOutputStream.acquire()) { // use s.stream() } ``` ########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/state/WindmillBag.java: ########## @@ -165,18 +166,27 @@ public Windmill.WorkItemCommitRequest persistDirectly(WindmillStateCache.ForKeyA if (bagUpdatesBuilder == null) { bagUpdatesBuilder = commitBuilder.addBagUpdatesBuilder(); } - for (T value : localAdditions) { - ByteStringOutputStream stream = new ByteStringOutputStream(); - // Encode the value - elemCoder.encode(value, stream, Coder.Context.OUTER); - ByteString encoded = stream.toByteString(); - if (cachedValues != null) { - // We'll capture this value in the cache below. - // Capture the value's size now since we have it. - encodedSize += encoded.size(); - } - bagUpdatesBuilder.addValues(encoded); - } + final Windmill.TagBag.Builder finalBagUpdatesBuilder = bagUpdatesBuilder; + @Nullable + Object unused = Review Comment: can you just remove unused? -- 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]
