Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala 2.13.0 and Hash derivation rework #157

Merged
merged 1 commit into from
Jun 13, 2019
Merged

Conversation

joroKr21
Copy link
Member

@joroKr21 joroKr21 commented Jun 4, 2019

Case classes' hashCode is not backwards compatible in Scala 2.13,
because it mixes the productPrefix for better distribution.
This means we need version specific code to pass the Hash laws.

  • Update dependencies for Scala 2.13.0
  • Rework Hash derivation for consistency
  • Split product and coproduct derivation
  • Add a Scala version specific seed for scala.Product subtypes
  • Optimize HashBuilder to a one-pass hash
  • Replace usages of Statics (not recommended) with MurmurHash3
  • Test all variants of Hash derivation

@joroKr21 joroKr21 added this to the 2.0.0-RC1 milestone Jun 4, 2019
@joroKr21 joroKr21 requested a review from kailuowang June 4, 2019 04:20
@joroKr21 joroKr21 self-assigned this Jun 4, 2019
implicit def mkHashGenericProduct[A, R <: HList](
implicit A: Generic.Aux[A, R], R: Lazy[HashBuilder[R]], ev: A <:< Product = null
): MkHash[A] = instance(
x => R.value.hash(A.to(x), if (ev == null) MurmurHash3.productSeed else util.VersionSpecific.productSeed(x)),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this to be honest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a pattern match here? like

x match {
   case p : Product => util.VersionSpecific.productSeed(p)
   case _ => MurmurHash3.productSeed
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the part I'm not sure about. On one hand, ev: A <:< Product = null looks like a hack, but on the other pattern matching on the type parameter would break parametricity.

But what if the solution is to drop this law altogether from cats-testkit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to think about the law. Could we split this one method into two derivation methods with different priorities? One for Product another for the rest.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's also an option 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. We can later remove the version specific code if we decide not to enforce this law.

@joroKr21
Copy link
Member Author

joroKr21 commented Jun 4, 2019

This uncovered an issue with Hash[Long]: typelevel/cats#2878

@joroKr21 joroKr21 force-pushed the hash branch 4 times, most recently from 65c637b to 2c9bbc4 Compare June 13, 2019 13:01
Case classes' `hashCode` is not backwards compatible in Scala 2.13,
because it mixes the `productPrefix` for better distribution.
This means we need version specific code to pass the `Hash` laws.

  * Update dependencies for Scala 2.13.0
  * Rework `Hash` derivation for consistency
  * Split product and coproduct derivation
  * Add a Scala version specific seed for `scala.Product` subtypes
  * Optimize `HashBuilder` to a one-pass hash
  * Replace usages of `Statics` (not recommended) with `MurmurHash3`
  * Test all variants of `Hash` derivation
@joroKr21 joroKr21 changed the title Scala 2.13.0-RC3 and Hash derivation rework Scala 2.13.0 and Hash derivation rework Jun 13, 2019
@joroKr21
Copy link
Member Author

Updated to Scala 2.13.0 🎉

Copy link
Contributor

@kailuowang kailuowang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hurrah

@kailuowang kailuowang merged commit 185c343 into typelevel:master Jun 13, 2019
@joroKr21 joroKr21 deleted the hash branch June 13, 2019 18:14
@kailuowang kailuowang modified the milestones: 2.0.0-RC1, 2.0.0-M1 Jul 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants