Skip to content

Commit 9d2d8be

Browse files
committed
minor #29309 Optimize perf by replacing call_user_func with dynamic variables (ostrolucky)
This PR was merged into the 4.1 branch. Discussion ---------- Optimize perf by replacing call_user_func with dynamic variables | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | This provides similar boost as in symfony/symfony#29245, but on more places and without complexity increase. Check eg. https://github.com/fab2s/call_user_func for proof Fabpot failure unrelated Commits ------- 0c6ef01713 Optimize perf by replacing call_user_func with dynamic vars
2 parents 49987b6 + 4b14e57 commit 9d2d8be

File tree

10 files changed

+16
-16
lines changed

10 files changed

+16
-16
lines changed

CallbackTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(callable $transform, callable $reverseTransform)
4141
*/
4242
public function transform($data)
4343
{
44-
return \call_user_func($this->transform, $data);
44+
return ($this->transform)($data);
4545
}
4646

4747
/**
@@ -57,6 +57,6 @@ public function transform($data)
5757
*/
5858
public function reverseTransform($data)
5959
{
60-
return \call_user_func($this->reverseTransform, $data);
60+
return ($this->reverseTransform)($data);
6161
}
6262
}

ChoiceList/ArrayChoiceList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function getValuesForChoices(array $choices)
155155
$givenValues = array();
156156

157157
foreach ($choices as $i => $givenChoice) {
158-
$givenValues[$i] = (string) \call_user_func($this->valueCallback, $givenChoice);
158+
$givenValues[$i] = (string) ($this->valueCallback)($givenChoice);
159159
}
160160

161161
return array_intersect($givenValues, array_keys($this->choices));
@@ -202,7 +202,7 @@ protected function flatten(array $choices, $value, &$choicesByValues, &$keysByVa
202202
continue;
203203
}
204204

205-
$choiceValue = (string) \call_user_func($value, $choice);
205+
$choiceValue = (string) $value($choice);
206206
$choicesByValues[$choiceValue] = $choice;
207207
$keysByValues[$choiceValue] = $key;
208208
$structuredValues[$key] = $choiceValue;

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
118118
// $value may be an integer or a string, since it's stored in the array
119119
// keys. We want to guarantee it's a string though.
120120
$key = $keys[$value];
121-
$nextIndex = \is_int($index) ? $index++ : \call_user_func($index, $choice, $key, $value);
121+
$nextIndex = \is_int($index) ? $index++ : $index($choice, $key, $value);
122122

123123
// BC normalize label to accept a false value
124124
if (null === $label) {
@@ -127,7 +127,7 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
127127
} elseif (false !== $label) {
128128
// If "choice_label" is set to false and "expanded" is true, the value false
129129
// should be passed on to the "label" option of the checkboxes/radio buttons
130-
$dynamicLabel = \call_user_func($label, $choice, $key, $value);
130+
$dynamicLabel = $label($choice, $key, $value);
131131
$label = false === $dynamicLabel ? false : (string) $dynamicLabel;
132132
}
133133

@@ -137,11 +137,11 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
137137
$label,
138138
// The attributes may be a callable or a mapping from choice indices
139139
// to nested arrays
140-
\is_callable($attr) ? \call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())
140+
\is_callable($attr) ? $attr($choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())
141141
);
142142

143143
// $isPreferred may be null if no choices are preferred
144-
if ($isPreferred && \call_user_func($isPreferred, $choice, $key, $value)) {
144+
if ($isPreferred && $isPreferred($choice, $key, $value)) {
145145
$preferredViews[$nextIndex] = $view;
146146
} else {
147147
$otherViews[$nextIndex] = $view;
@@ -200,7 +200,7 @@ private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $key
200200

201201
private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
202202
{
203-
$groupLabel = \call_user_func($groupBy, $choice, $keys[$value], $value);
203+
$groupLabel = $groupBy($choice, $keys[$value], $value);
204204

205205
if (null === $groupLabel) {
206206
// If the callable returns null, don't group the choice

ChoiceList/Loader/CallbackChoiceLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function loadChoiceList($value = null)
4646
return $this->choiceList;
4747
}
4848

49-
return $this->choiceList = new ArrayChoiceList(\call_user_func($this->callback), $value);
49+
return $this->choiceList = new ArrayChoiceList(($this->callback)(), $value);
5050
}
5151

5252
/**

Extension/Core/EventListener/ResizeFormListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function onSubmit(FormEvent $event)
135135
/** @var FormInterface $child */
136136
foreach ($form as $name => $child) {
137137
$isNew = !isset($previousData[$name]);
138-
$isEmpty = \is_callable($this->deleteEmpty) ? \call_user_func($this->deleteEmpty, $child->getData()) : $child->isEmpty();
138+
$isEmpty = \is_callable($this->deleteEmpty) ? ($this->deleteEmpty)($child->getData()) : $child->isEmpty();
139139

140140
// $isNew can only be true if allowAdd is true, so we don't
141141
// need to check allowAdd again

Extension/HttpFoundation/HttpFoundationRequestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function handleRequest(FormInterface $form, $request = null)
7373
$form->submit(null, false);
7474

7575
$form->addError(new FormError(
76-
\call_user_func($form->getConfig()->getOption('upload_max_size_message')),
76+
$form->getConfig()->getOption('upload_max_size_message')(),
7777
null,
7878
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
7979
));

Extension/Validator/Constraints/FormValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private static function getValidationGroups(FormInterface $form)
180180
private static function resolveValidationGroups($groups, FormInterface $form)
181181
{
182182
if (!\is_string($groups) && \is_callable($groups)) {
183-
$groups = \call_user_func($groups, $form);
183+
$groups = $groups($form);
184184
}
185185

186186
if ($groups instanceof GroupSequence) {

Extension/Validator/Type/UploadValidatorExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function configureOptions(OptionsResolver $resolver)
4040
$translationDomain = $this->translationDomain;
4141
$resolver->setNormalizer('upload_max_size_message', function (Options $options, $message) use ($translator, $translationDomain) {
4242
return function () use ($translator, $translationDomain, $message) {
43-
return $translator->trans(\call_user_func($message), array(), $translationDomain);
43+
return $translator->trans($message(), array(), $translationDomain);
4444
};
4545
});
4646
}

NativeRequestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function handleRequest(FormInterface $form, $request = null)
7878
$form->submit(null, false);
7979

8080
$form->addError(new FormError(
81-
\call_user_func($form->getConfig()->getOption('upload_max_size_message')),
81+
$form->getConfig()->getOption('upload_max_size_message')(),
8282
null,
8383
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
8484
));

Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public function testPostMaxSizeTranslation()
4040
$extension->configureOptions($resolver);
4141
$options = $resolver->resolve();
4242

43-
$this->assertEquals('translated max {{ max }}!', \call_user_func($options['upload_max_size_message']));
43+
$this->assertEquals('translated max {{ max }}!', $options['upload_max_size_message']());
4444
}
4545
}

0 commit comments

Comments
 (0)