Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
In native mode, the Scala collection and map serializers write the value's generic factory (
iterableFactory,sortedIterableFactoryormapFactory). For some kinds that factory builds a different class:immutable.IntMap,immutable.LongMapimmutable.Mapimmutable.BitSet,mutable.BitSetTreeSetmutable.LongMapmutable.HashMapmutable.StringBuildermutable.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?
ScalaSerializerFactoryasks the newSpecificCollectionSerializers.createSerializerfirst. It picks the serializer once per class, so the write path has no extra branch.ScalaSpecificMapSerializer/ScalaSpecificCollectionSerializeruse the existing size + factory + elements layout, but write the kind's own stdlib factory instead of the generic one:IntMap.toFactory,immutable.LongMap.toFactoryandmutable.LongMap.toFactoryimmutable.BitSet/mutable.BitSetcompanions (SpecificIterableFactory)ArraySeq.evidenceIterableFactory(ClassTag.X)for the primitiveArraySeqvariants.ArraySeq.ofRefkeeps the generic path, which already rebuilds anofRef.ScalaStringBuilderSerializerwrites the wrappedjava.lang.StringBuilderwith fory-core'sStringBuilderSerializerencoding, instead of writing eachCharas a collection element. LikeRangeSerializer, it extendsCollectionLikeSerializerwith the codegen hook disabled, because struct fields send every ScalaIterabledown the collection serializer path.Compatibility
The binary format is unchanged for the map,
BitSetandArraySeqkinds. Only which factory object is written changes.mutable.StringBuilderis now encoded as a string rather than aSeq[Char], so data previously written for that type will not deserialize as aStringBuilder. That data already came back as anArrayBuffer[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 withrequireClassRegistration(false).Related issues
Closes #4102
Verification
CollectionKindsTestcover root values and declared case class fields, withcompatibleboth 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
🤖 Generated with Claude Code