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

hashCode for NumericRange is computed by evaluating all values #19217

Closed
AvaPL opened this issue Dec 6, 2023 · 1 comment
Closed

hashCode for NumericRange is computed by evaluating all values #19217

AvaPL opened this issue Dec 6, 2023 · 1 comment
Labels

Comments

@AvaPL
Copy link

AvaPL commented Dec 6, 2023

Compiler version

3.3.0

Minimized code

val largeRangeHashCode = (Long.MinValue to Long.MaxValue).hashCode()

Output

java.lang.IllegalArgumentException: More than Int.MaxValue elements.
  at scala.collection.immutable.NumericRange$.check$1(NumericRange.scala:330)
  at scala.collection.immutable.NumericRange$.count(NumericRange.scala:361)
  at scala.collection.immutable.NumericRange.length$lzycompute(NumericRange.scala:75)
  at scala.collection.immutable.NumericRange.length(NumericRange.scala:75)
  at scala.util.hashing.MurmurHash3.indexedSeqHash(MurmurHash3.scala:239)
  at scala.util.hashing.MurmurHash3$.seqHash(MurmurHash3.scala:354)
  at scala.collection.immutable.NumericRange.hashCode$lzycompute(NumericRange.scala:246)
  at scala.collection.immutable.NumericRange.hashCode(NumericRange.scala:246)
  ... 32 elided

Expectation

The hash code of a numeric range should be calculated. It seems that NumericRange uses MurmurHash3.indexedSeqHash which evaluates all values in the range to compute the hash code.

Additionally, even when the range is small enough, the hash code computation is ineffective and may affect the code performance e.g. when a Set[NumericRange[Long]] is used.

@AvaPL AvaPL added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 6, 2023
@mbovel mbovel added area:library Standard library and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 11, 2023
@odersky
Copy link
Contributor

odersky commented Dec 12, 2023

This bug should be opened against scala/scala. We can't change the standard library on the dotty side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants