Seems like suspend lambdas keep around closure val...
# compiler
y
Seems like suspend lambdas keep around closure values for too long even when its later suspension steps don't use those closure values. Is there some magic compiler option to prevent that from happening? Is this a missing optimization? My example boils down to:
Copy code
suspend fun foo(a: Any): Any =
  bar { k ->
    baz(k, a)
  }
suspend fun bar(body: (Any) -> Any): Any
suspend fun baz(k: Any, a: Any): Any
both
k
and
a
stick around in the generated suspend lambda for the argument to
bar
, even though clearly after the call to baz,
k
and
a
would never get used again. Also, TCO isn't kicking in for some reason, and so that generated suspend lambda has 2 states, and it calls
baz
with
this
as the continuation instead of using
completion
which is what I'd expect with TCO (according to some issue I saw TCO should be fully implemented for JVM, but here it doesn't work weirdly)
d
cc @Ilmir Usmanov [JB]