[compose][hr] Add compose compiler flag to disable lambda memoization
diff --git a/plugins/compose/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposeIrGenerationExtension.kt b/plugins/compose/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposeIrGenerationExtension.kt index 8a08035..88e5543 100644 --- a/plugins/compose/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposeIrGenerationExtension.kt +++ b/plugins/compose/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposeIrGenerationExtension.kt
@@ -49,6 +49,7 @@ private val descriptorSerializerContext: ComposeDescriptorSerializerContext? = null, private val featureFlags: FeatureFlags, private val skipIfRuntimeNotFound: Boolean = false, + private val lambdaMemoization: Boolean = true, private val messageCollector: MessageCollector, ) : IrGenerationExtension { var metrics: ModuleMetrics = EmptyModuleMetrics @@ -155,12 +156,14 @@ ).lower(moduleFragment) // Memoize normal lambdas and wrap composable lambdas - ComposerLambdaMemoization( - pluginContext, - metrics, - stabilityInferencer, - featureFlags, - ).lower(moduleFragment) + if (lambdaMemoization) { + ComposerLambdaMemoization( + pluginContext, + metrics, + stabilityInferencer, + featureFlags, + ).lower(moduleFragment) + } // transform all composable functions to have an extra synthetic composer // parameter. this will also transform all types and calls to include the extra
diff --git a/plugins/compose/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposePlugin.kt b/plugins/compose/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposePlugin.kt index f4fc5d4..8fcf01d 100644 --- a/plugins/compose/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposePlugin.kt +++ b/plugins/compose/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/ComposePlugin.kt
@@ -80,6 +80,9 @@ ) val SKIP_IR_LOWERING_IF_RUNTIME_NOT_FOUND_KEY = CompilerConfigurationKey<Boolean>("Skip IR lowering transformation when finding Compose runtime fails") + + val LAMBDA_MEMOIZATION_ENABLED_KEY = + CompilerConfigurationKey<Boolean>("Enable lambda memoization") } @OptIn(ExperimentalCompilerApi::class) @@ -207,6 +210,13 @@ required = false, allowMultipleOccurrences = false ) + + val LAMBDA_MEMOIZATION = CliOption( + "lambdaMemoization", + "<true|false>", + "Memoize lambda instances for composition", + required = false, + ) } override val pluginId = PLUGIN_ID @@ -227,6 +237,7 @@ TRACE_MARKERS_OPTION, FEATURE_FLAG_OPTION, SKIP_IR_LOWERING_IF_RUNTIME_NOT_FOUND_OPTION, + LAMBDA_MEMOIZATION, ) override fun processOption( @@ -329,6 +340,12 @@ ComposeConfiguration.SKIP_IR_LOWERING_IF_RUNTIME_NOT_FOUND_KEY, value == "true" ) + + LAMBDA_MEMOIZATION -> configuration.put( + ComposeConfiguration.LAMBDA_MEMOIZATION_ENABLED_KEY, + value == "true" + ) + else -> throw CliOptionProcessingException("Unknown option: ${option.optionName}") } } @@ -667,6 +684,11 @@ ComposeConfiguration.SKIP_IR_LOWERING_IF_RUNTIME_NOT_FOUND_KEY, ) + val lambdaMemoization = configuration.get( + ComposeConfiguration.LAMBDA_MEMOIZATION_ENABLED_KEY, + true + ) + val featureFlags = FeatureFlags( configuration.get( ComposeConfiguration.FEATURE_FLAGS, emptyList() @@ -723,6 +745,7 @@ descriptorSerializerContext = descriptorSerializerContext, featureFlags = featureFlags, skipIfRuntimeNotFound = skipIrLoweringIfRuntimeNotFound, + lambdaMemoization = lambdaMemoization, messageCollector = configuration.messageCollector, ) }