Skip to content

This is an Android plugin, which can improve the performance of layout loading by parsing the layout xml file during the compilation period and converting the reflection instantiation in LayoutInflater into a normal instantiation. 这是一个Android插件,通过在编译期时解析布局xml文件,将LayoutInflater中的反射实例化转成普通的实例化,以此来提高布局加载的性能

License

Notifications You must be signed in to change notification settings

dreamgyf/FastInflate

Repository files navigation

Android Kotlin Gradle Maven version stars LICENSE

English | 中文

This is an Android plugin, which can improve the performance of layout loading by parsing the layout xml file during the compilation period and converting the reflection instantiation in LayoutInflater into a normal instantiation.

Get Started

Dependencies

  1. Open the setting.gradle file, and make sure that the repositories, gradlePluginPortal and mavenCentral, are configured.
pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenLocal()
        // other repository
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        mavenCentral()
        mavenLocal()
        // other repository
    }
}
  1. Open the build.gradle file of project, and add the FastInflate plugin in the plugins.
plugins {
    id 'com.dreamgyf.android.fastinflate' version '0.1.0-alpha-03' apply false
}
  1. Open the build.gradle file of module, and add the FastInflate plugin in the plugins.
plugins {
    id 'com.dreamgyf.android.fastinflate'
}
  1. Open the build.gradle file of module, and add the FastInflate library in the dependencies.
dependencies {
    implementation 'com.dreamgyf.android.fastinflate:0.1.0-alpha-03'
}

Usage

Just use FastInflate instead LayoutInflater.

// LayoutInflater.from(this).inflate(R.layout.activity_main, null)
FastInflate.from(this).inflate(R.layout.activity_main, null)

Notice

  • If you want compile this project, please clone the parent project AndroidLibraries directly.

  • Multi-module projects are not supported currently.

  • Multi-directory of layout is not supported currently. (For Example: layout-v23)

  • The <include /> tag doesn't supported to use theme attribute.

  • The current version is an alpha beta, and have not done compatibility tests for all of the Android versions.

  • The initialization of the current version of FastInflate is slow, this problem will be solved in subsequent versions.

Performance test

ps:The following test results are based on activity_main layout of this branch.

Pixel 6 pro, Android SDK 33

  • Time consumption of first inflate layout(Unit: ms)
FastInflate LayoutInflater
23ms 28ms
23ms 29ms
22ms 29ms
24ms 30ms
21ms 29ms
23ms 29ms
23ms 29ms
23ms 29ms
22ms 28ms
23ms 30ms

FastInflate takes 22.7ms on average

LayoutInflater takes 29ms on average

Performance improved by about 27.75%

  • Time consumption of second inflate layout(Unit: µs)
FastInflate LayoutInflater
1895µs 2810µs
2009µs 3223µs
1898µs 2659µs
1904µs 2990µs
1891µs 2767µs
1861µs 2802µs
2051µs 3004µs
1886µs 3001µs
1957µs 2930µs
1888µs 2940µs

FastInflate takes 1924µs on average

LayoutInflater takes 2912.6µs on average

Performance improved by about 51.38%

  • Time consumption of inflate 1000 times for the same layout(单位:ms 毫秒)
FastInflate LayoutInflater
835ms 1318ms
848ms 1330ms
844ms 1343ms
845ms 1338ms
848ms 1344ms
853ms 1343ms
856ms 1354ms
855ms 1363ms
846ms 1369ms
843ms 1344ms

FastInflate takes 847.3ms on average

LayoutInflater takes 1344.6ms on average

Performance improved by about 58.69%

Repositories

LICENSE

This project uses the Apache-2.0 license.

About

This is an Android plugin, which can improve the performance of layout loading by parsing the layout xml file during the compilation period and converting the reflection instantiation in LayoutInflater into a normal instantiation. 这是一个Android插件,通过在编译期时解析布局xml文件,将LayoutInflater中的反射实例化转成普通的实例化,以此来提高布局加载的性能

Topics

Resources

License

Stars

Watchers

Forks

Languages