Skip to content

Commit 5c07787

Browse files
committed
Add platform-specific build optimizations for web, desktop, and mobile
1 parent a431c01 commit 5c07787

File tree

7 files changed

+340
-43
lines changed

7 files changed

+340
-43
lines changed

.github/workflows/README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ The CI workflow automates building, testing, and releasing the OpenSVM-Dioxus ap
1515
### Jobs
1616

1717
1. **build-and-test**: Builds and tests the application on Ubuntu, macOS, and Windows
18-
2. **build-web**: Builds the web (WASM) version of the application
19-
3. **build-desktop**: Builds desktop versions for Linux, macOS (Intel and Apple Silicon), and Windows
20-
4. **build-android**: Builds the Android APK
18+
2. **build-web**: Builds the optimized web (WASM) version of the application, including WASM size reduction, code minification, and SIMD acceleration
19+
3. **build-desktop**: Builds optimized desktop versions for Linux, macOS (Intel and Apple Silicon), and Windows with native CPU instruction sets and performance tuning
20+
4. **build-android**: Builds optimized Android APK with resource management and balanced performance/size optimizations
2121
5. **create-release**: Collects all artifacts and attaches them to the GitHub release
2222
6. **homebrew**: Creates and updates a Homebrew formula for easy installation on macOS
2323

