Skip to content

fix(scala): rebuild specialized Scala collections as their own class - #4222

Open
pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:fix/scala-specialized-collections
Open

pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:fix/scala-specialized-collections

Conversation

@pjfanning

Copy link
Copy Markdown
Member

Why?

In native mode, the Scala collection and map serializers write the value's generic factory (iterableFactory, sortedIterableFactory or mapFactory). For some kinds that factory builds a different class:

Written Read back as before this PR
immutable.IntMap, immutable.LongMap immutable.Map
immutable.BitSet, mutable.BitSet TreeSet
mutable.LongMap mutable.HashMap
mutable.StringBuilder mutable.ArrayBuffer[Char]
ArraySeq.ofInt (and the other primitive variants, immutable and mutable) ArraySeq.ofRef (boxed)

Root values still compared equal, but declared case class fields of these types failed with ClassCastException.

What does this PR do?

  • ScalaSerializerFactory asks the new SpecificCollectionSerializers.createSerializer first. It picks the serializer once per class, so the write path has no extra branch.
  • ScalaSpecificMapSerializer / ScalaSpecificCollectionSerializer use the existing size + factory + elements layout, but write the kind's own stdlib factory instead of the generic one:
    • IntMap.toFactory, immutable.LongMap.toFactory and mutable.LongMap.toFactory
    • the immutable.BitSet / mutable.BitSet companions (SpecificIterableFactory)
    • ArraySeq.evidenceIterableFactory(ClassTag.X) for the primitive ArraySeq variants. ArraySeq.ofRef keeps the generic path, which already rebuilds an ofRef.
  • ScalaStringBuilderSerializer writes the wrapped java.lang.StringBuilder with fory-core's StringBuilderSerializer encoding, instead of writing each Char as a collection element. Like RangeSerializer, it extends CollectionLikeSerializer with the codegen hook disabled, because struct fields send every Scala Iterable down the collection serializer path.
  • Xlang mode is unchanged.

Compatibility

The binary format is unchanged for the map, BitSet and ArraySeq kinds. Only which factory object is written changes. mutable.StringBuilder is now encoded as a string rather than a Seq[Char], so data previously written for that type will not deserialize as a StringBuilder. That data already came back as an ArrayBuffer[Char] before this PR.

Registration coverage for the runtime subclasses of these types (for example IntMap.Tip, ArraySeq.ofInt) is still tracked in #4101. The new tests run with requireClassRegistration(false).

Related issues

Closes #4102

Verification

  • New tests in CollectionKindsTest cover root values and declared case class fields, with compatible both true and false. They fail without the change (6 failures) and pass with it.
  • sbt "++2.13.18 fory-scala/test": 173 succeeded, 0 failed.
  • sbt "++3.3.8 fory-scala/test": 202 succeeded, 0 failed.

AI Usage Disclosure

  • substantial_ai_assistance: yes
  • scope: implementation and tests, written with Claude Code and reviewed by the maintainer
  • ai_review: no separate AI review passes were run

🤖 Generated with Claude Code

The native Scala collection and map serializers write the value's
generic factory (iterableFactory, sortedIterableFactory or mapFactory).
For some kinds that factory builds a different class, so IntMap and
LongMap came back as Map, BitSet as TreeSet, mutable.LongMap as HashMap,
StringBuilder as ArrayBuffer[Char], and primitive ArraySeq as a boxed
ArraySeq.ofRef. Declared case class fields of these types failed with
ClassCastException.

ScalaSerializerFactory now picks, once per class, a serializer that
writes the kind's own stdlib factory: the IntMap/LongMap ToFactory
objects, the BitSet companions, and a ClassTag factory for primitive
ArraySeq variants. Scala StringBuilder gets a dedicated serializer that
reuses the java.lang.StringBuilder string encoding.

Closes apache#4102

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

[Scala] Native mode rebuilds IntMap, LongMap, BitSet, mutable.LongMap and StringBuilder as a different collection class

1 participant