Skip to content

fix: use a Seq rather than a Set to hold TCP magic ByteStrings - #3484

Merged
pjfanning merged 1 commit into
apache:mainfrom
pjfanning:bytestring-set-to-list
Aug 31, 2026
Merged

fix: use a Seq rather than a Set to hold TCP magic ByteStrings#3484
pjfanning merged 1 commit into
apache:mainfrom
pjfanning:bytestring-set-to-list

Conversation

@pjfanning

Copy link
Copy Markdown
Member

Motivation:
#3425 introduced a Set as the collection of accepted TCP magic values. Sets default to HashSet, which hashes its members, and hashing a ByteString walks every one of its bytes. Comparing this handful of 4-byte values is better done with a couple of equality checks over a small ordered collection.

Modification:
Change ArterySettings.Advanced.TcpMagicValues from Set[ByteString] to immutable.Seq[ByteString], built with distinct.toList rather than toSet so the de-duplication the Set provided incidentally is kept, and change the TcpFraming acceptedMagic parameter to match. These were the only two Set[ByteString] in the tree; both are private[pekko] and both arrived in #3425.

Add ArterySettingsSpec, which did not exist: the tcp-magic parsing added by #3425 had no direct test.

Result:
TcpFraming's magic check on each inbound connection is a small number of ByteString equality comparisons instead of a hashCode over the received bytes.

One semantic change worth calling out: acceptance order is now the configured order, where the Set left it unspecified. This is consistent with TcpMagic, the outbound magic, already being the first configured value. No other behaviour changes.

Tests:

  • sbt "remote/testOnly org.apache.pekko.remote.artery.ArterySettingsSpec" - 7 tests succeeded, 0 failed
  • sbt "remote/testOnly org.apache.pekko.remote.artery.tcp.TcpFramingSpec" - 11 tests succeeded, 0 failed, covering the default, custom and legacy AKKA magic paths through the changed contains call
  • sbt "remote/mimaReportBinaryIssues" - no issues, no filters needed
  • ArterySettingsSpec pins the configuration order, the de-duplication including after 4-byte truncation, and both require rejections, so the collection type cannot be changed back without a failure.

References:
Fixes #3483, Refs #3425

Still to follow: a backport to 1.7.x, since #3425 is in 1.7.0 and that branch does not have the faster ByteString.equals from main.

Motivation:
apache#3425 introduced a Set as the collection of accepted TCP magic values. Sets
default to HashSet, which hashes its members, and hashing a ByteString walks
every one of its bytes. Comparing this handful of 4-byte values is better done
with a couple of equality checks over a small ordered collection.

Modification:
Change ArterySettings.Advanced.TcpMagicValues from Set[ByteString] to
immutable.Seq[ByteString], built with distinct.toList rather than toSet so the
de-duplication the Set provided incidentally is kept, and change the
TcpFraming acceptedMagic parameter to match. These were the only two
Set[ByteString] in the tree; both are private[pekko] and both arrived in apache#3425.

Add ArterySettingsSpec, which did not exist: the tcp-magic parsing added by
apache#3425 had no direct test.

Result:
TcpFraming's magic check on each inbound connection is a small number of
ByteString equality comparisons instead of a hashCode over the received bytes.
Acceptance order is now the configured order, where the Set left it
unspecified; this is consistent with TcpMagic, the outbound magic, already
being the first configured value. No other behaviour changes.

Tests:
- sbt "remote/testOnly org.apache.pekko.remote.artery.ArterySettingsSpec" - 7
  tests succeeded, 0 failed
- sbt "remote/testOnly org.apache.pekko.remote.artery.tcp.TcpFramingSpec" - 11
  tests succeeded, 0 failed, covering the default, custom and legacy AKKA magic
  paths through the changed contains call
- sbt "remote/mimaReportBinaryIssues" - no issues, no filters needed
- ArterySettingsSpec pins the configuration order, the de-duplication including
  after 4-byte truncation, and both require rejections, so the collection type
  cannot be changed back without a failure.

References:
Fixes apache#3483, Refs apache#3425
@pjfanning
pjfanning merged commit 43695a7 into apache:main Aug 31, 2026
10 checks passed
@pjfanning
pjfanning deleted the bytestring-set-to-list branch August 31, 2026 08:54
@pjfanning pjfanning added this to the 2.0.0-M5 milestone Aug 31, 2026
pjfanning added a commit to pjfanning/incubator-pekko that referenced this pull request Aug 31, 2026
…e#3484)

Motivation:
default to HashSet, which hashes its members, and hashing a ByteString walks
every one of its bytes. Comparing this handful of 4-byte values is better done
with a couple of equality checks over a small ordered collection.

Modification:
Change ArterySettings.Advanced.TcpMagicValues from Set[ByteString] to
immutable.Seq[ByteString], built with distinct.toList rather than toSet so the
de-duplication the Set provided incidentally is kept, and change the
TcpFraming acceptedMagic parameter to match. These were the only two
Set[ByteString] in the tree; both are private[pekko] and both arrived in apache#3425.

Add ArterySettingsSpec, which did not exist: the tcp-magic parsing added by

Result:
TcpFraming's magic check on each inbound connection is a small number of
ByteString equality comparisons instead of a hashCode over the received bytes.
Acceptance order is now the configured order, where the Set left it
unspecified; this is consistent with TcpMagic, the outbound magic, already
being the first configured value. No other behaviour changes.

Tests:
- sbt "remote/testOnly org.apache.pekko.remote.artery.ArterySettingsSpec" - 7
  tests succeeded, 0 failed
- sbt "remote/testOnly org.apache.pekko.remote.artery.tcp.TcpFramingSpec" - 11
  tests succeeded, 0 failed, covering the default, custom and legacy AKKA magic
  paths through the changed contains call
- sbt "remote/mimaReportBinaryIssues" - no issues, no filters needed
- ArterySettingsSpec pins the configuration order, the de-duplication including
  after 4-byte truncation, and both require rejections, so the collection type
  cannot be changed back without a failure.

References:
Fixes apache#3483, Refs apache#3425

Update TcpFramingSpec.scala

Update ArterySettingsSpec.scala
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

don't use sets to store ByteStrings

2 participants