Performance Modes
SimpleDto provides multiple performance modes to balance features and speed based on your needs.
Performance Modes Overview
Section titled “Performance Modes Overview”| Mode | Speed | Features | Use Case |
|---|---|---|---|
| Normal | 12.7μs | Full features | Default mode with all features |
| #[UltraFast] | 1.7μs | Minimal | Maximum speed with basic mapping |
| #[NoCasts] | ~8μs | No casting | Skip type casting |
| #[NoValidation] | ~10μs | No validation | Skip validation |
| #[NoAttributes] | ~9μs | No attributes | Skip attribute processing |
#[UltraFast] Mode
Section titled “#[UltraFast] Mode”The #[UltraFast] attribute provides maximum performance by bypassing all SimpleDto overhead.
Features
Section titled “Features”- ✅ 7.4x faster than normal SimpleDto
- ✅ Direct reflection (no cache overhead)
- ✅ Minimal memory (2.7mb vs 6.7mb)
- ✅ Supports
#[MapFrom]and#[MapTo]attributes - ✅ Handles nested DTOs recursively
- ❌ No validation
- ❌ No type casting
- ❌ No lazy loading
- ❌ No optional properties
- ❌ No computed properties
use event4u\DataHelpers\SimpleDto;use event4u\DataHelpers\SimpleDto\Attributes\UltraFast;use event4u\DataHelpers\SimpleDto\Attributes\MapFrom;
#[UltraFast]class FastUserDto extends SimpleDto{ public function __construct( #[MapFrom('user_name')] public readonly string $name, public readonly string $email, public readonly int $age, ) {}}
// Create from array (1.7μs)$user = FastUserDto::fromArray([ 'user_name' => 'John Doe', 'email' => 'john@example.com', 'age' => 30,]);
// Convert to array$array = $user->toArray();// ['name' => 'John Doe', 'email' => 'john@example.com', 'age' => 30]Nested DTOs
Section titled “Nested DTOs”#[UltraFast]class FastAddressDto extends SimpleDto{ public function __construct( public readonly string $street, public readonly string $city, ) {}}
#[UltraFast]class FastUserDto extends SimpleDto{ public function __construct( public readonly string $name, public readonly FastAddressDto $address, ) {}}
$user = FastUserDto::fromArray([ 'name' => 'John', 'address' => [ 'street' => '123 Main St', 'city' => 'New York', ],]);Automatic Attribute Detection
Section titled “Automatic Attribute Detection”#[UltraFast] automatically detects and processes #[MapFrom], #[MapTo], #[CastWith] and #[Hidden] attributes if they are present on properties. No configuration needed!
use event4u\DataHelpers\SimpleDto\Attributes\Hidden;
#[UltraFast]class AutoDetectDto extends SimpleDto{ public function __construct( #[MapFrom('old_name')] public readonly string $name, // Automatically detected!
public readonly string $email,
#[Hidden] public readonly string $password, // Automatically hidden! ) {}}
$dto = AutoDetectDto::fromArray([ 'old_name' => 'John', 'email' => 'john@example.com', 'password' => 'secret123',]);
$array = $dto->toArray();// ['name' => 'John', 'email' => 'john@example.com']// password is hidden!JSON/XML Support with #[ConverterMode]
Section titled “JSON/XML Support with #[ConverterMode]”Combine #[UltraFast] with #[ConverterMode] to enable JSON/XML/CSV parsing with minimal overhead:
use event4u\DataHelpers\SimpleDto;use event4u\DataHelpers\SimpleDto\Attributes\UltraFast;use event4u\DataHelpers\SimpleDto\Attributes\ConverterMode;
#[UltraFast]#[ConverterMode]class ApiDto extends SimpleDto{ public function __construct( public readonly string $name, public readonly string $email, ) {}}
// Accept JSON (1.3-1.5μs)$dto = ApiDto::from('{"name": "John", "email": "john@example.com"}');
// Accept XML (1.3-1.5μs)$dto = ApiDto::from('<root><name>John</name><email>john@example.com</email></root>');
// Accept arrays (0.8μs - no parsing overhead)$dto = ApiDto::from(['name' => 'John', 'email' => 'john@example.com']);Performance:
- Array only (without
#[ConverterMode]): ~0.8μs - With JSON/XML (with
#[ConverterMode]): ~1.3-1.5μs - Still 12x faster than normal SimpleDto (~18.4μs)
Note: Use from() method for mixed input types. fromArray() only accepts arrays.
#[NoCasts] Mode
Section titled “#[NoCasts] Mode”Skip all type casting for better performance when input data is already correctly typed.
Features
Section titled “Features”- ✅ 34-63% faster than normal SimpleDto
- ✅ All other features work (validation, mapping, etc.)
- ❌ No automatic type conversion
- ❌ No nested DTO auto-casting
use event4u\DataHelpers\SimpleDto\Attributes\NoCasts;
#[NoCasts]class StrictDto extends SimpleDto{ public function __construct( public readonly string $name, public readonly int $age, ) {}}
// This works (correct types)$dto = StrictDto::fromArray(['name' => 'John', 'age' => 30]);
// This throws TypeError (wrong types, no casting)$dto = StrictDto::fromArray(['name' => 'John', 'age' => '30']);#[NoValidation] Mode
Section titled “#[NoValidation] Mode”Skip validation for better performance when input data is already validated.
Features
Section titled “Features”- ✅ 20-30% faster than normal SimpleDto
- ✅ All other features work (casting, mapping, etc.)
- ❌ No validation attributes processed
use event4u\DataHelpers\SimpleDto\Attributes\NoValidation;
#[NoValidation]class TrustedDto extends SimpleDto{ public function __construct( public readonly string $name, public readonly string $email, ) {}}#[NoAttributes] Mode
Section titled “#[NoAttributes] Mode”Skip all attribute processing for maximum performance.
Features
Section titled “Features”- ✅ 25-40% faster than normal SimpleDto
- ✅ Basic DTO functionality works
- ❌ No validation attributes
- ❌ No visibility attributes
- ❌ No mapping attributes
- ❌ No cast attributes
use event4u\DataHelpers\SimpleDto\Attributes\NoAttributes;
#[NoAttributes]class MinimalDto extends SimpleDto{ public function __construct( public readonly string $name, public readonly int $age, ) {}}Combining Attributes
Section titled “Combining Attributes”You can combine multiple performance attributes:
#[NoCasts, NoValidation]class FastDto extends SimpleDto{ public function __construct( public readonly string $name, public readonly int $age, ) {}}Performance Comparison
Section titled “Performance Comparison”Benchmark Results (10,000 iterations):
Normal SimpleDto: 12.740μs (baseline)#[NoValidation]: ~10.000μs (20% faster)#[NoAttributes]: ~9.000μs (29% faster)#[NoCasts]: ~8.000μs (37% faster)#[NoCasts, NoValidation]: ~6.000μs (53% faster)#[UltraFast]: 1.723μs (639% faster)
Plain PHP Constructor: 0.141μs (9,000% faster)Other Dto Libraries: 0.315μs (4,000% faster)Choosing the Right Mode
Section titled “Choosing the Right Mode”Use Normal Mode When:
Section titled “Use Normal Mode When:”- ✅ You need full features (validation, casts, mapping)
- ✅ Performance is acceptable (not in tight loops)
- ✅ Developer experience is priority
- ✅ Type safety and validation are important
Use #[UltraFast] When:
Section titled “Use #[UltraFast] When:”- ✅ You need maximum speed
- ✅ You only need basic mapping (#[MapFrom], #[MapTo])
- ✅ Input data is already validated
- ✅ Input data is already correctly typed
- ✅ You’re processing large datasets
Use #[NoCasts] When:
Section titled “Use #[NoCasts] When:”- ✅ Input data is already correctly typed
- ✅ You need validation but not casting
- ✅ You want better performance without losing features
Use #[NoValidation] When:
Section titled “Use #[NoValidation] When:”- ✅ Input data is already validated (e.g., from database)
- ✅ You need casting but not validation
- ✅ You trust the data source
Use #[NoAttributes] When:
Section titled “Use #[NoAttributes] When:”- ✅ You don’t use any attributes
- ✅ You want simple DTOs with better performance
- ✅ You only need basic fromArray/toArray functionality
Real-World Example
Section titled “Real-World Example”// API endpoint - needs validation and castingclass CreateUserRequest extends SimpleDto{ #[Required, Email] public readonly string $email;
#[Required, Min(3)] public readonly string $name;}
// Internal DTO - already validated, use #[UltraFast]#[UltraFast]class UserDto extends SimpleDto{ public function __construct( public readonly string $email, public readonly string $name, public readonly int $id, ) {}}
// Database result - already typed, use #[NoCasts]#[NoCasts]class UserFromDatabase extends SimpleDto{ public function __construct( public readonly int $id, public readonly string $email, public readonly string $name, ) {}}See Also
Section titled “See Also”- Performance Benchmarks - Detailed benchmark results
- Caching Guide - Cache optimization strategies
- Performance Attributes - Detailed attribute documentation