@@ -52,10 +52,41 @@ brew tap opensvm/opensvm
5252
brew install opensvm-dioxus
5353
```
5454

55+
### Platform-Specific Optimizations
56+
57+
The CI workflow applies several platform-specific optimizations to ensure optimal performance:
58+
59+
#### Web (WASM) Optimizations
60+
61+
- Uses custom `wasm-release` profile optimized for binary size
62+
- Enables WASM SIMD instructions with `-C target-feature=+atomics,+bulk-memory,+simd128`
63+
- Applies `wasm-opt -Oz` for additional size optimization
64+
- Minifies JavaScript with terser for faster loading
65+
66+
#### Desktop Optimizations
67+
68+
- Uses `-C target-cpu=native` to utilize all available CPU features
69+
- Applies fat LTO for maximum runtime performance
70+
- Configures thread pool size based on available CPU cores
71+
- Optimizes memory allocator settings for desktop environments
72+
73+
#### Android Optimizations
74+
75+
- Uses thin LTO for balanced performance and APK size
76+
- Optimizes APKs with zipalign for improved runtime memory usage
77+
- Automatically signs APKs for installation
78+
- Limits background threads to conserve battery life
79+
5580
### Customization
5681

5782
To modify the CI workflow:
5883

5984
1. Edit the `.github/workflows/ci.yml` file
6085
2. Commit and push your changes
61-
3. The updated workflow will be used for subsequent runs
86+
3. The updated workflow will be used for subsequent runs
87+
88+
To modify platform-specific optimizations:
89+
90+
1. Edit `opensvm-dioxus/src/platform_optimizations.rs` for runtime optimizations
91+
2. Edit `opensvm-dioxus/Cargo.toml` for compile-time optimizations
92+
3. Update the appropriate job in the CI workflow for build-time optimizations

.github/workflows/ci.yml

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
working-directory: opensvm-dioxus
5454
run: cargo test
5555

56-
# Build for web (WASM)
56+
# Build for web (WASM) with optimizations
5757
build-web:
5858
name: Build Web
5959
runs-on: ubuntu-latest
@@ -80,10 +80,26 @@ jobs:
8080
- name: Install Dioxus CLI
8181
run: cargo install dioxus-cli --locked
8282

83-
- name: Build for web
83+
- name: Install wasm-opt
84+
run: |
85+
sudo apt-get update
86+
sudo apt-get install -y binaryen
87+
88+
- name: Install js-minifier
89+
run: npm install -g terser
90+
91+
- name: Build for web (with optimizations)
8492
working-directory: opensvm-dioxus
8593
run: |
86-
dioxus build --release --platform web
94+
# Build using the wasm-release profile optimized for size
95+
RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+simd128" \
96+
dioxus build --features web --profile wasm-release --platform web --release
97+
98+
# Additional optimization with wasm-opt for smaller binary size
99+
find dist -name "*.wasm" -exec wasm-opt -Oz -o {} {} \;
100+
101+
# Minify JavaScript files
102+
find dist -name "*.js" -exec terser --compress --mangle -o {} {} \;
87103
88104
- name: Package web build
89105
working-directory: opensvm-dioxus
@@ -143,10 +159,15 @@ jobs:
143159
sudo apt-get update
144160
sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.0-dev libappindicator3-dev librsvg2-dev patchelf
145161
146-
- name: Build desktop app
162+
- name: Build desktop app with optimizations
147163
working-directory: opensvm-dioxus
148164
run: |
149-
dioxus build --release --platform desktop --target ${{ matrix.target }}
165+
# Install additional dependency for desktop optimizations
166+
cargo add num_cpus --features
167+
168+
# Build with desktop-specific optimizations
169+
RUSTFLAGS="-C target-cpu=native -C opt-level=3 -C lto=fat" \
170+
dioxus build --features desktop --profile desktop-release --release --platform desktop --target ${{ matrix.target }}
150171
151172
- name: Rename binary (Linux/macOS)
152173
if: matrix.os != 'windows-latest'
@@ -203,10 +224,22 @@ jobs:
203224
- name: Install cargo-ndk
204225
run: cargo install cargo-ndk
205226

206-
- name: Build for Android
227+
- name: Install Android-specific dependencies
228+
run: |
229+
cargo add num_cpus --features
230+
231+
- name: Build for Android with optimizations
207232
working-directory: opensvm-dioxus
208233
run: |
209-
dioxus build --release --platform android
234+
# Build with Android-specific optimizations
235+
RUSTFLAGS="-C opt-level=3 -C lto=thin -C codegen-units=1" \
236+
dioxus build --features android --release --platform android
237+
238+
# Apply additional APK optimizations using Android build tools
239+
cd target/release/apk
240+
find . -name "*.apk" -exec echo "Optimizing {}" \; -exec zipalign -v -p 4 {} {}.aligned \; -exec mv {}.aligned {} \;
241+
# Sign APK with debug key for testing - in production you would use your release keystore
242+
find . -name "*.apk" -exec echo "Signing {}" \; -exec $ANDROID_SDK_ROOT/build-tools/*/apksigner sign --ks ~/.android/debug.keystore --ks-pass pass:android --key-pass pass:android {} \;
210243
211244
- name: Upload Android APK
212245
uses: actions/upload-artifact@v3

opensvm-dioxus/Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

opensvm-dioxus/Cargo.toml

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,73 @@ repository = "https://github.com/openSVM/opensvm-mobile"
99
homepage = "https://github.com/openSVM/opensvm-mobile"
1010
documentation = "https://github.com/openSVM/opensvm-mobile/tree/main/opensvm-dioxus"
1111

12+
# Platform-specific features
13+
[features]
14+
default = []
15+
web = [
16+
"dioxus-web",
17+
"wasm-bindgen",
18+
"wasm-bindgen-futures",
19+
"web-sys",
20+
"js-sys",
21+
"gloo",
22+
"console_log",
23+
]
24+
desktop = [
25+
"dioxus-desktop",
26+
"env_logger",
27+
"num_cpus",
28+
]
29+
android = [
30+
"dioxus-mobile",
31+
"env_logger",
32+
"num_cpus",
33+
]
34+
35+
# Define platform optimization profiles
36+
[profile.release]
37+
opt-level = 3
38+
lto = true
39+
codegen-units = 1
40+
panic = "abort"
41+
42+
# Optimize for web (smaller binary size)
43+
[profile.release.package."*"]
44+
opt-level = 'z'
45+
46+
# Profile for optimizing binary size (used for web)
47+
[profile.wasm-release]
48+
inherits = "release"
49+
opt-level = 'z' # Optimize for size
50+
lto = true
51+
codegen-units = 1
52+
panic = "abort"
53+
strip = true
54+
55+
# Profile for desktop builds (optimized for speed)
56+
[profile.desktop-release]
57+
inherits = "release"
58+
opt-level = 3 # Optimize for speed
59+
lto = "fat"
60+
codegen-units = 1
61+
1262
[dependencies]
1363
dioxus = "0.4.0"
14-
dioxus-web = "0.4.0"
15-
dioxus-desktop = "0.4.0"
1664
dioxus-router = "0.4.0"
1765
dioxus-signals = "0.4.0"
1866
dioxus-free-icons = { version = "0.7.0", features = ["font-awesome-solid"] }
19-
wasm-bindgen = "0.2.87"
20-
web-sys = { version = "0.3.64", features = [
67+
log = "0.4.20"
68+
serde = { version = "1.0.188", features = ["derive"] }
69+
serde_json = "1.0.107"
70+
chrono = { version = "0.4.31", features = ["serde"] }
71+
72+
# Optional dependencies based on features
73+
dioxus-web = { version = "0.4.0", optional = true }
74+
dioxus-desktop = { version = "0.4.0", optional = true }
75+
dioxus-mobile = { version = "0.4.0", optional = true }
76+
wasm-bindgen = { version = "0.2.87", optional = true }
77+
js-sys = { version = "0.3.64", optional = true }
78+
web-sys = { version = "0.3.64", optional = true, features = [
2179
"Storage",
2280
"Window",
2381
"Document",
@@ -30,13 +88,13 @@ web-sys = { version = "0.3.64", features = [
3088
"Clipboard",
3189
"ClipboardEvent",
3290
"Location",
91+
"Performance",
3392
] }
34-
gloo = "0.10.0"
35-
log = "0.4.20"
36-
console_log = "1.0.0"
37-
env_logger = "0.10.0"
38-
serde = { version = "1.0.188", features = ["derive"] }
39-
serde_json = "1.0.107"
40-
chrono = { version = "0.4.31", features = ["serde"] }
41-
wasm-bindgen-futures = "0.4.37"
93+
gloo = { version = "0.10.0", optional = true }
94+
console_log = { version = "1.0.0", optional = true }
95+
env_logger = { version = "0.10.0", optional = true }
96+
wasm-bindgen-futures = { version = "0.4.37", optional = true }
4297
uuid = { version = "1.4.1", features = ["v4", "js"] }
98+
99+
# Optimization-related dependencies
100+
num_cpus = { version = "1.16.0", optional = true }

opensvm-dioxus/README.md

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,52 @@ brew tap opensvm/opensvm
104104
brew install opensvm-dioxus
105105
```
106106

