Skip to content

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

Open
pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:bytestring-set-to-list
Open

fix: use a Seq rather than a Set to hold TCP magic ByteStrings#3484
pjfanning wants to merge 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
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

1 participant