aloyszhang commented on code in PR #9894:
URL: https://github.com/apache/inlong/pull/9894#discussion_r1544004213


##########
inlong-common/src/main/java/org/apache/inlong/common/msg/InLongMsg.java:
##########
@@ -246,13 +246,12 @@ public boolean addMsg(String attr, ByteBuffer data) {
             return false;
         }
 
-        DataBuffer outputBuffer = attr2MsgBuffer.get(attr);
-        if (outputBuffer == null) {
-            outputBuffer = new DataBuffer();
-            attr2MsgBuffer.put(attr, outputBuffer);
+        DataBuffer outputBuffer = attr2MsgBuffer.computeIfAbsent(attr, key -> {
+            DataBuffer newBuffer = new DataBuffer();
             // attrlen + utflen + meglen + compress
             this.datalen += attr.length() + 2 + 4 + 1;
-        }
+            return newBuffer;
+        });

Review Comment:
   ```java
   DataBuffer outputBuffer = attr2MsgBuffer.computeIfAbsent(attr, key -> {
               DataBuffer newBuffer = new DataBuffer();
               // attrlen + utflen + meglen + compress
               this.datalen += attr.length() + 2 + 4 + 1;
               return newBuffer;
           });
   ```
   computeIfAbsent should only create a new DataBuffer if the mapping for attr 
does not exist.
   These two lines should move out of the mappingFunction.
   ```java
               // attrlen + utflen + meglen + compress
               this.datalen += attr.length() + 2 + 4 + 1;
   ```
   Suggestion:
   ```java
           attr2MsgBuffer.computeIfAbsent(attr, k-> new DataBuffer());
            // attrlen + utflen + meglen + compress
           this.datalen += attr.length() + 2 + 4 + 1;
   ```
   



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