Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 83c41ff

Browse files
committed
merged branch bschussek/options-efficiency (PR #5102)
Commits ------- 57ac110 [Form] Removed unnecessary method call 27ab56d [OptionsResolver] Removed LazyOption class and improved performance a tiny bit Discussion ---------- [OptionsResolver] Removed LazyOption class and improved performance Bug fix: no Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: -
2 parents 50652a9 + 57ac110 commit 83c41ff

File tree

4 files changed

+38
-143
lines changed

4 files changed

+38
-143
lines changed

src/Symfony/Component/Form/FormRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function renderBlock(FormView $view, $blockName, array $variables = array
131131
*/
132132
public function searchAndRenderBlock(FormView $view, $blockNameSuffix, array $variables = array())
133133
{
134-
$renderOnlyOnce = in_array($blockNameSuffix, array('row', 'widget'));
134+
$renderOnlyOnce = 'row' === $blockNameSuffix || 'widget' === $blockNameSuffix;
135135

136136
if ($renderOnlyOnce && $view->isRendered()) {
137137
return '';

src/Symfony/Component/OptionsResolver/LazyOption.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/Symfony/Component/OptionsResolver/Options.php

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class Options implements \ArrayAccess, \Iterator, \Countable
2222
{
2323
/**
24-
* A list of option values and LazyOption instances.
24+
* A list of option values.
2525
* @var array
2626
*/
2727
private $options = array();
@@ -33,19 +33,13 @@ class Options implements \ArrayAccess, \Iterator, \Countable
3333
private $normalizers = array();
3434

3535
/**
36-
* A list storing the names of all options that need to be normalized.
37-
* @var array
38-
*/
39-
private $normalization = array();
40-
41-
/**
42-
* A list storing the names of all LazyOption instances as keys.
36+
* A list of closures for evaluating lazy options.
4337
* @var array
4438
*/
4539
private $lazy = array();
4640

4741
/**
48-
* A list of Boolean locks for each LazyOption.
42+
* A list containing the currently locked options.
4943
* @var array
5044
*/
5145
private $lock = array();
@@ -95,6 +89,7 @@ public function set($option, $value)
9589
// Setting is equivalent to overloading while discarding the previous
9690
// option value
9791
unset($this->options[$option]);
92+
unset($this->lazy[$option]);
9893

9994
$this->overload($option, $value);
10095
}
@@ -126,9 +121,6 @@ public function setNormalizer($option, \Closure $normalizer)
126121
}
127122

128123
$this->normalizers[$option] = $normalizer;
129-
130-
// Each option for which a normalizer exists needs to be normalized
131-
$this->normalization[$option] = true;
132124
}
133125

134126
/**
@@ -150,6 +142,7 @@ public function replace(array $options)
150142
}
151143

152144
$this->options = array();
145+
$this->lazy = array();
153146

154147
foreach ($options as $option => $value) {
155148
$this->overload($option, $value);
@@ -184,25 +177,30 @@ public function overload($option, $value)
184177
}
185178

186179
// If an option is a closure that should be evaluated lazily, store it
187-
// inside a LazyOption instance.
180+
// in the "lazy" property.
188181
if ($value instanceof \Closure) {
189182
$reflClosure = new \ReflectionFunction($value);
190183
$params = $reflClosure->getParameters();
191184

192185
if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && __CLASS__ === $class->name) {
193-
$currentValue = isset($params[1]) && isset($this->options[$option]) ? $this->options[$option] : null;
194-
$value = new LazyOption($value, $currentValue);
186+
// Initialize the option if no previous value exists
187+
if (!isset($this->options[$option])) {
188+
$this->options[$option] = null;
189+
}
195190

196-
// Store which options are lazy for more efficient resolving
197-
$this->lazy[$option] = true;
191+
// Ignore previous lazy options if the closure has no second parameter
192+
if (!isset($this->lazy[$option]) || !isset($params[1])) {
193+
$this->lazy[$option] = array();
194+
}
198195

199-
$this->options[$option] = $value;
196+
// Store closure for later evaluation
197+
$this->lazy[$option][] = $value;
200198

201199
return;
202200
}
203201
}
204202

205-
// Reset lazy flag and locks by default
203+
// Remove lazy options by default
206204
unset($this->lazy[$option]);
207205

208206
$this->options[$option] = $value;
@@ -233,7 +231,7 @@ public function get($option)
233231
$this->resolve($option);
234232
}
235233

236-
if (isset($this->normalization[$option])) {
234+
if (isset($this->normalizers[$option])) {
237235
$this->normalize($option);
238236
}
239237

@@ -269,6 +267,7 @@ public function remove($option)
269267

270268
unset($this->options[$option]);
271269
unset($this->lazy[$option]);
270+
unset($this->normalizers[$option]);
272271
}
273272

274273
/**
@@ -286,6 +285,7 @@ public function clear()
286285

287286
$this->options = array();
288287
$this->lazy = array();
288+
$this->normalizers = array();
289289
}
290290

291291
/**
@@ -301,14 +301,16 @@ public function all()
301301

302302
// Performance-wise this is slightly better than
303303
// while (null !== $option = key($this->lazy))
304-
foreach ($this->lazy as $option => $isLazy) {
304+
foreach ($this->lazy as $option => $closures) {
305+
// Double check, in case the option has already been resolved
306+
// by cascade in the previous cycles
305307
if (isset($this->lazy[$option])) {
306308
$this->resolve($option);
307309
}
308310
}
309311

310-
foreach ($this->normalization as $option => $normalize) {
311-
if (isset($this->normalization[$option])) {
312+
foreach ($this->normalizers as $option => $normalizer) {
313+
if (isset($this->normalizers[$option])) {
312314
$this->normalize($option);
313315
}
314316
}
@@ -443,6 +445,10 @@ public function count()
443445
*/
444446
private function resolve($option)
445447
{
448+
// The code duplication with normalize() exists for performance
449+
// reasons, in order to save a method call.
450+
// Remember that this method is potentially called a couple of thousand
451+
// times and needs to be as efficient as possible.
446452
if (isset($this->lock[$option])) {
447453
$conflicts = array();
448454

@@ -456,7 +462,9 @@ private function resolve($option)
456462
}
457463

458464
$this->lock[$option] = true;
459-
$this->options[$option] = $this->options[$option]->evaluate($this);
465+
foreach ($this->lazy[$option] as $closure) {
466+
$this->options[$option] = $closure($this, $this->options[$option]);
467+
}
460468
unset($this->lock[$option]);
461469

462470
// The option now isn't lazy anymore
@@ -475,6 +483,10 @@ private function resolve($option)
475483
*/
476484
private function normalize($option)
477485
{
486+
// The code duplication with resolve() exists for performance
487+
// reasons, in order to save a method call.
488+
// Remember that this method is potentially called a couple of thousand
489+
// times and needs to be as efficient as possible.
478490
if (isset($this->lock[$option])) {
479491
$conflicts = array();
480492

@@ -495,6 +507,6 @@ private function normalize($option)
495507
unset($this->lock[$option]);
496508

497509
// The option is now normalized
498-
unset($this->normalization[$option]);
510+
unset($this->normalizers[$option]);
499511
}
500512
}

src/Symfony/Component/OptionsResolver/Tests/LazyOptionTest.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)