From 784f4256a3fd4e51b33ccdeb8932c1b352fd8c93 Mon Sep 17 00:00:00 2001 From: Shreyas Patil Date: Sat, 19 Oct 2024 11:01:43 +0530 Subject: [PATCH 1/3] #128: Fix: only deserialize metrics values and skip object deserialization --- .../core/ComposeCompilerMetricsProvider.kt | 63 ++++++++++++++++++- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt b/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt index 18fab8a..cf3e21a 100644 --- a/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt +++ b/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt @@ -31,7 +31,9 @@ import dev.shreyaspatil.composeCompilerMetricsGenerator.core.model.composables.C import dev.shreyaspatil.composeCompilerMetricsGenerator.core.parser.ClassReportParser import dev.shreyaspatil.composeCompilerMetricsGenerator.core.parser.ComposableReportParser import kotlinx.serialization.decodeFromString -import kotlinx.serialization.json.Json +import kotlinx.serialization.json.* +import java.io.File +import java.nio.file.Files /** * Provides metrics and reports of a Compose compiler @@ -62,14 +64,24 @@ interface ComposeCompilerMetricsProvider { * Default implementation for [ComposeCompilerMetricsProvider] which parses content provided by * [ComposeCompilerRawReportProvider]. */ -@OptIn(ExperimentalStdlibApi::class) private class DefaultComposeCompilerMetricsProvider( private val contentProvider: ComposeMetricsContentProvider, ) : ComposeCompilerMetricsProvider { override fun getOverallStatistics(): Map { val statistics = mutableMapOf() contentProvider.briefStatisticsContents.forEach { statContent -> - val stats = Json.decodeFromString>(statContent) + val stats = Json + .decodeFromString(statContent) + .mapNotNull { entry -> + // In Compose 1.7.0, now JSON also includes the details of compiler features. + // To avoid deserialization issues, we have to skip adding these details in this report and just + // have to take primitive values (i.e. actually metrics) in the account. + val primitive = entry.value as? JsonPrimitive + primitive?.longOrNull?.let { longValue -> + entry.key to longValue + } + } + if (statistics.isEmpty()) { statistics.putAll(stats) } else { @@ -116,3 +128,48 @@ fun ComposeCompilerMetricsProvider(files: ComposeCompilerRawReportProvider): Com val contentProvider = ComposeMetricsContentProvider(files) return DefaultComposeCompilerMetricsProvider(contentProvider) } + +fun main() { + val json = """ + { + "skippableComposables" : 110, + "restartableComposables" : 146, + "readonlyComposables" : 0, + "totalComposables" : 154, + "restartGroups" : 146, + "totalGroups" : 165, + "staticArguments" : 217, + "certainArguments" : 163, + "knownStableArguments" : 1363, + "knownUnstableArguments" : 31, + "unknownStableArguments" : 2, + "totalArguments" : 1396, + "markedStableClasses" : 2, + "inferredStableClasses" : 8, + "inferredUnstableClasses" : 2, + "inferredUncertainClasses" : 0, + "effectivelyStableClasses" : 10, + "totalClasses" : 12, + "memoizedLambdas" : 103, + "singletonLambdas" : 14, + "singletonComposableLambdas" : 25, + "composableLambdas" : 69, + "totalLambdas" : 122, + "featureFlags" : { + "StrongSkipping" : true + } + } + """.trimIndent() + + val ss = DefaultComposeCompilerMetricsProvider(ComposeMetricsContentProvider( + ComposeCompilerRawReportProvider.FromIndividualFiles( + listOf( + Files.createTempFile("dfdfd", "dsdsd").toFile().apply { + writeText(json) + }), emptyList(), emptyList(), emptyList() + ) + ) + ) + + println(ss.getOverallStatistics()) +} \ No newline at end of file From 5ab36e609d733949f3b27fdaf0d6a25dae395e82 Mon Sep 17 00:00:00 2001 From: Shreyas Patil Date: Sat, 19 Oct 2024 11:03:12 +0530 Subject: [PATCH 2/3] #128: Reformat with spotless --- .../core/ComposeCompilerMetricsProvider.kt | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt b/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt index cf3e21a..74904ad 100644 --- a/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt +++ b/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt @@ -30,9 +30,10 @@ import dev.shreyaspatil.composeCompilerMetricsGenerator.core.model.classes.Class import dev.shreyaspatil.composeCompilerMetricsGenerator.core.model.composables.ComposablesReport import dev.shreyaspatil.composeCompilerMetricsGenerator.core.parser.ClassReportParser import dev.shreyaspatil.composeCompilerMetricsGenerator.core.parser.ComposableReportParser -import kotlinx.serialization.decodeFromString -import kotlinx.serialization.json.* -import java.io.File +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.longOrNull import java.nio.file.Files /** @@ -70,17 +71,18 @@ private class DefaultComposeCompilerMetricsProvider( override fun getOverallStatistics(): Map { val statistics = mutableMapOf() contentProvider.briefStatisticsContents.forEach { statContent -> - val stats = Json - .decodeFromString(statContent) - .mapNotNull { entry -> - // In Compose 1.7.0, now JSON also includes the details of compiler features. - // To avoid deserialization issues, we have to skip adding these details in this report and just - // have to take primitive values (i.e. actually metrics) in the account. - val primitive = entry.value as? JsonPrimitive - primitive?.longOrNull?.let { longValue -> - entry.key to longValue + val stats = + Json + .decodeFromString(statContent) + .mapNotNull { entry -> + // In Compose 1.7.0, now JSON also includes the details of compiler features. + // To avoid deserialization issues, we have to skip adding these details in this report and just + // have to take primitive values (i.e. actually metrics) in the account. + val primitive = entry.value as? JsonPrimitive + primitive?.longOrNull?.let { longValue -> + entry.key to longValue + } } - } if (statistics.isEmpty()) { statistics.putAll(stats) @@ -130,7 +132,8 @@ fun ComposeCompilerMetricsProvider(files: ComposeCompilerRawReportProvider): Com } fun main() { - val json = """ + val json = + """ { "skippableComposables" : 110, "restartableComposables" : 146, @@ -159,17 +162,23 @@ fun main() { "StrongSkipping" : true } } - """.trimIndent() - - val ss = DefaultComposeCompilerMetricsProvider(ComposeMetricsContentProvider( - ComposeCompilerRawReportProvider.FromIndividualFiles( - listOf( - Files.createTempFile("dfdfd", "dsdsd").toFile().apply { - writeText(json) - }), emptyList(), emptyList(), emptyList() + """.trimIndent() + + val ss = + DefaultComposeCompilerMetricsProvider( + ComposeMetricsContentProvider( + ComposeCompilerRawReportProvider.FromIndividualFiles( + listOf( + Files.createTempFile("dfdfd", "dsdsd").toFile().apply { + writeText(json) + }, + ), + emptyList(), + emptyList(), + emptyList(), + ), + ), ) - ) - ) println(ss.getOverallStatistics()) -} \ No newline at end of file +} From 77549c66fa24016f72acc4c6efc5dda50a8bc1f6 Mon Sep 17 00:00:00 2001 From: Shreyas Patil Date: Sat, 19 Oct 2024 11:07:36 +0530 Subject: [PATCH 3/3] Remove sample usage test --- .../core/ComposeCompilerMetricsProvider.kt | 53 ------------------- 1 file changed, 53 deletions(-) diff --git a/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt b/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt index 74904ad..abf5c09 100644 --- a/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt +++ b/core/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/core/ComposeCompilerMetricsProvider.kt @@ -34,7 +34,6 @@ import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.longOrNull -import java.nio.file.Files /** * Provides metrics and reports of a Compose compiler @@ -130,55 +129,3 @@ fun ComposeCompilerMetricsProvider(files: ComposeCompilerRawReportProvider): Com val contentProvider = ComposeMetricsContentProvider(files) return DefaultComposeCompilerMetricsProvider(contentProvider) } - -fun main() { - val json = - """ - { - "skippableComposables" : 110, - "restartableComposables" : 146, - "readonlyComposables" : 0, - "totalComposables" : 154, - "restartGroups" : 146, - "totalGroups" : 165, - "staticArguments" : 217, - "certainArguments" : 163, - "knownStableArguments" : 1363, - "knownUnstableArguments" : 31, - "unknownStableArguments" : 2, - "totalArguments" : 1396, - "markedStableClasses" : 2, - "inferredStableClasses" : 8, - "inferredUnstableClasses" : 2, - "inferredUncertainClasses" : 0, - "effectivelyStableClasses" : 10, - "totalClasses" : 12, - "memoizedLambdas" : 103, - "singletonLambdas" : 14, - "singletonComposableLambdas" : 25, - "composableLambdas" : 69, - "totalLambdas" : 122, - "featureFlags" : { - "StrongSkipping" : true - } - } - """.trimIndent() - - val ss = - DefaultComposeCompilerMetricsProvider( - ComposeMetricsContentProvider( - ComposeCompilerRawReportProvider.FromIndividualFiles( - listOf( - Files.createTempFile("dfdfd", "dsdsd").toFile().apply { - writeText(json) - }, - ), - emptyList(), - emptyList(), - emptyList(), - ), - ), - ) - - println(ss.getOverallStatistics()) -}