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

dropUnusedMap implementation #120

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

dropUnusedMap implementation #120

wants to merge 1 commit into from

Conversation

KacperFKorban
Copy link
Member

@KacperFKorban KacperFKorban commented Jul 9, 2024

Calling dropUnusedMap with a for comprehension as an argument potentially removed an unused map call at the end of the desugared for, which allows for better stack-safety in FP Scala code.

For example, let's consider the following code:

//> using scala 3.3.3
//> using lib "dev.zio::zio:2.1.5"
//> using lib "org.virtuslab::avocado:0.2.0-a-very-local-SNAPSHOT-not-available-on-maven-yet-since-its-local"

import zio.*
import avocado.*

def loop: Task[Unit] =
//  dropUnusedMap:
    for
      _ <- Console.print("loop")
      _ <- loop
    yield ()

@main
def run =
  val runtime = Runtime.default
  Unsafe.unsafe { implicit unsafe =>
    runtime.unsafe.run(loop).getOrThrowFiberFailure()
  }

It's memory usage looks like this:

image

However, if we uncomment the dropUnusedMap line, the memory usage looks like this:

image

Similar example for cats-effect:

//> using scala 3.3.3
//> using lib "org.typelevel::cats-effect:3.5.4"
//> using lib "org.virtuslab::avocado:0.2.0+3-8e54d976+20240709-1515-SNAPSHOT"

import cats.effect.*
import cats.effect.unsafe.implicits.global
import avocado.*

def loop: IO[Unit] =
//  dropUnusedMap:
    for
      _ <- IO(print("loop"))
      _ <- loop
    yield ()

@main
def run =
  loop.unsafeRunSync()

The new implementation doesn't require an `AvocADO` instance to work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant