Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize creation from constructor (#186)
When object gets created from constructor, there are always checks for constructor arguments in context. This eats some of performance, especially if you don't use context. When looking at the generated code, I noticed that it can be optimized a little. I didn't benchmark it a lot, but I did see around 10% more performance with this optimization ## Comparisons ### Argument from source Before: ```php if (\AutoMapper\MapperContext::hasConstructorArgument($context, 'Dto', 'propertyName')) { $constructarg = $source->propertyName ?? \AutoMapper\MapperContext::getConstructorArgument($context, 'Dto', 'propertyName'); } else { $constructarg = $source->propertyName ?? throw new \AutoMapper\Exception\MissingConstructorArgumentsException('Cannot create an instance ...'); } ``` After: ```php $constructarg = $source->propertyName ?? ( \AutoMapper\MapperContext::hasConstructorArgument($context, 'Dto', 'propertyName') ? \AutoMapper\MapperContext::getConstructorArgument($context, 'Dto', 'propertyName') : throw new \AutoMapper\Exception\MissingConstructorArgumentsException('Cannot create an instance ...') ); ``` ### Argument from source with array transformer Before: ```php if (\AutoMapper\MapperContext::hasConstructorArgument($context, 'Dto', 'propertyName')) { $values = []; foreach ($value['propertyName'] ?? [] as $key => $value) { $values[$key] = $value; } $constructarg = count($values) > 0 ? $values : \AutoMapper\MapperContext::getConstructorArgument($context, 'Dto', 'propertyName'); } else { $values = []; foreach ($value['propertyName'] ?? [] as $key => $value) { $values[$key] = $value; } $constructarg = count($values) > 0 ? $values : throw new \AutoMapper\Exception\MissingConstructorArgumentsException('Cannot create an instance'); } ``` After: ```php $values = []; foreach ($value['propertyName'] ?? [] as $key => $value) { $values[$key] = $value; } $constructarg = count($values) > 0 ? $values : (\AutoMapper\MapperContext::hasConstructorArgument($context, 'Dto', 'propertyName') ? \AutoMapper\MapperContext::getConstructorArgument($context, 'Dto', 'propertyName') : throw new \AutoMapper\Exception\MissingConstructorArgumentsException('Cannot create an instance') ); ``` ### Argument without source Before and After here logically the same, I just changed it so code would be consistent with arguments from source. Before: ```php if (\AutoMapper\MapperContext::hasConstructorArgument($context, 'Dto', 'propertyName')) { $constructarg = \AutoMapper\MapperContext::getConstructorArgument($context, 'Dto', 'propertyName'); } else { throw new \AutoMapper\Exception\MissingConstructorArgumentsException('Cannot create an instance'); } ``` After: ```php $constructarg = \AutoMapper\MapperContext::hasConstructorArgument($context, 'Dto', 'propertyName') ? \AutoMapper\MapperContext::getConstructorArgument($context, 'Dto', 'propertyName') : throw new \AutoMapper\Exception\MissingConstructorArgumentsException('Cannot create an instance'); ```
- Loading branch information