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

He-Pin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko.git


The following commit(s) were added to refs/heads/main by this push:
     new 88320c6978 refactor: use HexFormat for hex encoding instead of manual 
formatting (#3145)
88320c6978 is described below

commit 88320c697867acf62dd887031f4bac3fa9be4641
Author: He-Pin(kerr) <[email protected]>
AuthorDate: Tue Jun 23 19:48:06 2026 +0800

    refactor: use HexFormat for hex encoding instead of manual formatting 
(#3145)
    
    * refactor: use HexFormat for hex encoding instead of manual formatting
    
    Motivation:
    Manual per-byte hex encoding with "%02x".format() is verbose and
    allocates a String per byte. JDK 17's HexFormat formats entire byte
    arrays in one call.
    
    Modification:
    - VectorClock.scala: replace digester.digest.map("%02x".format).mkString
      with HexFormat.of().formatHex(digester.digest())
    - TcpFraming.scala: replace .map("%02x".format).mkString(" ")
      with HexFormat.ofDelimiter(" ").formatHex(bytes.toArray)
    
    Result:
    More concise code, fewer allocations (one String vs N+1 Strings),
    standard JDK API for hex encoding.
    
    Tests:
    sbt "cluster/compile" "remote/compile" — passed
    
    References:
    Refs #3136
    
    * refactor: cache HexFormat instance as singleton in VectorClock.Node
    
    Motivation:
    HexFormat is immutable and thread-safe, so there is no need to create
    a new instance on every hash call.
    
    Modification:
    Extract HexFormat.of() to a private val in the Node companion object.
    
    Result:
    Avoids unnecessary object allocation on each hash computation.
    
    References:
    Refs #3145
---
 cluster/src/main/scala/org/apache/pekko/cluster/VectorClock.scala   | 6 +++---
 .../main/scala/org/apache/pekko/remote/artery/tcp/TcpFraming.scala  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/cluster/src/main/scala/org/apache/pekko/cluster/VectorClock.scala 
b/cluster/src/main/scala/org/apache/pekko/cluster/VectorClock.scala
index 06e2efcd3b..738cdfd225 100644
--- a/cluster/src/main/scala/org/apache/pekko/cluster/VectorClock.scala
+++ b/cluster/src/main/scala/org/apache/pekko/cluster/VectorClock.scala
@@ -37,12 +37,12 @@ private[cluster] object VectorClock {
 
     def fromHash(hash: String): Node = hash
 
+    private val hexFormat = java.util.HexFormat.of()
+
     private def hash(name: String): String = {
       val digester = MessageDigest.getInstance("MD5")
       digester.update(name.getBytes(StandardCharsets.UTF_8))
-      digester.digest.map { h =>
-        "%02x".format(0xFF & h)
-      }.mkString
+      hexFormat.formatHex(digester.digest())
     }
   }
 
diff --git 
a/remote/src/main/scala/org/apache/pekko/remote/artery/tcp/TcpFraming.scala 
b/remote/src/main/scala/org/apache/pekko/remote/artery/tcp/TcpFraming.scala
index fbf72fffb0..6aca0ddd95 100644
--- a/remote/src/main/scala/org/apache/pekko/remote/artery/tcp/TcpFraming.scala
+++ b/remote/src/main/scala/org/apache/pekko/remote/artery/tcp/TcpFraming.scala
@@ -85,7 +85,7 @@ import pekko.util.ByteString
         else
           throw new FramingException(
             "Stream didn't start with expected magic bytes, " +
-            s"got [${(magic ++ 
reader.remainingData).take(10).map("%02x".format(_)).mkString(" ")}] " +
+            s"got [${java.util.HexFormat.ofDelimiter(" ").formatHex((magic ++ 
reader.remainingData).take(10).toArray)}] " +
             "Connection is rejected. Probably invalid accidental access.")
       }
     }


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

Reply via email to