Skip to content

Commit

Permalink
feat: Don't use named parameters by default
Browse files Browse the repository at this point in the history
(Explanation: Named parameters add a (tiny) overhead, so we should only use them when we really need to. Additionally this gives easier interop, since parameter names may change between compilations)
  • Loading branch information
L3tum committed Nov 21, 2021
1 parent eb3029c commit 3937c27
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Compiler/Emitter/ContainerEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,30 @@ private function generateAutowiredConstructor(string $key, ServiceDefinition $se
$resolvingStack[$className] = true;

$parameters = [];
$parameterNames = [];
$skippedAnyParameter = false;

foreach ($serviceDefinition->getParameters() as $parameter) {
$getter = $parameter->getName() . ': ';
$generated = $this->createGetterFromParameter($parameter, $resolvingStack);
// We cannot inject a parameter. Therefore we cannot construct the service.
// Return null
if ($generated === false) {
return null;
} elseif ($generated === null) {
// Skip parameter if it can't be injected and isn't important
$skippedAnyParameter = true;
continue;
}
$parameters[] = $getter . $generated;
$parameters[] = $generated;
$parameterNames[] = $parameter->getName();
}

// If we skipped any parameter, then we need to explicitly set them by name
// Else we can avoid that 0,005μs and 0,2kb overhead
if ($skippedAnyParameter) {
foreach ($parameters as $key => $value) {
$parameters[$key] = $parameterNames[$key] . ': ' . $value;
}
}

$parameterString = implode(', ', $parameters);
Expand Down

0 comments on commit 3937c27

Please sign in to comment.