Skip to content

Bad performance involving cats Resource and apply #14366

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

Closed
oyvindberg opened this issue Jan 27, 2022 · 4 comments
Closed

Bad performance involving cats Resource and apply #14366

oyvindberg opened this issue Jan 27, 2022 · 4 comments
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@oyvindberg
Copy link

Compiler version

3.1.0, 3.1.1

Minimized code

I minimized it from production code, but didn't inline the needed parts from cats and cats effect.

import cats.effect.{IO, Resource}
import cats.implicits.catsSyntaxApply

val faster: Resource[IO, Int] = {
  val foo = Resource.liftK(IO(println(s"flaff")))
  foo *> Resource.pure(1)
}

val slower: Resource[IO, Int] =
  Resource.liftK(IO(println(s"flaff"))) *> Resource.pure(1)

Scastie doesn't show how long it takes to compile, but you can see the difference. Here is the link anyway https://scastie.scala-lang.org/nQC63vOCTG6GuodVwwWKOg

Output

The two expressions faster and slower are equivalent, but one has the definition broken up in two. note that no futher types are ascribed.
On my machine compiling slower takes about 10 seconds, while faster is nearly instant.

Expectation

@oyvindberg oyvindberg added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 27, 2022
@oyvindberg oyvindberg changed the title Asymptomatic performance involving cats Resource and apply Bad performance involving cats Resource and apply Jan 27, 2022
@oyvindberg
Copy link
Author

Asymptomatic wasn't the word I was looking for :D anyways, you get my point :)

@smarter
Copy link
Member

smarter commented Jan 27, 2022

Fixed literally yesterday: #14333 :) Try scalaVersion := "3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY".

@smarter smarter closed this as completed Jan 27, 2022
@dwijnand
Copy link
Member

Indeed. Before:

$ scala-cli clean i14366.fast.scala
$ time scala-cli compile -d org.typelevel::cats-effect:3.3.4 i14366.fast.scala
Compiling project (Scala 3.0.2, JVM)
Compiled project (Scala 3.0.2, JVM)
scala-cli compile -d org.typelevel::cats-effect:3.3.4 i14366.fast.scala  1.14s user 0.26s system 78% cpu 1.766 total

$ scala-cli clean i14366.slow.scala
$ time scala-cli compile -d org.typelevel::cats-effect:3.3.4 i14366.slow.scala
Compiling project (Scala 3.0.2, JVM)
Compiled project (Scala 3.0.2, JVM)
scala-cli compile -d org.typelevel::cats-effect:3.3.4 i14366.slow.scala  1.13s user 0.26s system 22% cpu 6.101 total

After:

$ scala-cli clean i14366.fast.scala
$ time scala-cli compile -S 3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY -d org.typelevel::cats-effect:3.3.4 i14366.fast.scala
Compiling project (Scala 3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY, JVM)
Compiled project (Scala 3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY, JVM)
scala-cli compile -S 3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY -d    1.09s user 0.26s system 78% cpu 1.717 total

$ scala-cli clean i14366.slow.scala
$ time scala-cli compile -S 3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY -d org.typelevel::cats-effect:3.3.4 i14366.slow.scala
Compiling project (Scala 3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY, JVM)
Compiled project (Scala 3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY, JVM)
scala-cli compile -S 3.1.3-RC1-bin-20220126-8ae2962-NIGHTLY -d    1.09s user 0.24s system 72% cpu 1.833 total

@oyvindberg
Copy link
Author

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

No branches or pull requests

3 participants