Skip to content

Initialization and usage efficiency of generated compose ImageVector's #672

@nxoim

Description

@nxoim

The intellij plugin currently provides 2 types of output modes: backing property with an equality check and delegated lazy property. Both are common ways to initialize data lazily on access, but also both introduce a small memory and performance impact due to additional allocations and equality checks that happen on access.

There are ways to initialize and use image vectors more efficiently:

Singleton value wrapper

val getterPropety get() = Wrapper.value

private object Wrapper {
    val value = 
}

Singleton value wrappers still have a small impact on memory but will only get initialized at first access in a thread safe manner. Accessing the property in the wrapper does not involve additional equality checks.

Singleton implementation

val getterProperty: ImageVector get()= Implementation

private object Implementation : ImageVector {

}

Singleton implementations are initialized at first access and have an even smaller impact on memory because they hold ImageVector data directly, but are not an option because ImageVector is final.

Standard property field initialization

val getterProperty get() = icon

private val icon: ImageVector = 

Simple properties should have the least impact on memory and performance, however, to reference the following JetBrains blog, they will initialize on first access to any code in the file. In order to guarantee the implied performance improvements each IconVector must be declared in a separate file. This nuance is not an issue per se, especially in the context of tools like Valkyrie, but it's worth making the user aware that in order to benefit from the lazy initialization they must not declare multiple ImageVector properties in one file.

So, the intellij plugin could be improved by implementing any of the following changes:

  • Providing singleton value wrapper and/or standard property field output modes
  • Adjusting the current implementation of backing property output mode and adding a note that prompts the user to keep the generated output as-is

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions