PHP Performance Optimization
This document describes the PHP performance optimizations implemented in wprint3d-core for systems running on hard-disk or SD-card storage.
Overview
The optimization system uses a layered approach:
Hardware Detection - Automatically detects storage type (HDD/SSD/SD-card/microSD) and available RAM
PHP OPcache - Caches compiled bytecode in shared memory with JIT compilation
Full Ramdisk - Mirrors entire
/var/wwwto tmpfs for eligible roles on slow storageOptional Application Caching - Primes config and route caches
Environment Variables
Variable |
Default |
Description |
|---|---|---|
|
|
Set to |
|
|
Enable application-level caching |
|
|
When |
Performance Improvements
On hard-disk systems, expect:
70-80% faster cold starts (2-5s → 0.5-1s)
70-80% faster subsequent requests (200-500ms → 50-100ms)
70-80% faster scheduler execution (500ms-1s → 100-200ms)
Memory Requirements
The ramdisk self-sizes based on the actual size of /var/www (excluding persistent data). It adds 25% headroom (to cover tar/filesystem overhead) and requires at least 2x the ramdisk size in available RAM.
|
Ramdisk size (1.25x) |
Min RAM required (2x) |
|---|---|---|
120MB |
150MB |
300MB |
150MB |
188MB |
376MB |
200MB |
250MB |
500MB |
300MB |
375MB |
750MB |
If available RAM is below the 2x threshold, the ramdisk is skipped and the app runs from disk with OPcache only.
Development Notes
In development mode (DEVELOPER_MODE=true):
OPcache revalidates files every 2 seconds
Code changes take effect quickly
Run
php artisan clear-compiledif changes don’t appear
In production mode:
OPcache never revalidates (maximum performance)
Code changes require container restart
This is the correct pattern for containerized applications
Ramdisk Behavior
The ramdisk mirrors the entire /var/www directory to tmpfs at container startup. This eliminates all disk I/O for PHP file loading on slow storage (HDD, SD card, microSD). The ramdisk persists for the container’s lifetime and is automatically cleaned up by the kernel when the container stops.
Role-Based Eligibility
Most PHP-based roles benefit from a ramdisk. Even long-running processes like Octane benefit because cold start on HDD requires reading ~116MB of vendor files before classes go resident:
Role |
Ramdisk |
Reason |
|---|---|---|
server (Octane) |
Enable |
Cold start reads all vendor files from disk |
concurrency-scheduler |
Enable |
Supervisor respawns |
ws-server |
Enable |
Crash-restart loop spawns fresh PHP |
scheduler |
Enable |
Each cron job is a fresh PHP process |
mapper |
Skip |
Long-running udev monitor, not PHP-heavy |
streamer |
Skip |
Native binaries, not PHP |
Multi-role mode: When ROLE contains a comma-separated list (e.g., ROLE=server,scheduler,concurrency-scheduler,ws-server), all processes run under supervisord in a single container sharing one ramdisk. The default docker-compose.yml uses this mode via the backend service, which runs all four PHP-based roles in a single container with one shared ramdisk (~116MB).
What’s on Ramdisk vs. Disk
On ramdisk (entire /var/www minus exclusions):
vendor/— PHP dependencies (~116MB, the main I/O bottleneck)app/,config/,routes/,lang/— application codebootstrap/cache/,storage/framework/— ephemeral cachespublic/,internal/— static assets and scripts
Excluded (kept on disk for persistence/debugging):
storage/app/gcode/— user-uploaded G-code filesstorage/app/recordings/— camera recordingsstorage/app/public/— user-uploaded mediastorage/app/plugins/— plugin statestorage/logs/— application logs.env— application secrets (only when it’s a regular file, not a symlink).external-configs/— external secret volume mount
How It Works
run.shcreates all required directories (including persistent subdirectories)detect-hardware.shidentifies storage type and available RAMramdisk-setup.shmeasures/var/wwwsize, adds 10% headroomIf available RAM >= 2x ramdisk size, a
tmpfsis mounted/var/wwwis copied to the ramdisk (excluding persistent dirs)Persistent directories are stashed via bind mounts before the overlay
The ramdisk is bind-mounted over
/var/wwwPersistent directories are bound back from the stash onto the overlay
If any step fails, a full rollback restores the original /var/www.
Storage Type Detection
Storage |
Detection |
Ramdisk |
|---|---|---|
HDD |
|
Enabled |
SSD |
|
Skipped (unless forced) |
microSD/eMMC |
Device name starts with |
Enabled |
SD card |
|
Enabled |
Unknown |
Fallback |
Enabled |
Troubleshooting
Verify OPcache is working
php internal/test-opcache.php
Check hardware detection
source internal/detect-hardware.sh
echo "Storage: $WPRINT3D_STORAGE_TYPE"
echo "Memory: ${WPRINT3D_AVAILABLE_MEMORY_MB}MB"
Force ramdisk on SSD systems
export WPRINT3D_FORCE_RAMDISK=1
Disable ramdisk on low-memory systems
export WPRINT3D_FORCE_RAMDISK=0