目录

Performance/Memory Optimization Practices: Multi-HAP/Multi-HSP Build

Overview

In a project with multiple HAP/HSP modules, when you execute a build (Build Hap(s)), a separate compilation task is initiated for each module. Each compilation task performs syntax checking, conversion, and other compilation steps on the module's dependencies. If different HAPs/HSPs depend on the same HAR module, repeated compilation checking increases the build time and memory usage.

This topic describes several common optimization solutions to reduce the build time and memory usage in certain scenarios by specifying the build startup mode, adjusting module types, and enabling common configurations.

Optimization Solution 1: Specifying the Build Start Mode

By selecting a proper build and run start mode, you can avoid compiling all HAPs/HSPs and only compile the affected modules and their dependencies.

Optimization Solution 2: Converting HSPs to HARs

HAPs and HSPs, as well as different HSPs, may depend on the same HARs, resulting in redundant build and increased package size. If the modules do not require on-demand loading, converting HSPs to HARs can reduce the build time and memory usage, and shrink the package size.

[h2]Conversion Principles

  1. Top-down conversion
    • Run the ohpm list -r command to analyze the dependency chain of the project.
    • Perform the conversion starting from the top-level HSP in the dependency chain.
    • Reason: If you convert the bottom-level HSP into the HAR first, upper-layer HSPs may introduce duplicate HAR dependencies, resulting in longer build time.

  2. Applicable scope
    • HSP modules that do not require the on-demand loading feature in the project.

[h2]Conversion Procedure

For details, see Converting HSP to HAR.

[h2]FAQs

  1. Resource file conflicts due to duplicate names

    Symptom

    In a single-HAP project structure, after multiple HSPs are converted to HARs, their resource files are merged into the entry module. If different HSP modules contain resources with the same name, the overwrite logic is applied based on the order of global dependency configuration, which may result in resource loss or errors.

    Solution

    Standardize resource names by adding unique identifiers to resource files in each module.

  2. Worker thread loading path changes

    Symptom

    If a Worker thread is used in an HSP module, after HSP-to-HAR conversion, the Worker loading path rules may change due to the module type transition.

    Solution

    Adjust the Worker loading mode by referring to File URL Rules in Stage Model.

Optimization Solution 3: Using Common Configurations for Performance/Memory Optimization

Additionally, there are some configurations that can further reduce the build time and memory usage, improving the overall development experience. Add the following fields to the hvigor-config.json5 file. For details about the fields, see Experimental Features.

"properties": {
  "hvigor.task.schedule.optimization": true,   // Enable task scheduling optimization, which prioritizes execution of CompileArkTs and its dependent tasks to start the CompileArkTs task earlier and reduce the build time.
  "ohos.arkCompile.noEmitJs": true,    // Optimize intermediate compilation product generation. After optimization, no intermediate JavaScript code is generated during ArkTS compilation, reducing the compilation time and memory usage.
  "ohos.arkCompile.singleFileEmit": true     // Optimize compilation product writing. After optimization, when an individual file is parsed during ArkTS compilation, it is written to the disk, reducing the peak memory usage.
}