FlyJingFish / AndroidAOP

🔥🔥🔥AndroidAOP 是专属于 Android 端 Aop 框架,只需一个注解就可以请求权限、切换线程、禁止多点、一次监测所有点击事件、监测生命周期等等,没有使用 AspectJ,也可以定制出属于你的 Aop 代码
Apache License 2.0
259 stars 15 forks source link

perf: enhance ksp compiler performance via using isolating mode #4

Closed JailedBird closed 5 months ago

JailedBird commented 5 months ago

简介

本次Pr是为Ksp注解处理器启用增量编译,以提升编译性能;

APT注解处理器包含隔离模式和聚合模式,这在你的APT处理中已经体现:

com.flyjingfish.android_aop_processor.AndroidAopProcessor,aggregating

Ksp中同样存在这个概念,不过更为精细,需要注解处理器开发者主动描述[input1 .... inputN => output]的关系;

参考文档如下:

当前代码使用Dependencies.ALL_FILES,当修改任意文件都会导致注解处理器重新全量处理,性能非常低下;根据AOP的特性,应该可以判断出这一个isolating模式的处理器,也就是一对一;

  /*private fun writeToFile(typeBuilder: TypeSpec.Builder,fileName:String){
    val typeSpec = typeBuilder.build()
    val kotlinFile = FileSpec.builder(packageName, fileName).addType(typeSpec)
      .build()
    codeGenerator
      .createNewFile(
        Dependencies.ALL_FILES,
        packageName,
        fileName
      )
      .writer()
      .use { kotlinFile.writeTo(it) }
  }*/

改进:仅当前文件变化,才会导致输出文件变化;

val dependencies = Dependencies(false, symbol.containingFile!!)
writeToFile(typeBuilder,fileName, dependencies)

private fun writeToFile(
    typeBuilder: TypeSpec.Builder,
    fileName: String,
    dependencies: Dependencies
  ) {
    val typeSpec = typeBuilder.build()
    val kotlinFile = FileSpec.builder(packageName, fileName).addType(typeSpec)
      .build()
    codeGenerator
      .createNewFile(
        dependencies,
        packageName,
        fileName
      )
      .writer()
      .use { kotlinFile.writeTo(it) }
  }

如何观测

首先在gradle配置中启用ksp增量编译和日志记录(见pr),然后观测ksp处理日志:

image

前者描述编译时增量编译藏文件,有 Dirty / All: xxx% 比例作为参考

后者描述增量编译输入与输出的关系;

以android_aop_core模块的CheckNetwork为例,修改CheckNetwork任意一行代码(确保文件hash变化即可)

修改前:

首次编译、二次编译 Dirty / All均为100%

改进后:

首次编译Dirty / All为100%、二次编译 Dirty / All: 5.71%

改进后的首次和二次编译的日志文件 kspDirtySet.log

=== Build 1702719915631 ===
All Files
  src\main\java\com\flyjingfish\android_aop_core\AndroidAopContentProvider.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\CheckNetwork.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\CustomIntercept.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Delay.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\DoubleClick.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\IOThread.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\MainThread.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\OnLifecycle.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Permission.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Scheduled.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\SingleClick.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\TryCatch.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\CheckNetworkCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\ClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\CustomInterceptCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\DelayCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\DoubleClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\IOThreadCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\MainThreadCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\OnLifecycleCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\PermissionCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\ScheduledCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\SingleClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\TryCatchCut.kt
  src\main\java\com\flyjingfish\android_aop_core\enums\ThreadType.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnCheckNetworkListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnCustomInterceptListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnPermissionsInterceptListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnRequestPermissionListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnThrowableListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnToastListener.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\AndroidAop.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\AppExecutors.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\NetworkUtils.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\Utils.kt
Modified
Removed
Disappeared Outputs
Affected By CP
Affected By new syms
Affected By sealed
CP changes
Dirty:
  src\main\java\com\flyjingfish\android_aop_core\AndroidAopContentProvider.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\CheckNetwork.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\CustomIntercept.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Delay.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\DoubleClick.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\IOThread.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\MainThread.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\OnLifecycle.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Permission.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Scheduled.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\SingleClick.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\TryCatch.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\CheckNetworkCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\ClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\CustomInterceptCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\DelayCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\DoubleClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\IOThreadCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\MainThreadCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\OnLifecycleCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\PermissionCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\ScheduledCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\SingleClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\TryCatchCut.kt
  src\main\java\com\flyjingfish\android_aop_core\enums\ThreadType.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnCheckNetworkListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnCustomInterceptListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnPermissionsInterceptListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnRequestPermissionListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnThrowableListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnToastListener.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\AndroidAop.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\AppExecutors.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\NetworkUtils.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\Utils.kt

Dirty / All: 100.00%

=== Build 1702720284859 ===
All Files
  src\main\java\com\flyjingfish\android_aop_core\AndroidAopContentProvider.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\CheckNetwork.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\CustomIntercept.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Delay.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\DoubleClick.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\IOThread.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\MainThread.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\OnLifecycle.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Permission.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\Scheduled.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\SingleClick.kt
  src\main\java\com\flyjingfish\android_aop_core\annotations\TryCatch.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\CheckNetworkCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\ClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\CustomInterceptCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\DelayCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\DoubleClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\IOThreadCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\MainThreadCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\OnLifecycleCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\PermissionCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\ScheduledCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\SingleClickCut.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\TryCatchCut.kt
  src\main\java\com\flyjingfish\android_aop_core\enums\ThreadType.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnCheckNetworkListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnCustomInterceptListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnPermissionsInterceptListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnRequestPermissionListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnThrowableListener.kt
  src\main\java\com\flyjingfish\android_aop_core\listeners\OnToastListener.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\AndroidAop.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\AppExecutors.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\NetworkUtils.kt
  src\main\java\com\flyjingfish\android_aop_core\utils\Utils.kt
Modified
  src\main\java\com\flyjingfish\android_aop_core\annotations\CheckNetwork.kt
Removed
Disappeared Outputs
Affected By CP
Affected By new syms
  src\main\java\com\flyjingfish\android_aop_core\annotations\CheckNetwork.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\CheckNetworkCut.kt
Affected By sealed
CP changes
Dirty:
  src\main\java\com\flyjingfish\android_aop_core\annotations\CheckNetwork.kt
  src\main\java\com\flyjingfish\android_aop_core\cut\CheckNetworkCut.kt

Dirty / All: 5.71%

如果本次改进无误,望采纳;