Skip to content

Commit

Permalink
Memoize non-capturing lambdas when K2 is enabled
Browse files Browse the repository at this point in the history
K2 uses invokedynamic for lambdas and D8 does not optimize capture-less generated instances on Android as K1 used to.

Fixes: 340582180 ( https://issuetracker.google.com/issues/340582180 )
Test: compiler tests
Relnote: "Fix memoization of captureless lambdas when K2 compiler is used"
Change-Id: I71c91882d0cdcbe88e0086bc4a14b3e9825383bc ( https://android-review.googlesource.com/q/I71c91882d0cdcbe88e0086bc4a14b3e9825383bc )

Moved from: androidx/androidx@0428c00
  • Loading branch information
ShikaSD authored and Space Cloud committed Jun 3, 2024
1 parent ef0dc95 commit 3c67cda
Show file tree
Hide file tree
Showing 22 changed files with 141 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package androidx.compose.compiler.plugins.kotlin

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test

/* ktlint-disable max-line-length */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ val myProperty: () -> Unit @Composable get() {

public final class TestKt {
public final static getMyProperty(Landroidx/compose/runtime/Composer;I)Lkotlin/jvm/functions/Function0;
private final static _get_myProperty_%lambda%0()Lkotlin/Unit;
private final static _get_myProperty_%lambda%1%lambda%0()Lkotlin/Unit;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public abstract interface MeasurePolicy {
public final class TestKt {
public final static Text(Landroidx/compose/runtime/Composer;I)V
public final static Layout(LMeasurePolicy;Landroidx/compose/runtime/Composer;I)V
private final static Text%lambda%0(I)V
private final static Text%lambda%1%lambda%0(I)V
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public final class TestKt {
public final static Foo(Ljava/lang/String;Lkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;I)V
public final static Example(Landroidx/compose/runtime/Composer;I)V
private final static Foo%lambda%0(Ljava/lang/String;Lkotlin/jvm/functions/Function0;ILandroidx/compose/runtime/Composer;I)Lkotlin/Unit;
private final static Example%lambda%1()Lkotlin/Unit;
private final static Example%lambda%2(ILandroidx/compose/runtime/Composer;I)Lkotlin/Unit;
private final static Example%lambda%2%lambda%1()Lkotlin/Unit;
private final static Example%lambda%3(ILandroidx/compose/runtime/Composer;I)Lkotlin/Unit;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ fun test() {
}
@Composable
fun B(%composer: Composer?, %changed: Int) {
sourceInformationMarkerStart(%composer, <>, "C(B)<Button...>:Test.kt")
sourceInformationMarkerStart(%composer, <>, "C(B)<{}>,<Button...>:Test.kt")
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
}
Button({
Button(<block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
{
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}, ComposableSingletons%TestKt.lambda-1, %composer, 0b00110110)
if (isTraceInProgress()) {
traceEventEnd()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ val myProperty: () -> Unit @Composable get() {
val myProperty: Function0<Unit>
@Composable @JvmName(name = "getMyProperty")
get() {
sourceInformationMarkerStart(%composer, <>, "C:Test.kt#2487m")
sourceInformationMarkerStart(%composer, <>, "C<{>:Test.kt#2487m")
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
}
val tmp0 = {
val tmp0 = <block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp1_group = %composer.cache(false) {
{
}
}
sourceInformationMarkerEnd(%composer)
tmp1_group
}
if (isTraceInProgress()) {
traceEventEnd()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// Source
// ------------------------------------------

import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.runtime.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardActions

object Ui {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// Source
// ------------------------------------------

import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.runtime.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardActions

object Ui {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,35 @@ fun Test(%composer: Composer?, %changed: Int) {
}
internal object ComposableSingletons%TestKt {
val lambda-1: Function2<Composer, Int, Unit> = composableLambdaInstance(<>, false) { %composer: Composer?, %changed: Int ->
sourceInformation(%composer, "C<effect>:Test.kt")
sourceInformation(%composer, "C<{>,<effect>:Test.kt")
if (%changed and 0b0011 != 0b0010 || !%composer.skipping) {
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
}
%composer.startReplaceGroup(<>)
sourceInformation(%composer, "*<effect>")
sourceInformation(%composer, "*<{>,<effect>")
repeat(number) { it: Int ->
effects[it] = effect({
0
effects[it] = effect(<block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
{
0
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}, %composer, 0b0110)
}
%composer.endReplaceGroup()
outside = effect({
"0"
outside = effect(<block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
{
"0"
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}, %composer, 0b0110)
if (isTraceInProgress()) {
traceEventEnd()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun BasicText(
@Composable
fun BasicText(style: TextStyle?, onTextLayout: Function1<TextLayoutResult, Unit>?, overflow: TextOverflow, %composer: Composer?, %changed: Int, %default: Int) {
%composer = %composer.startRestartGroup(<>)
sourceInformation(%composer, "C(BasicText)P(2!,1:c#ui.text.style.TextOverflow):Test.kt")
sourceInformation(%composer, "C(BasicText)P(2!,1:c#ui.text.style.TextOverflow)<{}>:Test.kt")
val %dirty = %changed
if (%default and 0b0001 != 0) {
%dirty = %dirty or 0b0110
Expand All @@ -51,7 +51,14 @@ fun BasicText(style: TextStyle?, onTextLayout: Function1<TextLayoutResult, Unit>
style = Companion.Default
}
if (%default and 0b0010 != 0) {
onTextLayout = { it: TextLayoutResult ->
onTextLayout = <block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
{ it: TextLayoutResult ->
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}
}
if (%default and 0b0100 != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ import androidx.compose.runtime.ReadOnlyComposable
@Composable
fun A(%composer: Composer?, %changed: Int) {
%composer = %composer.startRestartGroup(<>)
sourceInformation(%composer, "C(A)<D>,<C({})>,<C(stab...>,<C(16.d...>,<C(Dp(1...>,<C(16.d...>,<C(norm...>,<C(Int....>,<C(stab...>,<C(Modi...>,<C(Foo....>,<C(cons...>,<C(123)>,<C(123>,<C(x)>,<C(x>:Test.kt")
sourceInformation(%composer, "C(A)<D>,<{}>,<C({})>,<C(stab...>,<C(16.d...>,<C(Dp(1...>,<C(16.d...>,<C(norm...>,<C(Int....>,<C(stab...>,<C(Modi...>,<C(Foo....>,<C(cons...>,<C(123)>,<C(123>,<C(x)>,<C(x>:Test.kt")
if (%changed != 0 || !%composer.skipping) {
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
}
val x = 123
D(ComposableSingletons%TestKt.lambda-1, %composer, 0b0110)
C({
C(<block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
{
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}, %composer, 0b0110)
C(stableFun(123), %composer, 0b0110)
C(16.dp + 10.dp, %composer, 0b0110)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ interface Consumer {
@Composable
fun Test(int: Int, %composer: Composer?, %changed: Int) {
%composer = %composer.startRestartGroup(<>)
sourceInformation(%composer, "C(Test)<Exampl...>:Test.kt")
sourceInformation(%composer, "C(Test)<{>,<Exampl...>:Test.kt")
if (%changed and 0b0001 != 0 || !%composer.skipping) {
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
}
Example(Consumer { it: Int ->
println(it)
}, %composer, 0b0110)
Example(<block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
Consumer { it: Int ->
println(it)
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}, %composer, 0)
if (isTraceInProgress()) {
traceEventEnd()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ fun Problem() {
fun Problem() {
fun foo() { }
val lambda = composableLambdaInstance(<>, false) { %composer: Composer?, %changed: Int ->
sourceInformation(%composer, "C:Test.kt")
sourceInformation(%composer, "C<::foo>:Test.kt")
if (%changed and 0b1011 != 0b0010 || !%composer.skipping) {
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
}
::foo
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
::foo
}
sourceInformationMarkerEnd(%composer)
tmp0_group
if (isTraceInProgress()) {
traceEventEnd()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ fun TestLambda(content: Function0<Unit>, %composer: Composer?, %changed: Int) {
@Composable
fun Test(%composer: Composer?, %changed: Int) {
%composer = %composer.startRestartGroup(<>)
sourceInformation(%composer, "C(Test)<TestLa...>:Test.kt")
sourceInformation(%composer, "C(Test)<{>,<TestLa...>:Test.kt")
if (%changed != 0 || !%composer.skipping) {
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
}
TestLambda({
println("Doesn't capture")
TestLambda(<block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
{
println("Doesn't capture")
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}, %composer, 0b0110)
if (isTraceInProgress()) {
traceEventEnd()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun Err() {
@NonRestartableComposable
@Composable
fun Err(%composer: Composer?, %changed: Int) {
sourceInformationMarkerStart(%composer, <>, "C(Err):Test.kt")
sourceInformationMarkerStart(%composer, <>, "C(Err)<{>:Test.kt")
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
}
Expand All @@ -34,9 +34,14 @@ fun Err(%composer: Composer?, %changed: Int) {
x
}
}
{
handler()
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
{
handler()
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
if (isTraceInProgress()) {
traceEventEnd()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ fun Test(%composer: Composer?, %changed: Int) {
traceEventStart(<>, %changed, -1, <>)
}
val capture = 0
TestMemoizedFun(TestFunInterface { it: Int ->
use(it)
}, %composer, 0b0110)
TestMemoizedFun(%composer.cache(false) {
TestFunInterface { it: Int ->
use(it)
}
}, %composer, 0)
TestMemoizedFun(%composer.cache(%composer.changed(capture)) {
TestFunInterface { it: Int ->
use(capture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ fun Test(%composer: Composer?, %changed: Int) {
traceEventStart(<>, %changed, -1, <>)
}
val capture = 0
TestMemoizedFun(TestFunInterface { it: Int ->
use(it)
}, %composer, 0b0110)
TestMemoizedFun(%composer.cache(false) {
TestFunInterface { it: Int ->
use(it)
}
}, %composer, 0)
TestMemoizedFun(%composer.cache(false) {
TestFunInterface { it: Int ->
use(capture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

import androidx.compose.runtime.*
import androidx.compose.ui.layout.*
import androidx.compose.foundation.text.*
import androidx.compose.ui.text.*
import androidx.compose.ui.text.style.*
import androidx.compose.foundation.text.*

@Composable
fun Test1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

import androidx.compose.runtime.*
import androidx.compose.ui.layout.*
import androidx.compose.foundation.text.*
import androidx.compose.ui.text.*
import androidx.compose.ui.text.style.*
import androidx.compose.foundation.text.*

@Composable
fun Test1() {
Expand Down Expand Up @@ -57,7 +56,7 @@ fun T(value: String) { }
@ComposableTarget(applier = "androidx.compose.ui.UiComposable")
fun Test1(%composer: Composer?, %changed: Int) {
%composer = %composer.startRestartGroup(<>)
sourceInformation(%composer, "C(Test1)<Layout...>:Test.kt")
sourceInformation(%composer, "C(Test1)<{>,<Layout...>:Test.kt")
if (%changed != 0 || !%composer.skipping) {
if (isTraceInProgress()) {
traceEventStart(<>, %changed, -1, <>)
Expand All @@ -66,8 +65,15 @@ fun Test1(%composer: Composer?, %changed: Int) {
sourceInformationMarkerStart(%composer, <>, "C:Test.kt")
Unit
sourceInformationMarkerEnd(%composer)
}, null, MeasurePolicy { <unused var>: List<Measurable>, <unused var>: Constraints ->
error("")
}, null, <block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
MeasurePolicy { <unused var>: List<Measurable>, <unused var>: Constraints ->
error("")
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}, %composer, 0b000110000000, 0b0010)
if (isTraceInProgress()) {
traceEventEnd()
Expand All @@ -83,7 +89,7 @@ fun Test1(%composer: Composer?, %changed: Int) {
@ComposableInferredTarget(scheme = "[androidx.compose.ui.UiComposable[androidx.compose.ui.UiComposable]]")
fun Test2(content: Function2<Composer, Int, Unit>, %composer: Composer?, %changed: Int) {
%composer = %composer.startRestartGroup(<>)
sourceInformation(%composer, "C(Test2)<Layout...>:Test.kt")
sourceInformation(%composer, "C(Test2)<{>,<Layout...>:Test.kt")
val %dirty = %changed
if (%changed and 0b0110 == 0) {
%dirty = %dirty or if (%composer.changedInstance(content)) 0b0100 else 0b0010
Expand All @@ -92,8 +98,15 @@ fun Test2(content: Function2<Composer, Int, Unit>, %composer: Composer?, %change
if (isTraceInProgress()) {
traceEventStart(<>, %dirty, -1, <>)
}
Layout(content, null, MeasurePolicy { <unused var>: List<Measurable>, <unused var>: Constraints ->
error("")
Layout(content, null, <block>{
sourceInformationMarkerStart(%composer, <>, "CC(remember):Test.kt#9igjgp")
val tmp0_group = %composer.cache(false) {
MeasurePolicy { <unused var>: List<Measurable>, <unused var>: Constraints ->
error("")
}
}
sourceInformationMarkerEnd(%composer)
tmp0_group
}, %composer, 0b000110000000 or 0b1110 and %dirty, 0b0010)
if (isTraceInProgress()) {
traceEventEnd()
Expand Down
Loading

0 comments on commit 3c67cda

Please sign in to comment.