The Database Performance Problem
Traditional database operations in PHP involve multiple data copies:- Database sends result over network
- Native driver allocates memory for result
- PDO copies data into PHP-compatible structures
- Your code converts to PHP arrays
json_encode()serializes for API response
Zero-Copy Architecture
Lyger’s Zero-Copy Database keeps data in Rust memory and only copies once:- Database sends result to Rust
- Rust stores in native memory structures
- Rust returns pointer (memory address) to PHP
- When needed, Rust serializes directly to JSON
- PHP receives final JSON (one copy)
The result set stays in Rust’s memory until you’re ready to use it. No intermediate copies.
How It Works
Query Execution
lyger_db_query FFI function:
- Connects to the database (or reuses connection)
- Executes the SQL query
- Stores results in Rust’s heap memory
- Returns the memory address as an integer
The return value is a memory pointer disguised as an integer. PHP doesn’t touch the actual data.
Result Serialization
lyger_jsonify_result FFI function:
- Receives the pointer to the result set
- Iterates through rows in Rust (fast!)
- Serializes directly to JSON string
- Returns JSON to PHP
Memory Cleanup
High-Level API
ThedbQueryJson method combines all three operations:
Usage Examples
Basic Query
Manual Memory Management
For advanced use cases, manage the pointer lifecycle manually:Why hold the pointer?
Why hold the pointer?
In some scenarios, you may want to defer serialization:
- Execute multiple queries concurrently
- Perform calculations before fetching results
- Reduce memory pressure by serializing only when needed
Database Support
Lyger’s Zero-Copy Database supports multiple database engines through their Rust drivers:The Rust driver is determined by the DSN prefix. All drivers support the same zero-copy architecture.
Connection Examples
Performance Comparison
Memory Usage
Let’s query 10,000 user records (~1MB result set):Speed Benchmarks
Query:SELECT * FROM users LIMIT 10000
Actual performance depends on database, network latency, and hardware. Zero-Copy shines with large result sets.
Integration with QueryBuilder
While the Engine class provides low-level access, you’ll typically use the QueryBuilder:Currently, QueryBuilder uses PDO internally. Zero-Copy database is used by Cache and internal systems. Full QueryBuilder integration is planned for future releases.
Advanced: Streaming Results
For extremely large result sets, you can stream data:Connection Pooling
The Rust layer maintains connection pools for better performance:- No connection overhead for subsequent queries
- Connections kept alive across requests (Always-Alive server)
- Automatic reconnection on failure
- Configurable pool size and timeouts
In Always-Alive mode, database connections persist across HTTP requests, making subsequent queries even faster.
Error Handling
Common error scenarios
Common error scenarios
- Pointer is 0: Query execution failed (syntax error, connection issue, etc.)
- Invalid JSON: Result serialization failed (encoding issue, memory corruption)
- Exception during processing: Network timeout, connection lost, out of memory
Best Practices
1. Always Free Results
2. Check for Errors
3. Use Prepared Statements
4. Limit Result Size
Troubleshooting
Query Returns Empty Array
- Table is empty
- Query syntax error
- Connection failed
- FFI not available
Memory Leaks
If memory grows over time:- Ensure you call
freeResult()for every query - Use
dbQueryJson()which handles cleanup automatically - Check for stored pointers in class properties
Future Improvements
Planned features for future releases:- Parameter binding - Safe query parameters
- Streaming results - Process huge datasets chunk-by-chunk
- Query result objects - Type-safe result handling
- Async queries - Non-blocking database operations
- QueryBuilder integration - Full zero-copy support in QueryBuilder
Next Steps
Architecture Overview
Understand the complete architecture
Rust FFI Integration
Learn about FFI and memory management
Query Builder
Use the fluent query builder
Models
Work with Eloquent-style models