Performance & Optimization
Relevant source files
The following files were used as context for generating this wiki page:The performance and optimization systems in Skid-Inc are designed to enhance the user experience by ensuring a fast application startup and smooth, efficient runtime operation. This is achieved through two primary modules: the StartupOptimizer and the PerformanceManager. The StartupOptimizer focuses on minimizing the application's initial load time, with a target of under 3 seconds, by using techniques like asynchronous loading and metadata caching.
The PerformanceManager handles runtime optimizations, particularly for the resource-intensive save system. It implements features such as memory monitoring to prevent excessive resource consumption, data compression to reduce save file size, and an adaptive save frequency that adjusts based on user activity. These systems work together to provide a responsive and reliable experience, which is crucial for an idle game with frequent background processes.
Sources: src/startup-optimizer.js:13-14, src/main.js:28-31, src/performance-manager.js:8-9
Startup Optimizer
The StartupOptimizer class is responsible for managing the application's startup sequence to meet a performance target of 3 seconds. It achieves this through asynchronous operations, metadata caching, and preloading of essential data. It is initialized and executed within the main application entry point, src/main.js.
Sources: src/startup-optimizer.js:8-9, src/main.js:213-226
Optimized Startup Flow
The core of the StartupOptimizer is the startOptimizedStartup method. This method orchestrates a multi-phase startup process designed to perform non-essential tasks asynchronously, prioritizing a fast initial launch.
The following diagram illustrates the phases of the optimized startup process.
Sources: src/startup-optimizer.js:68-132
Metadata Caching
To accelerate startup, StartupOptimizer caches save file metadata. This cache is stored in a JSON file and is used to avoid costly file system operations on subsequent launches.
- Cache File:
.startup_cache.json, located in the save directory. - Validation: The cache is considered valid if its version matches
cacheVersionand its age is less thancacheMaxAge. - Cache Invalidation: The cache is rebuilt if it's expired (older than 24 hours) or the version mismatches.
Sources: src/startup-optimizer.js:23-25, src/startup-optimizer.js:192-220
Save File Preloading
The optimizer preloads metadata for the 5 most recently modified save files. This allows for near-instantaneous access to information about these saves without reading them from disk. The loadSaveFileOptimized method leverages this preloaded data to speed up loading a specific save file.
The renderer-side SaveStateManager uses an IPC call to load-save-optimized to take advantage of this feature.
*Sources: src/startup-optimizer.js:246-286, src/renderer/save-manager.js:283-322
Configuration
The behavior of the StartupOptimizer can be configured through several properties.
| Property | Type | Default Value | Description |
|---|---|---|---|
targetStartupTime | number | 3000 | The target startup time in milliseconds. |
enableAsyncLoading | boolean | true | Enables asynchronous preloading and metadata refresh. |
enableMetadataCache | boolean | true | Enables the use of a metadata cache file. |
enablePreloading | boolean | true | Enables preloading of recent save files. |
cacheMaxAge | number | 86400000 (24h) | Maximum age of the metadata cache in milliseconds before it's invalidated. |
cacheVersion | string | 1.0.0 | Version of the cache structure to handle format changes. |
Sources: src/startup-optimizer.js:15-25
Performance Manager
The PerformanceManager is a runtime module that optimizes the application, primarily focusing on the save system. It is instantiated by the AutoSaveManager and provides three key features: memory monitoring, data compression, and adaptive save frequency.
This diagram shows the structure of the PerformanceManager class.
Memory Monitoring
The manager periodically checks memory usage to prevent the application from exceeding predefined limits.
- Memory Limit:
200MB - Check Interval:
30 seconds - Thresholds:
- Warning:
80%of limit (memory-warningevent emitted). - Critical:
95%of limit (memory-criticalevent emitted,global.gc()is called if available).
- Warning:
- History: Keeps a history of the last 100 memory usage snapshots.
Sources: src/performance-manager.js:14-17, src/performance-manager.js:90-151
Data Compression
To reduce disk I/O and storage space, the PerformanceManager compresses save data before writing it to a file.
- Algorithm: Gzip (via Node.js
zlibmodule). - Activation: Compression is enabled by default (
compressionEnabled: true). - Threshold: Data is only compressed if its size is greater than
1024bytes (1KB). - Configuration: The compression level defaults to
6, offering a balance between speed and compression ratio.
The compressData and decompressData methods handle these operations.
Sources: src/performance-manager.js:1-6, src/performance-manager.js:20-22, src/performance-manager.js:158-219
Adaptive Save Frequency
The PerformanceManager adjusts the auto-save interval based on user activity to optimize performance. During periods of low activity, the save frequency is reduced to conserve resources. During high activity, it is increased to prevent data loss.
The logic for adjusting the save interval is as follows:
Key configuration values for this feature are:
baseSaveInterval:30000mslowActivityThreshold:5activities per minutehighActivityThreshold:30activities per minutelowActivityMultiplier:2(saves every 60s)highActivityMultiplier:0.5(saves every 15s)
Sources: src/performance-manager.js:25-33
Integration and Usage
The optimization modules are integrated into the application's main and renderer processes to provide system-wide performance enhancements.
Main Process Integration
In src/main.js, the StartupOptimizer is a key component of the application's launch sequence. It is initialized early and its startOptimizedStartup method is called to manage the loading process.
The AutoSaveManager in src/auto-save-manager.js instantiates and utilizes the PerformanceManager to enable adaptive save intervals and other runtime optimizations.
The following sequence diagram shows the application startup and how a save file is loaded using the optimization modules.
IPC Handlers
Several IPC handlers are exposed in src/main.js to allow the renderer process to interact with the optimization systems.
| IPC Channel | Description | Source File |
|---|---|---|
get-startup-statistics | Retrieves performance metrics from the StartupOptimizer. | src/main.js |
load-save-optimized | Loads a save file using the StartupOptimizer's optimized method. | src/main.js |
get-preloaded-saves | Gets a list of save files whose metadata has been preloaded. | src/main.js |
clear-startup-caches | Instructs the StartupOptimizer to clear its caches. | src/main.js |
update-startup-config | Updates the configuration of the StartupOptimizer. | src/main.js |
Sources: src/main.js:588-718
Build & Deployment Performance
Performance considerations extend to the project's build and deployment pipeline, as documented in scripts/README.md.
Build Performance
The build process can be optimized by:
- Using SSD storage for faster I/O.
- Increasing available RAM.
- Caching dependencies between builds.
Sources: scripts/README.md
Deployment Optimization
The deployment scripts aim for efficiency through:
- Compressing artifacts into ZIP archives.
- Using a CDN for distribution (recommended).
- Implementing delta updates for smaller download sizes.
Sources: scripts/README.md, scripts/deploy.js:200-220