Skip to content

Commit

Permalink
[Form] Precalculated the closure for deciding whether a choice is sel…
Browse files Browse the repository at this point in the history
…ected (PHP +30ms, Twig +30ms)
  • Loading branch information
webmozart committed Jul 21, 2012
1 parent 05ad1a0 commit db9cf27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 75 deletions.
14 changes: 14 additions & 0 deletions Extension/Core/Type/ChoiceType.php
Expand Up @@ -90,6 +90,20 @@ public function buildView(FormView $view, FormInterface $form, array $options)
'empty_value' => null,
));

// The decision, whether a choice is selected, is potentially done
// thousand of times during the rendering of a template. Provide a
// closure here that is optimized for the value of the form, to
// avoid making the type check inside the closure.
if ($options['multiple']) {
$view->vars['is_selected'] = function ($choice, array $values) {
return false !== array_search($choice, $values, true);
};
} else {
$view->vars['is_selected'] = function ($choice, $value) {
return $choice === $value;
};
}

// Check if the choices already contain the empty value
// Only add the empty value option if this is not the case
if (0 === count($options['choice_list']->getIndicesForValues(array('')))) {
Expand Down
75 changes: 0 additions & 75 deletions Tests/FormRendererTest.php

This file was deleted.

0 comments on commit db9cf27

Please sign in to comment.