目录

运行效率提高

在开发过程中,优化影响性能的代码片段,以提高运行效率。以下实践总结了一些高性能的写法和建议:

变量声明

属性访问

数值计算与数据结构

减少同文件大量export *导出方式和延迟加载

通过路径展开提升运行效率

传统HAR包导入即使只需要HAR包中单个变量,也会触发整个依赖链所有模块的加载和执行,造成性能浪费。ArkTS编译器提供expandImportPath(配置文件结构)配置项。通过修改配置项,编译器会将间接包名导入转换为精准的文件路径导入,跳过中间模块。该优化可消除冗余的模块解析和初始化,减少冷启动阶段不必要的资源消耗。

由于import路径展开会跳过中间模块的执行,若业务依赖模块的执行顺序,修改后可能会导致业务异常。具体场景请参阅通过import路径展开优化性能。

[h2]场景案例

未使用路径展开

在以下示例中,即使只需要使用typeNum变量,但当使用import *全量导入HAR包时,所有关联模块(如Animals.ets等)仍会被加载执行,这会带来不必要的性能开销。

// entry\src\main\ets\pages\Index.ets
// Before optimization: Namespace Import
import { hilog } from '@kit.PerformanceAnalysisKit';
import * as library from 'library';

hilog.info(0x0000, 'testTag', 'library.typeNum is %{public}d', library.typeNum);

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize($r('app.float.page_text_font_size'))
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          this.message = 'Welcome';
        })
    }
    .height('100%')
    .width('100%')
  }
}
// library\src\main\ets\Animals.ets
import { hilog } from '@kit.PerformanceAnalysisKit';

export * from './Birds';

export * from './Fish';

export * from './Mammals';

export * from './TypeNum';

hilog.info(0x0000, 'testTag', 'library Animals.ets execute.');
// library\src\main\ets\Birds.ets
import { hilog } from '@kit.PerformanceAnalysisKit';

export const birds: string = 'Butterflies';

hilog.info(0x0000, 'testTag', 'library Birds.ets execute.');
// library\src\main\ets\Fish.ets
import { hilog } from '@kit.PerformanceAnalysisKit';

export const fish: string = 'carp';

hilog.info(0x0000, 'testTag', 'library Fish.ets execute.');
// library\src\main\ets\Mammals.ets
import { hilog } from '@kit.PerformanceAnalysisKit';

export const mammals: string = 'cat';

hilog.info(0x0000, 'testTag', 'library Mammals.ets execute.');
// library\src\main\ets\TypeNum.ets
import { hilog } from '@kit.PerformanceAnalysisKit';

export const typeNum: number = 4;

hilog.info(0x0000, 'testTag', 'library TypeNum.ets execute.');
// library\Index.ets
export { MainPage } from './src/main/ets/components/MainPage';

export * from "./src/main/ets/Animals";

oh-package.json如下增加模块依赖library包:

// entry\oh-package.json5
"dependencies": {
  "library": "file:../library"
}

使用路径展开

开发者可通过配置expandImportPath(配置文件结构)来启用路径展开功能,并且将导入方式改为import { typeNum } from 'library'。使源码中的import语句在编译时指向被引用符号的实际定义文件。从下图Trace上看此时只执行TypeNum.ets,同时通过修改前后的Trace对比也可以看出冷启动性能得到提升。

// entry\src\main\ets\pages\Index.ets
// After optimization: Named Space Import
import { hilog } from '@kit.PerformanceAnalysisKit';
import { typeNum } from 'library';

hilog.info(0x0000, 'testTag', 'library.typeNum is %{public}d', typeNum);

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize($r('app.float.page_text_font_size'))
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          this.message = 'Welcome';
        })
    }
    .height('100%')
    .width('100%')
  }
}