Skip to content

Commit

Permalink
Merge pull request scala#8672 from rorygraves/mike/2.12.x-HashMapLess…
Browse files Browse the repository at this point in the history
…Tupling

remove tupling for foreach in keySet/values of immutable HashMap and TreeMap
  • Loading branch information
SethTisue authored Jan 31, 2020
2 parents 62d67e1 + b4683a6 commit 31d4d2f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/library/mima-filters/2.12.0.forwards.excludes
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.immutable.L
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.immutable.HashMap#HashMap1.foreachEntry")
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.immutable.HashMap#HashMapCollision1.foreachEntry")
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.util.hashing.MurmurHash3.product2Hash")
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.util.hashing.MurmurHash3.emptyMapHash")
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.util.hashing.MurmurHash3.emptyMapHash")

ProblemFilters.exclude[MissingClassProblem]("scala.collection.immutable.HashMap$HashMapKeys")
ProblemFilters.exclude[MissingClassProblem]("scala.collection.immutable.HashMap$HashMapValues")
15 changes: 15 additions & 0 deletions src/library/scala/collection/immutable/HashMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ sealed class HashMap[A, +B] extends AbstractMap[A, B]

override def par = ParHashMap.fromTrie(this)

/* Override to avoid tuple allocation in foreach */
private[collection] class HashMapKeys extends ImmutableDefaultKeySet {
override def foreach[U](f: A => U) = foreachEntry((key, _) => f(key))
override lazy val hashCode = super.hashCode()
}
override def keySet: immutable.Set[A] = new HashMapKeys

/** The implementation class of the iterable returned by `values`.
*/
private[collection] class HashMapValues extends DefaultValuesIterable {
override def foreach[U](f: B => U) = foreachEntry((_, value) => f(value))
}
override def values: scala.collection.Iterable[B] = new HashMapValues


}

/** $factoryInfo
Expand Down
7 changes: 7 additions & 0 deletions src/library/scala/collection/immutable/TreeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,11 @@ final class TreeMap[A, +B] private (tree: RB.Tree[A, B])(implicit val ordering:
}
}

override def keySet: SortedSet[A] = new DefaultKeySortedSet {
override def foreach[U](f: A => U): Unit = RB.foreachEntry(tree, {(key: A, _: B) => f(key)})
}

override def values: scala.Iterable[B] = new DefaultValuesIterable {
override def foreach[U](f: B => U): Unit = RB.foreachEntry(tree, {(_: A, value: B) => f(value)})
}
}

0 comments on commit 31d4d2f

Please sign in to comment.