Skip to content

Commit e13d505

Browse files
Optimize config.ini updates by batching shell executions
- Reduce up to 5 separate shell executions to 1 for AVD configuration - Improves performance by eliminating redundant process spawns - Add comprehensive efficiency report documenting all identified improvements - All existing tests pass, no functional changes Co-Authored-By: Yang <[email protected]>
1 parent 2548f9f commit e13d505

File tree

2 files changed

+80
-16
lines changed

2 files changed

+80
-16
lines changed

EFFICIENCY_REPORT.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Android Emulator Runner - Efficiency Improvements Report
2+
3+
## Overview
4+
This report documents efficiency improvements identified in the Android Emulator Runner codebase. The analysis focused on finding opportunities to reduce redundant operations, optimize I/O, and improve overall performance.
5+
6+
## Identified Inefficiencies
7+
8+
### 1. Multiple Shell Executions for Config.ini Updates (HIGH IMPACT) ⚡
9+
**File:** `src/emulator-manager.ts` (Lines 40-58)
10+
**Issue:** The code executes up to 5 separate shell commands to append configuration entries to the AVD config.ini file.
11+
**Impact:** Each shell execution spawns a new process, which is expensive. When multiple config options are set, this results in 5 separate process spawns.
12+
**Solution:** Batch all configuration entries into a single shell command.
13+
**Performance Gain:** Reduces shell executions from 5 to 1 (up to 80% reduction in process spawns).
14+
15+
### 2. Inefficient Channel Mapping (MEDIUM IMPACT)
16+
**File:** `src/channel-id-mapper.ts` (Lines 1-13)
17+
**Issue:** Uses if-else chain instead of a lookup table/map for channel name to ID mapping.
18+
**Impact:** O(n) lookup time instead of O(1), though with only 4 channels the impact is minimal.
19+
**Solution:** Replace with a Map or object lookup.
20+
**Performance Gain:** Constant time lookup instead of linear search.
21+
22+
### 3. Repeated Number Conversions (LOW IMPACT)
23+
**File:** `src/input-validator.ts` (Lines 79, 92, 97)
24+
**Issue:** The `checkEmulatorBuild` and `checkDiskSize` functions call `Number()` multiple times on the same string.
25+
**Impact:** Unnecessary computation overhead.
26+
**Solution:** Store the converted number in a variable and reuse it.
27+
**Performance Gain:** Eliminates redundant type conversions.
28+
29+
### 4. Regex Creation on Every Function Call (LOW IMPACT)
30+
**File:** `src/script-parser.ts` (Line 7)
31+
**Issue:** Creates regex `/\r\n|\n|\r/` on every `parseScript` function call.
32+
**Impact:** Regex compilation overhead on each invocation.
33+
**Solution:** Define regex as a module-level constant.
34+
**Performance Gain:** Eliminates regex recompilation.
35+
36+
### 5. Redundant Boolean Validation Functions (LOW IMPACT)
37+
**File:** `src/input-validator.ts` (Lines 39-76)
38+
**Issue:** Multiple similar validation functions that all use the same `isValidBoolean` helper.
39+
**Impact:** Code duplication and maintenance overhead.
40+
**Solution:** Create a generic boolean validator function.
41+
**Performance Gain:** Reduced code size and improved maintainability.
42+
43+
## Implementation Priority
44+
45+
1. **HIGH PRIORITY:** Config.ini shell execution batching (implemented in this PR)
46+
2. **MEDIUM PRIORITY:** Channel mapping optimization
47+
3. **LOW PRIORITY:** Number conversion optimization
48+
4. **LOW PRIORITY:** Regex constant optimization
49+
5. **LOW PRIORITY:** Boolean validation consolidation
50+
51+
## Performance Impact Summary
52+
53+
The primary fix implemented in this PR (batching config.ini updates) provides the most significant performance improvement by reducing shell process spawns from up to 5 to 1. This is particularly beneficial when multiple AVD configuration options are specified, which is a common use case.
54+
55+
The other identified inefficiencies have lower impact but could be addressed in future optimization efforts for marginal performance gains and improved code maintainability.
56+
57+
## Testing
58+
59+
All existing tests pass with the implemented changes, ensuring no functional regressions while providing performance benefits.

src/emulator-manager.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,29 @@ export async function launchEmulator(
3737
);
3838
}
3939

40-
if (cores) {
41-
await exec.exec(`sh -c \\"printf 'hw.cpu.ncore=${cores}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
42-
}
43-
44-
if (ramSize) {
45-
await exec.exec(`sh -c \\"printf 'hw.ramSize=${ramSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
46-
}
40+
if (cores || ramSize || heapSize || enableHardwareKeyboard || diskSize) {
41+
const configEntries: string[] = [];
4742

48-
if (heapSize) {
49-
await exec.exec(`sh -c \\"printf 'hw.heapSize=${heapSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
50-
}
51-
52-
if (enableHardwareKeyboard) {
53-
await exec.exec(`sh -c \\"printf 'hw.keyboard=yes\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
54-
}
43+
if (cores) {
44+
configEntries.push(`hw.cpu.ncore=${cores}`);
45+
}
46+
if (ramSize) {
47+
configEntries.push(`hw.ramSize=${ramSize}`);
48+
}
49+
if (heapSize) {
50+
configEntries.push(`hw.heapSize=${heapSize}`);
51+
}
52+
if (enableHardwareKeyboard) {
53+
configEntries.push('hw.keyboard=yes');
54+
}
55+
if (diskSize) {
56+
configEntries.push(`disk.dataPartition.size=${diskSize}`);
57+
}
5558

56-
if (diskSize) {
57-
await exec.exec(`sh -c \\"printf 'disk.dataPartition.size=${diskSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`);
59+
if (configEntries.length > 0) {
60+
const configContent = configEntries.join('\\n') + '\\n';
61+
await exec.exec(`sh -c \\"printf '${configContent}' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini"`);
62+
}
5863
}
5964

6065
// turn off hardware acceleration on Linux

0 commit comments

Comments
 (0)