This is an automated email from the ASF dual-hosted git repository.
fanningpj 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 f207a1761f cache bytestring hashcode (#2836)
f207a1761f is described below
commit f207a1761ff44821d357683121bea90bcd9c1038
Author: PJ Fanning <[email protected]>
AuthorDate: Wed Apr 8 13:18:36 2026 +0200
cache bytestring hashcode (#2836)
---
.../scala/org/apache/pekko/util/ByteStringSpec.scala | 19 ++++++++++++++++++-
.../main/scala/org/apache/pekko/util/ByteString.scala | 3 +++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git
a/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala
b/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala
index 1fb4102878..701b19df38 100644
--- a/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala
+++ b/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala
@@ -1106,7 +1106,24 @@ class ByteStringSpec extends AnyWordSpec with Matchers
with Checkers {
val slice3 = "cdefghijklmn".getBytes(StandardCharsets.UTF_8)
byteStringWithOffset.startsWith(slice3) should ===(true)
}
-
+ "return same hashCode" in {
+ val slice0 = ByteString1.fromString("xyz")
+ val slice1 = ByteString1.fromString("xyzabc")
+ val slice2 = ByteString1.fromString("12345")
+ val byteStringLong = ByteString1.fromString("abcdefghijklmnopqrstuvwxyz")
+ val byteStringLong2 =
ByteString1.fromString("abcdefghijklmnopqrstuvwxyz")
+ val byteStrings = ByteStrings(byteStringLong, byteStringLong)
+ val byteStrings2 = ByteStrings(byteStringLong, byteStringLong)
+ slice0.hashCode should ===(slice0.hashCode)
+ slice1.hashCode should ===(slice1.hashCode)
+ slice2.hashCode should ===(slice2.hashCode)
+ byteStringLong.hashCode should ===(byteStringLong.hashCode)
+ byteStringLong.hashCode should ===(byteStringLong2.hashCode)
+ byteStringLong2.equals(byteStringLong) should ===(true)
+ byteStrings.hashCode should ===(byteStrings.hashCode)
+ byteStrings.hashCode should ===(byteStrings2.hashCode)
+ byteStrings2.equals(byteStrings) should ===(true)
+ }
}
"A ByteString" must {
diff --git a/actor/src/main/scala/org/apache/pekko/util/ByteString.scala
b/actor/src/main/scala/org/apache/pekko/util/ByteString.scala
index a74f3dc246..2742436b68 100644
--- a/actor/src/main/scala/org/apache/pekko/util/ByteString.scala
+++ b/actor/src/main/scala/org/apache/pekko/util/ByteString.scala
@@ -995,6 +995,9 @@ sealed abstract class ByteString
// of ByteString which changed for Scala 2.12, see
https://github.com/akka/akka/issues/21774
override final def className: String = "ByteString"
+ // Cache the hash code since ByteString is immutable
+ override lazy val hashCode: Int = super.hashCode()
+
override def isEmpty: Boolean = length == 0
// override protected[this] def newBuilder: ByteStringBuilder =
ByteString.newBuilder
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]