目录

Performance Optimization Practices: Mixed-Language Project Synchronization

Overview

For a large HarmonyOS project with multiple C++ modules, the traditional synchronization (Sync) phase often suffers from long build times due to serial execution of module tasks and repeated ohpm install executions, which compromises the development efficiency. To enhance the development experience, DevEco Studio offers several optimization mechanisms that significantly reduce the build time, such as adjusting the dependency installation logic, synchronization strategy, and CMake configuration update method. The key optimization methods are as follows.

Method 1: Improving the Dependency Installation Efficiency via ohpm

  1. ohpm provides two configuration options that, when enabled, can reduce the time required for ohpm install. Configure the following fields in the .ohpmrc file. For details about the fields, see ohpmrc.
    • Set enable_unified_lockfile to true to enable unified lockfile handling, which significantly reduces the execution time of ohpm install when there are duplicate dependencies between modules and optimizes the build process.
    • Set enable_boost_extraction_speed to true to enable high-performance decompression and traversal algorithms, which significantly reduces the execution time of ohpm install when the project has dependencies on large files.
  2. After you call methods such as setDependenciesOpt and setOverrides to dynamically modify the dependency information in the oh-package.json5 file, DevEco Studio will execute ohpm install twice during operations such as Sync or Build.

    Since DevEco Studio 6.0.0 Beta3, Enable ohpm execution by hvigor is introduced. When this feature is enabled, Hvigor will execute ohpm install only once, improving the build efficiency.

    How to enable: Choose File > Settings (DevEco Studio > Preferences/Settings on macOS) > Build, Execution, Deployment > Build Tools > Hvigor and select Enable ohpm execution by hvigor.

Method 2: Enabling Parallel C++ Build for the Synchronization Phase

In projects with C++ modules, executing Sync and Refresh Project triggers a separate compileNative task for each C++ module, with modules built serially. If a project contains many C++ modules, the build process will take a long time.

Since version 26.0.0, parallel C++ compilation for the synchronization phase is available, which accelerates the build process by executing the syncNative task within the synchronization phase.

How to enable: Choose File > Settings (DevEco Studio > Preferences/Settings on macOS) > Build, Execution, Deployment > Build Tools > Hvigor and select Enable C++ syncNative compilation.

When this feature is enabled, the behavior depends on whether dependencies exist between modules, as described in the following scenarios:

Possible impact: If there are custom plugins or tasks in the compileNative task phase of the hvigorfile.ts script, enabling this feature will cause the custom plugins or tasks not to be executed since the compileNative task will not be triggered.

Method 3: Dynamically Updating the C++ Project Configuration

In projects with C++ modules, you may often use FILE(GLOB) in CMakeLists.txt to dynamically collect source files, as shown below:

# Files in the same directory as the current CMakeLists.txt file
FILE(GLOB SRC_LIST "test/*.cpp")

# Merge SRC_LIST into a new variable PROJECT_SOURCE.
list(APPEND PROJECT_SOURCE
     ${SRC_LIST}
     )

# Create a shared library.
add_library(MyProject SHARED ${PROJECT_SOURCE})

When you add or modify .cpp source files in the test directory, build errors often occur if you directly run Sync or Build. The root cause is that CMake uses a static caching mechanism by default. When FILE(GLOB) is used, CMake scans the files only once during the configuration phase. If source files are added, removed, or modified, CMake may not recognize these changes in time, causing the build system to still reference the outdated cache state. In such cases, clearing the .cxx cache can fix the issue, but frequent cache cleanup significantly compromises the development efficiency.

Since version 26.0.0, the Refresh C++ Project feature is introduced, allowing you to manually trigger CMake to reconfigure the project, ensuring that the dynamically generated source file list stays synchronized with the current file system state.

Method 4: Changing the Code Indexing Mode

For large C/C++ project development, code indexing is often a key factor affecting the startup speed and development smoothness. While traditional full indexing provides complete code navigation capabilities, it often entails prolonged indexing and persistently high CPU and memory consumption for large codebases, leaving you waiting before coding.

Since version 26.0.0, DevEco Studio allows you to switch the C/C++ code indexing mode. You can flexibly select the most suitable indexing mode based on the project size, hardware performance, and specific development requirements, striking the optimal balance between development experience and resource consumption. For details about the supported code indexing modes and how to switch between them, see Code Indexing (clangd).