Skip to content

Commit

Permalink
[compose] Use durable function keys for generating ComposableSingleto…
Browse files Browse the repository at this point in the history
…n lambda names
  • Loading branch information
sellmair authored and Space Cloud committed Nov 19, 2024
1 parent 04b2006 commit 4993e59
Show file tree
Hide file tree
Showing 110 changed files with 2,839 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ class ComposeBytecodeCodegenTest(useFir: Boolean) : AbstractCodegenTest(useFir)
val classesRegex = Regex("final class (.*?) \\{[\\S\\s]*?^}", RegexOption.MULTILINE)
val matches = classesRegex.findAll(bytecode)
val lambdaClass = matches
.first { it.groups[1]?.value?.startsWith("test/ComposableSingletons%TestKt%lambda-1%1") == true }
.single { it.groups[1]?.value?.startsWith("test/ComposableSingletons%TestKt%lambda%") == true }
.value
val invokeRegex = Regex("public final invoke([\\s\\S]*?)LOCALVARIABLE")
val invokeMethod = invokeRegex.find(lambdaClass)?.value ?: error("Could not find invoke method in $lambdaClass")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,235 @@ class LambdaMemoizationTransformTests(useFir: Boolean) : AbstractIrTransformTest
}
"""
)

@Test
fun testLambdasNextToEachOther() = verifyGoldenComposeIrTransform(
extra = """
import androidx.compose.runtime.Composable
@Composable
fun Box(content: @Composable () -> Unit) {}
""",
source = """
import androidx.compose.runtime.*
@Composable
fun root() {
Box { print("1") }
Box { print("2") }
Box { print("2") }
}
""".trimIndent()
)

@Test
fun testMemoizingNestedLambdas() = verifyGoldenComposeIrTransform(
extra = """
import androidx.compose.runtime.Composable
@Composable
fun Box(content: @Composable () -> Unit) {}
""",
source = """
import androidx.compose.runtime.*
@Composable
fun root() {
Box {
print("root/1")
Box {
print("root/1/1")
}
Box {
print("root/1/2")
Box {
print("root/1/2/1")
}
Box {
print("root/1/2/2")
}
}
}
}
""".trimIndent()
)

@Test
fun testMemoizingDifferentlyScopedLambdas() = verifyGoldenComposeIrTransform(
extra = """
import androidx.compose.runtime.Composable
@Composable
fun Box(content: @Composable () -> Unit) {}
""",
source = """
import androidx.compose.runtime.*
val topLevelScope = @Composable { println("topLevelScope") }
@Composable
fun functionScope() {
Box {
print("functionScope")
}
}
class ClassScope {
@Composable
fun classScope() {
Box {
print("classScope")
}
}
class NestedClassScope {
@Composable
fun nestedClassScope() {
Box {
print("nestedClassScope")
}
}
}
}
""".trimIndent()
)

@Test
fun testComposableInlineFunction() = verifyGoldenComposeIrTransform(
source = """
import androidx.compose.runtime.*
@Composable fun NonInlined() {
val a = @Composable { }
}
@Composable inline fun Inlined() {
val b = @Composable {}
}
""".trimIndent()
)

@Test
fun testComposableLambdaPropertyUsedInTwoFunctions() = verifyGoldenComposeIrTransform(
source = """
import androidx.compose.runtime.*
val a = @Composable { }
val b = @Composable { }
@Composable fun Foo() {
a()
b()
}
@Composable inline fun Bar() {
a()
b()
}
""".trimIndent()
)

@Test
fun testOverloads() = verifyGoldenComposeIrTransform(
extra = """
import androidx.compose.runtime.Composable
@Composable
fun Box(content: @Composable () -> Unit) {}
""",
source = """
import androidx.compose.runtime.*
@Composable fun Foo() {
Box {}
}
@Composable fun Foo(x: Int) {
Box {}
}
@Composable fun Int.Foo() {
Box {}
}
@Composable fun Foo(x: String) {
Box {}
}
""".trimIndent()
)

@Test
fun testGetterAndSetter() = verifyGoldenComposeIrTransform(
extra = """
import androidx.compose.runtime.Composable
@Composable
fun Box(content: @Composable () -> Unit) {}
""",
source = """
import androidx.compose.runtime.*
var foo: @Composable () -> Unit = { Box { print("field") } }
get() = {
Box {
print("get")
}
}
set(value) {
field = {
Box {
print("set")
}
}
}
""".trimIndent()
)


@Test
fun testDifferentLambdaTypes() = verifyGoldenComposeIrTransform(
source = """
import androidx.compose.runtime.*
@Composable
fun Foo() {
val a: @Composable () -> Unit = { }
val b: @Composable (x: Int) -> Unit = {}
val c: @Composable (x: String) -> Unit = {}
val d: @Composable Int.() -> Unit = {}
}
""".trimIndent()
)

@Test
fun testCallingInlineFunction() = verifyGoldenComposeIrTransform(
source = """
import androidx.compose.runtime.*
@Composable
fun Box(child: @Composable () -> Unit) {
print("box")
child()
}
@Composable
inline fun Foo(crossinline child: @Composable () -> Unit) {
val a = @Composable {
print("a")
}
Box {
print("foo")
a()
child()
}
}
@Composable
fun Test() {
Foo {
print("test")
}
}
""".trimIndent()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fun C(items: List<String>, %composer: Composer?, %changed: Int) {
if (isTraceInProgress()) {
traceEventStart(<>, %dirty, -1, <>)
}
X(items, ComposableSingletons%TestKt.lambda-1, %composer, 0b00110000 or 0b1110 and %dirty)
X(items, ComposableSingletons%TestKt.lambda%1463567948, %composer, 0b00110000 or 0b1110 and %dirty)
if (isTraceInProgress()) {
traceEventEnd()
}
Expand All @@ -105,7 +105,7 @@ fun C(items: List<String>, %composer: Composer?, %changed: Int) {
}
}
internal object ComposableSingletons%TestKt {
val lambda-1: Function3<String, Composer, Int, Unit> = composableLambdaInstance(<>, false) { item: String, %composer: Composer?, %changed: Int ->
val lambda%1463567948: Function3<String, Composer, Int, Unit> = composableLambdaInstance(<>, false) { item: String, %composer: Composer?, %changed: Int ->
sourceInformation(%composer, "C<A(item...>,<A(Wrap...>:Test.kt")
val %dirty = %changed
if (%changed and 0b0110 == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fun C(items: List<String>, %composer: Composer?, %changed: Int) {
if (isTraceInProgress()) {
traceEventStart(<>, %dirty, -1, <>)
}
X(items, ComposableSingletons%TestKt.lambda-1, %composer, 0b00110000 or 0b1110 and %dirty)
X(items, ComposableSingletons%TestKt.lambda%1463567948, %composer, 0b00110000 or 0b1110 and %dirty)
if (isTraceInProgress()) {
traceEventEnd()
}
Expand All @@ -105,7 +105,7 @@ fun C(items: List<String>, %composer: Composer?, %changed: Int) {
}
}
internal object ComposableSingletons%TestKt {
val lambda-1: Function3<String, Composer, Int, Unit> = composableLambdaInstance(<>, false) { item: String, %composer: Composer?, %changed: Int ->
val lambda%1463567948: Function3<String, Composer, Int, Unit> = composableLambdaInstance(<>, false) { item: String, %composer: Composer?, %changed: Int ->
sourceInformation(%composer, "C<A(item...>,<A(Wrap...>:Test.kt")
val %dirty = %changed
if (%changed and 0b0110 == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ val foo = @Composable { x: Int -> print(x) }

public final class ComposableSingletons%TestKt {
public <init>()V
public final getLambda-1%test_module()Lkotlin/jvm/functions/Function3;
public final getLambda%202954256%test_module()Lkotlin/jvm/functions/Function3;
static <clinit>()V
public final static LComposableSingletons%TestKt; INSTANCE
private static Lkotlin/jvm/functions/Function3; lambda-1
final static INNERCLASS ComposableSingletons%TestKt%lambda-1%1 null null
private static Lkotlin/jvm/functions/Function3; lambda%202954256
final static INNERCLASS ComposableSingletons%TestKt%lambda%202954256%1 null null
}
final class ComposableSingletons%TestKt%lambda-1%1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function3 {
final class ComposableSingletons%TestKt%lambda%202954256%1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function3 {
<init>()V
public final invoke(ILandroidx/compose/runtime/Composer;I)V
public synthetic bridge invoke(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
static <clinit>()V
public final static LComposableSingletons%TestKt%lambda-1%1; INSTANCE
public final static LComposableSingletons%TestKt%lambda%202954256%1; INSTANCE
OUTERCLASS ComposableSingletons%TestKt null
final static INNERCLASS ComposableSingletons%TestKt%lambda-1%1 null null
final static INNERCLASS ComposableSingletons%TestKt%lambda%202954256%1 null null
}
public final class TestKt {
public final static getFoo()Lkotlin/jvm/functions/Function3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ val foo = @Composable { x: Int -> print(x) }

public final class ComposableSingletons%TestKt {
public <init>()V
public final getLambda-1%test_module()Lkotlin/jvm/functions/Function3;
public final getLambda%208465591%test_module()Lkotlin/jvm/functions/Function3;
static <clinit>()V
public final static LComposableSingletons%TestKt; INSTANCE
private static Lkotlin/jvm/functions/Function3; lambda-1
final static INNERCLASS ComposableSingletons%TestKt%lambda-1%1 null null
private static Lkotlin/jvm/functions/Function3; lambda%208465591
final static INNERCLASS ComposableSingletons%TestKt%lambda%208465591%1 null null
}
final class ComposableSingletons%TestKt%lambda-1%1 implements kotlin/jvm/functions/Function3 {
final class ComposableSingletons%TestKt%lambda%208465591%1 implements kotlin/jvm/functions/Function3 {
<init>()V
public final invoke(ILandroidx/compose/runtime/Composer;I)V
public synthetic bridge invoke(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
static <clinit>()V
public final static LComposableSingletons%TestKt%lambda-1%1; INSTANCE
public final static LComposableSingletons%TestKt%lambda%208465591%1; INSTANCE
OUTERCLASS ComposableSingletons%TestKt null
final static INNERCLASS ComposableSingletons%TestKt%lambda-1%1 null null
final static INNERCLASS ComposableSingletons%TestKt%lambda%208465591%1 null null
}
public final class TestKt {
public final static getFoo()Lkotlin/jvm/functions/Function3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@

public final class ComposableSingletons%TestKt {
public <init>()V
public final getLambda-1%test_module()Lkotlin/jvm/functions/Function3;
public final getLambda%940817586%test_module()Lkotlin/jvm/functions/Function3;
static <clinit>()V
public final static LComposableSingletons%TestKt; INSTANCE
private static Lkotlin/jvm/functions/Function3; lambda-1
final static INNERCLASS ComposableSingletons%TestKt%lambda-1%1 null null
private static Lkotlin/jvm/functions/Function3; lambda%940817586
final static INNERCLASS ComposableSingletons%TestKt%lambda%940817586%1 null null
}
final class ComposableSingletons%TestKt%lambda-1%1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function3 {
final class ComposableSingletons%TestKt%lambda%940817586%1 extends kotlin/jvm/internal/Lambda implements kotlin/jvm/functions/Function3 {
<init>()V
public final invoke(ILandroidx/compose/runtime/Composer;I)V
public synthetic bridge invoke(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
static <clinit>()V
public final static LComposableSingletons%TestKt%lambda-1%1; INSTANCE
public final static LComposableSingletons%TestKt%lambda%940817586%1; INSTANCE
OUTERCLASS ComposableSingletons%TestKt null
final static INNERCLASS ComposableSingletons%TestKt%lambda-1%1 null null
final static INNERCLASS ComposableSingletons%TestKt%lambda%940817586%1 null null
}
public final class TestKt {
public final static Bar(Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;I)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@

public final class ComposableSingletons%TestKt {
public <init>()V
public final getLambda-1%test_module()Lkotlin/jvm/functions/Function3;
public final getLambda%-1785015162%test_module()Lkotlin/jvm/functions/Function3;
static <clinit>()V
public final static LComposableSingletons%TestKt; INSTANCE
private static Lkotlin/jvm/functions/Function3; lambda-1
final static INNERCLASS ComposableSingletons%TestKt%lambda-1%1 null null
private static Lkotlin/jvm/functions/Function3; lambda%-1785015162
final static INNERCLASS ComposableSingletons%TestKt%lambda%-1785015162%1 null null
}
final class ComposableSingletons%TestKt%lambda-1%1 implements kotlin/jvm/functions/Function3 {
final class ComposableSingletons%TestKt%lambda%-1785015162%1 implements kotlin/jvm/functions/Function3 {
<init>()V
public final invoke(ILandroidx/compose/runtime/Composer;I)V
public synthetic bridge invoke(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
static <clinit>()V
public final static LComposableSingletons%TestKt%lambda-1%1; INSTANCE
public final static LComposableSingletons%TestKt%lambda%-1785015162%1; INSTANCE
OUTERCLASS ComposableSingletons%TestKt null
final static INNERCLASS ComposableSingletons%TestKt%lambda-1%1 null null
final static INNERCLASS ComposableSingletons%TestKt%lambda%-1785015162%1 null null
}
public final class TestKt {
public final static Bar(Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;I)V
Expand Down
Loading

0 comments on commit 4993e59

Please sign in to comment.