107+
## Platform-Specific Optimizations
108+
109+
This application implements several platform-specific optimizations to ensure optimal performance across different devices:
110+
111+
### Web Optimizations
112+
113+
- **WASM Size Reduction**: Uses special build profile (`wasm-release`) to minimize binary size
114+
- **Code Minification**: JavaScript and CSS are minified for faster loading
115+
- **WASM Optimization**: Uses `wasm-opt` to further reduce WebAssembly file size
116+
- **SIMD Acceleration**: Enables WASM SIMD instructions where supported
117+
- **Performance Hints**: Sets browser performance hints for computationally intensive operations
118+
119+
### Desktop Optimizations
120+
121+
- **Native CPU Instructions**: Builds with `-C target-cpu=native` to use all available CPU features
122+
- **Thread Pool Tuning**: Automatically sizes thread pools based on available CPU cores
123+
- **Memory Optimizations**: Configures memory allocator for desktop environments
124+
- **Link-Time Optimization**: Uses fat LTO for maximum runtime performance
125+
126+
### Mobile (Android) Optimizations
127+
128+
- **APK Optimization**: Uses zipalign for better runtime memory usage
129+
- **Resource Management**: Limits thread usage to save battery life
130+
- **Binary Size Optimization**: Uses thin LTO to balance size and performance
131+
- **Adaptive Performance**: Detects device capabilities and adjusts accordingly
132+
133+
### How to Build with Optimizations
134+
135+
```bash
136+
# Web (optimized for size)
137+
dioxus build --features web --profile wasm-release --platform web --release
138+
139+
# Desktop (optimized for speed)
140+
RUSTFLAGS="-C target-cpu=native -C opt-level=3" dioxus build --features desktop --profile desktop-release --platform desktop --release
141+
142+
# Android (balanced optimization)
143+
RUSTFLAGS="-C opt-level=3 -C lto=thin" dioxus build --features android --platform android --release
144+
```
145+
107146
## Continuous Integration
108147

109148
This project uses GitHub Actions for continuous integration and deployment. The CI pipeline automatically:
110149

111150
- Builds and tests the application on multiple platforms
112-
- Creates release binaries for Windows, macOS, Linux, Android, and the web
151+
- Creates release binaries for Windows, macOS, Linux, Android, and the web with platform-specific optimizations
152+
- Applies additional post-build optimizations (minification, compression)
113153
- Generates a Homebrew formula for easy installation on macOS
114154

115155
For more details, see the [CI workflow documentation](../.github/workflows/README.md).
@@ -118,16 +158,17 @@ For more details, see the [CI workflow documentation](../.github/workflows/READM
118158

119159
```
120160
src/
121-
├── app.rs # Main application component
122-
├── assets/ # Static assets
123-
├── components/ # Reusable UI components
124-
├── constants/ # App constants
125-
├── main.rs # Entry point
126-
├── routes/ # Application routes
127-
├── stores/ # State management
128-
└── utils/ # Utility functions
161+
├── app.rs # Main application component
162+
├── assets/ # Static assets
163+
├── components/ # Reusable UI components
164+
├── constants/ # App constants
165+
├── main.rs # Entry point
166+
├── platform_optimizations.rs # Platform-specific optimizations
167+
├── routes/ # Application routes
168+
├── stores/ # State management
169+
└── utils/ # Utility functions
129170
```
130171

131172
## License
132173

133-
This project is licensed under the MIT License - see the LICENSE file for details.
174+
This project is licensed under the MIT License - see the LICENSE file for details.

0 commit comments

Comments
 (0)