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

Fix pathologically slow compilation of some pattern matches since 2.13.7 #10258

Merged
merged 1 commit into from Mar 2, 2023

Conversation

KisaragiEffective
Copy link
Contributor

@KisaragiEffective KisaragiEffective commented Jan 7, 2023

by switching the pattern matcher to use mutable.LinkedHashSet instead of ListSet

fixes scala/bug#12499

@scala-jenkins scala-jenkins added this to the 2.13.11 milestone Jan 7, 2023
@lrytz
Copy link
Member

lrytz commented Jan 9, 2023

This seems to non-deterministically fail partest test/files/neg/t7020.scala. I assume it's about the undefined Set iteration order.

@KisaragiEffective
Copy link
Contributor Author

I feel the same, passed/failed on local randomly. Which should I do to fix it?

  1. modify patmat's logic to fix t7020 -- This would make large diff and takes longer time.
  2. update t7020 errors and leave patmat's logic as is -- I don't think it will resolve the issue.
  3. use java.util.LinkedHashSet and leave t7020 as is -- LinkedHashSet is not immutable since it's from Java. Would it cause an issue?
  4. backport part of Add SeqSet and SetFromMap implementations scala-library-next#35 to use, and leave t7020 as is

@dwijnand
Copy link
Member

@KisaragiEffective I don't mind if we switch to java.util.LinkedHashSet.

@KisaragiEffective
Copy link
Contributor Author

I'm not sure why it was failed...

// would be nice to statically check whether a prop is equational or pure,
// but that requires typing relations like And(x: Tx, y: Ty) : (if(Tx == PureProp && Ty == PureProp) PureProp else Prop)
final case class And(ops: Set[Prop]) extends Prop
object And {
def apply(ps: Prop*) = create(ps)
def create(ps: Iterable[Prop]) = ps match {
case ps: Set[Prop] => new And(ps)
case _ => new And(ps.to(scala.collection.immutable.ListSet))
case _ => new And(new java.util.LinkedHashSet[Prop](ps.toSet.asJava).asScala.to(Set))
Copy link
Member

Choose a reason for hiding this comment

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

ps.toSet creates an immutable HashSet which has non-deterministic iteration order.

I think we can use Scala's mutable.LinkedHashSet instead of Java's. Maybe the best option is to change the And and Or case classes to have a mutable.LinkedHashSet[Prop] parameter? That would express the fact that an ordered collection is required.

Otherwise, use collection.Set[Prop]?

@dwijnand what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Isn't java.util.LinkedHashSet known to be faster than mutable.LinkedHashSet? Good catch on the toSet: we need to build up the set in insertion-order of the iterable.

I don't mind if we change the parameter either.

Copy link
Member

Choose a reason for hiding this comment

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

The Java version is possibly still faster, but our LinkedHashSet was recently brought up to date (#10221). Compared to ListSet which we are replacing here, it's likely going to be fine.

@SethTisue
Copy link
Member

@KisaragiEffective interested in returning to this...?

@KisaragiEffective
Copy link
Contributor Author

It's still failed on t7020. Are there other implicit assumptions?

@lrytz
Copy link
Member

lrytz commented Feb 20, 2023

The current version still calls toSet, which creates an immutable HashSet. See my comment here: #10258 (comment)

@KisaragiEffective KisaragiEffective marked this pull request as ready for review February 21, 2023 06:44
@KisaragiEffective
Copy link
Contributor Author

they're green except combined step 🎉

* experiment fix for SI-12499 (patmat perf)
* use java.util.LinkedHashSet
* switch j.u to s.c.m LinkedHashSet
* avoid convert to Set to keep deterministic iteration order
* fix compile error

Co-authored-by: Dale Wijnand <dale.wijnand@gmail.com>
@KisaragiEffective KisaragiEffective changed the title experiment fix for SI-12499 (patmat perf) Patmat: Switch from ListSet to mutable.LinkedHashSet (SI-12499) Mar 2, 2023
@KisaragiEffective
Copy link
Contributor Author

(changed title according to squashed commit)

@dwijnand dwijnand merged commit 5ecdcf8 into scala:2.13.x Mar 2, 2023
@SethTisue SethTisue changed the title Patmat: Switch from ListSet to mutable.LinkedHashSet (SI-12499) Fix pathologically slow compilation of some pattern matches since 2.13.7 Mar 2, 2023
@michelou
Copy link

michelou commented Mar 6, 2023

@SethTisue IMO the issue is only partially fixed; see my comment in flix/flix PR 4916.

@SethTisue
Copy link
Member

@michelou ah, sorry to hear that. I wonder if what you're experiencing now has some entirely different cause, or if it's more that the PR we have needs a followup adjustment.

@KisaragiEffective
Copy link
Contributor Author

Seems this is not enough for solve problem, see flix/flix#4916 (comment)

@SethTisue
Copy link
Member

I see — new ticket, if you're able to minimize it?

@KisaragiEffective
Copy link
Contributor Author

I've already filed (however not sure if it's root cause of the perf regression) scala/bug#12760

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes worth highlighting in next release notes
Projects
None yet
6 participants