Skip to content

Commit

Permalink
make ordered serialization stable across compilations (#1664)
Browse files Browse the repository at this point in the history
* ordered serialization - fix `knownDirectSubclasses` indeterminism

* remove special handling for hierarchies with <= 4 elements
  • Loading branch information
fwbrasil authored and Timur Abishev committed May 16, 2017
1 parent b1bf1be commit 0f95484
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object SealedTraitOrderedBuf {
import c.universe._
def freshT(id: String) = newTermName(c.fresh(s"$id"))

val knownDirectSubclasses = outerType.typeSymbol.asClass.knownDirectSubclasses
val knownDirectSubclasses = StableKnownDirectSubclasses(c)(outerType)

if (knownDirectSubclasses.isEmpty)
c.abort(c.enclosingPosition, s"Unable to access any knownDirectSubclasses for $outerType , a bug in scala 2.10/2.11 makes this unreliable.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.twitter.scalding.serialization.macros.impl.ordered_serialization.providers

import scala.reflect.macros.whitebox.Context

/**
* The `knownDirectSubclasses` method doesn't provide stable ordering
* since it returns an unordered `Set` and the `Type` AST nodes don't
* override the `hashCode` method, relying on the default identity
* `hashCode`.
*
* This function makes the ordering stable using a list ordered by the
* full name of the types.
*/
object StableKnownDirectSubclasses {

def apply(c: Context)(tpe: c.Type): List[c.universe.TypeSymbol] =
tpe.typeSymbol.asClass.knownDirectSubclasses.map(_.asType).toList.sortBy(_.fullName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twitter.scalding.thrift.macros.impl.ordered_serialization

import com.twitter.scalding.serialization.macros.impl.ordered_serialization.providers.StableKnownDirectSubclasses
import com.twitter.scalding.serialization.macros.impl.ordered_serialization._
import com.twitter.scrooge.ThriftUnion

Expand All @@ -39,8 +40,8 @@ object ScroogeUnionOrderedBuf {

val dispatcher = buildDispatcher

val subClasses: List[Type] = outerType.typeSymbol.asClass.knownDirectSubclasses.map(_.asType.toType).toList

val subClasses: List[Type] = StableKnownDirectSubclasses(c)(outerType).map(_.toType)
val subData: List[(Int, Type, Option[TreeOrderedBuf[c.type]])] = subClasses.map { t =>
if (t.typeSymbol.name.toString == "UnknownUnionField") {
(t, None)
Expand Down

0 comments on commit 0f95484

Please sign in to comment.