airbnb / Showkase

🔦 Showkase is an annotation-processor based Android library that helps you organize, discover, search and visualize Jetpack Compose UI elements
https://medium.com/airbnb-engineering/introducing-showkase-a-library-to-organize-discover-and-visualize-your-jetpack-compose-elements-d5c34ef01095
Apache License 2.0
2.11k stars 107 forks source link

Optimize code generation to be more performant #284

Closed vinaygaba closed 1 year ago

vinaygaba commented 1 year ago

We hit an issue with a Jacoco task where it wasn't able to initialize the XRootCodegen file anymore as our codebase had reached the limit of components it was able to support. This was specifically happening if you used Jacoco as it tries to do a few things internally that initializes each class that's going to be processed for code coverage.

Error while instrumenting com/airbnb/android/feat/aggregator/showkase/MyShowkaseRootCodegen.class with JaCoCo 0.8.8.202204050719/5dcf34a.
          > Method too large: com/airbnb/android/feat/aggregator/showkase/MyShowkaseRootCodegen.<init> ()V

In this PR, I've implemented a couple changes:

These two changes will ensure that the initialization of the aggregated class is significantly more light weight and should avoid an issue like this from happening again.

Before

@ShowkaseRootCodegen(
  numComposablesWithoutPreviewParameter = 2,
  numComposablesWithPreviewParameter = 0,
  numColors = 1,
  numTypography = 1,
)
public class TestShowkaseRootCodegen : ShowkaseProvider {
  public val componentList: List<ShowkaseBrowserComponent> =
      mutableListOf<ShowkaseBrowserComponent>(
        ShowkaseBrowserComponent(
            group = "group1",
            componentName = "name1",
            componentKDoc = "",
            componentKey =
                """com.airbnb.android.showkase_processor_testing_null_group1_name1_0_null""",
            isDefaultStyle = false,
            component = @Composable { TestComposable1() }),
      )

  public val colorList: List<ShowkaseBrowserColor> = listOf<ShowkaseBrowserColor>(
        ShowkaseBrowserColor(
            colorGroup = "color",
            colorName = "name",
            colorKDoc = "",
            color = red)
      )

  public val typographyList: List<ShowkaseBrowserTypography> = listOf<ShowkaseBrowserTypography>(
        ShowkaseBrowserTypography(
            typographyGroup = "typography",
            typographyName = "name",
            typographyKDoc = "",
            textStyle = title)
      )

  public override fun getShowkaseComponents(): List<ShowkaseBrowserComponent> = componentList

  public override fun getShowkaseColors(): List<ShowkaseBrowserColor> = colorList

  public override fun getShowkaseTypography(): List<ShowkaseBrowserTypography> = typographyList

}

After

@ShowkaseRootCodegen(
  numComposablesWithoutPreviewParameter = 2,
  numComposablesWithPreviewParameter = 0,
  numColors = 1,
  numTypography = 1,
)
public class TestShowkaseRootCodegen : ShowkaseProvider {
  public override fun getShowkaseComponents(): List<ShowkaseBrowserComponent> {

    return listOf<ShowkaseBrowserComponent>(
        comairbnbandroidshowkaseprocessortestinggroup1name1,
    )
  }

  public override fun getShowkaseColors(): List<ShowkaseBrowserColor> {

    return listOf<ShowkaseBrowserColor>(
        comairbnbandroidshowkaseprocessortestingcolorname,
    )
  }

  public override fun getShowkaseTypography(): List<ShowkaseBrowserTypography> {

    return listOf<ShowkaseBrowserTypography>(
        comairbnbandroidshowkaseprocessortestingtypographyname,
    )
  }
}
// This is an auto-generated file. Please do not edit/modify this file.
package com.airbnb.android.showkase_processor_testing

import com.airbnb.android.showkase.models.ShowkaseBrowserColor

public val comairbnbandroidshowkaseprocessortestingcolorname: ShowkaseBrowserColor = 
    ShowkaseBrowserColor(
        colorGroup = "color",
        colorName = "name",
        colorKDoc = "",
        color = red
    )
// This is an auto-generated file. Please do not edit/modify this file.
package com.airbnb.android.showkase_processor_testing

import androidx.compose.runtime.Composable
import com.airbnb.android.showkase.models.ShowkaseBrowserComponent

public val comairbnbandroidshowkaseprocessortestinggroup1name1: ShowkaseBrowserComponent =
    ShowkaseBrowserComponent(
        group = "group1",
        componentName = "name1",
        componentKDoc = "",
        componentKey = """com.airbnb.android.showkase_processor_testing_null_group1_name1_0_null""",
        isDefaultStyle = false,
        component = @Composable { TestComposable1() }
    )
// This is an auto-generated file. Please do not edit/modify this file.
package com.airbnb.android.showkase_processor_testing

import com.airbnb.android.showkase.models.ShowkaseBrowserTypography

public val comairbnbandroidshowkaseprocessortestingtypographyname: ShowkaseBrowserTypography = 
    ShowkaseBrowserTypography(
        typographyGroup = "typography",
        typographyName = "name",
        typographyKDoc = "",
        textStyle = title
    )