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

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


The following commit(s) were added to refs/heads/master by this push:
     new d959514a979 HDDS-16277. Reuse CharsetEncoder/Decoder in StringCodec 
(#11112)
d959514a979 is described below

commit d959514a979967b78a9039b60b1406fc9db92e9d
Author: KUAN-HAO HUANG <[email protected]>
AuthorDate: Thu Aug 27 01:08:52 2026 +0800

    HDDS-16277. Reuse CharsetEncoder/Decoder in StringCodec (#11112)
---
 .../java/org/apache/hadoop/hdds/utils/db/StringCodecBase.java    | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git 
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/db/StringCodecBase.java
 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/db/StringCodecBase.java
index f64f1931831..2ff7fe25085 100644
--- 
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/db/StringCodecBase.java
+++ 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/db/StringCodecBase.java
@@ -42,6 +42,9 @@ abstract class StringCodecBase implements Codec<String> {
   private final Charset charset;
   private final boolean fixedLength;
   private final int maxBytesPerChar;
+  // CharsetEncoder/CharsetDecoder are stateful and not thread-safe: reuse one 
per thread.
+  private final ThreadLocal<CharsetEncoder> threadLocalEncoder = 
ThreadLocal.withInitial(this::newEncoder);
+  private final ThreadLocal<CharsetDecoder> threadLocalDecoder = 
ThreadLocal.withInitial(this::newDecoder);
 
   StringCodecBase(Charset charset) {
     this.charset = charset;
@@ -97,7 +100,7 @@ private int getSerializedSizeUpperBound(String s) {
   private <E extends Exception> PutToByteBuffer<E> encode(
       String string, Integer serializedSize, Function<String, E> newE) {
     return buffer -> {
-      final CoderResult result = newEncoder().encode(
+      final CoderResult result = threadLocalEncoder.get().reset().encode(
           CharBuffer.wrap(string), buffer, true);
       if (result.isError()) {
         throw newE.apply("Failed to encode with " + charset + ": " + result
@@ -114,7 +117,7 @@ private <E extends Exception> PutToByteBuffer<E> encode(
 
   String decodeNoFallback(ByteBuffer buffer) throws CodecException {
     try {
-      return newDecoder().decode(buffer.asReadOnlyBuffer()).toString();
+      return 
threadLocalDecoder.get().decode(buffer.asReadOnlyBuffer()).toString();
     } catch (Exception e) {
       throw new CodecException("Failed to decode " + buffer, e);
     }
@@ -123,7 +126,7 @@ String decodeNoFallback(ByteBuffer buffer) throws 
CodecException {
   String decodeWithFallback(ByteBuffer buffer) {
     Runnable error = null;
     try {
-      return newDecoder().decode(buffer.asReadOnlyBuffer()).toString();
+      return 
threadLocalDecoder.get().decode(buffer.asReadOnlyBuffer()).toString();
     } catch (Exception e) {
       error = () -> LOG.warn("Failed to decode buffer with {}, buffer = (hex) 
{}",
           charset, StringUtils.bytes2Hex(buffer, 20), e);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to