Skip to content

Commit b92314e

Browse files
ENGCOM-6037: Magento Framework - Performance improvements #24905
- Merge Pull Request #24905 from ven-com/magento2:2.3-general-framework-fixes - Merged commits: 1. 69444df 2. 6fddb10 3. f4fd1bd 4. 80dcb3e 5. 047efa1 6. 15a8933
2 parents a0c3c03 + 15a8933 commit b92314e

File tree

4 files changed

+151
-24
lines changed

4 files changed

+151
-24
lines changed

lib/internal/Magento/Framework/Code/Reader/ClassReader.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55
*/
66
namespace Magento\Framework\Code\Reader;
77

8+
/**
9+
* Class ClassReader
10+
*
11+
* @package Magento\Framework\Code\Reader
12+
*/
813
class ClassReader implements ClassReaderInterface
914
{
15+
private $parentsCache = [];
16+
1017
/**
1118
* Read class constructor signature
1219
*
@@ -54,6 +61,10 @@ public function getConstructor($className)
5461
*/
5562
public function getParents($className)
5663
{
64+
if (isset($this->parentsCache[$className])) {
65+
return $this->parentsCache[$className];
66+
}
67+
5768
$parentClass = get_parent_class($className);
5869
if ($parentClass) {
5970
$result = [];
@@ -75,6 +86,9 @@ public function getParents($className)
7586
$result = [];
7687
}
7788
}
89+
90+
$this->parentsCache[$className] = $result;
91+
7892
return $result;
7993
}
8094
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Framework\Code\Test\Unit\Reader;
10+
11+
use Magento\Framework\Code\Reader\ClassReader;
12+
use PHPUnit\Framework\TestCase;
13+
14+
require_once __DIR__ . '/_files/ClassesForArgumentsReader.php';
15+
16+
class ClassReaderTest extends TestCase
17+
{
18+
19+
/**
20+
* @var ClassReader $model
21+
*/
22+
private $model;
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
protected function setUp()
28+
{
29+
$this->model = new ClassReader();
30+
}
31+
32+
/**
33+
* Get constructor test
34+
*
35+
* @param array $testData
36+
* @dataProvider getTestData
37+
* @throws \ReflectionException
38+
*/
39+
public function testGetConstructor(array $testData)
40+
{
41+
$this->assertEquals(
42+
$testData,
43+
$this->model->getConstructor('FirstClassForParentCall')
44+
);
45+
}
46+
47+
/**
48+
* Ensure that if we have cached class then returns this class
49+
*/
50+
public function testGetParents()
51+
{
52+
$model = new ClassReader();
53+
$this->assertEquals([0 => 'FirstClassForParentCall'], $model->getParents('ThirdClassForParentCall'));
54+
$reflection = new \ReflectionClass(ClassReader::class);
55+
$expectedClass = $reflection->getProperty('parentsCache');
56+
$expectedClass->setAccessible(true);
57+
$this->assertEquals(
58+
$expectedClass->getValue($model)['ThirdClassForParentCall'],
59+
$model->getParents('ThirdClassForParentCall')
60+
);
61+
}
62+
63+
/**
64+
* Data provider
65+
*
66+
* @return array
67+
*/
68+
public function getTestData()
69+
{
70+
return
71+
[
72+
[
73+
[
74+
0 => [
75+
0 => 'stdClassObject',
76+
1 => 'stdClass',
77+
2 => true,
78+
3 => null,
79+
],
80+
1 => [
81+
0 => 'runeTimeException',
82+
1 => 'ClassExtendsDefaultPhpType',
83+
2 => true,
84+
3 => null,
85+
],
86+
2 => [
87+
0 => 'arrayVariable',
88+
1 => null,
89+
2 => false,
90+
3 => [
91+
'key' => 'value',
92+
]
93+
]
94+
]
95+
]
96+
];
97+
}
98+
}

lib/internal/Magento/Framework/Interception/PluginList/PluginList.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function __construct(
138138
protected function _inheritPlugins($type)
139139
{
140140
$type = ltrim($type, '\\');
141-
if (!array_key_exists($type, $this->_inherited)) {
141+
if (!isset($this->_inherited[$type])) {
142142
$realType = $this->_omConfig->getOriginalInstanceType($type);
143143

144144
if ($realType !== $type) {
@@ -292,28 +292,7 @@ protected function _loadScopedData()
292292
$this->_loadedScopes[$scopeCode] = true;
293293
}
294294
} else {
295-
$virtualTypes = [];
296-
foreach ($this->_scopePriorityScheme as $scopeCode) {
297-
if (false == isset($this->_loadedScopes[$scopeCode])) {
298-
$data = $this->_reader->read($scopeCode) ?: [];
299-
unset($data['preferences']);
300-
if (count($data) > 0) {
301-
$this->_inherited = [];
302-
$this->_processed = [];
303-
$this->merge($data);
304-
foreach ($data as $class => $config) {
305-
if (isset($config['type'])) {
306-
$virtualTypes[] = $class;
307-
}
308-
}
309-
}
310-
$this->_loadedScopes[$scopeCode] = true;
311-
}
312-
if ($this->isCurrentScope($scopeCode)) {
313-
break;
314-
}
315-
}
316-
foreach ($virtualTypes as $class) {
295+
foreach ($this->_loadScopedVirtualTypes() as $class) {
317296
$this->_inheritPlugins($class);
318297
}
319298
foreach ($this->getClassDefinitions() as $class) {
@@ -328,6 +307,37 @@ protected function _loadScopedData()
328307
}
329308
}
330309

310+
/**
311+
* Load virtual types for current scope
312+
*
313+
* @return array
314+
*/
315+
private function _loadScopedVirtualTypes()
316+
{
317+
$virtualTypes = [];
318+
foreach ($this->_scopePriorityScheme as $scopeCode) {
319+
if (!isset($this->_loadedScopes[$scopeCode])) {
320+
$data = $this->_reader->read($scopeCode) ?: [];
321+
unset($data['preferences']);
322+
if (count($data) > 0) {
323+
$this->_inherited = [];
324+
$this->_processed = [];
325+
$this->merge($data);
326+
foreach ($data as $class => $config) {
327+
if (isset($config['type'])) {
328+
$virtualTypes[] = $class;
329+
}
330+
}
331+
}
332+
$this->_loadedScopes[$scopeCode] = true;
333+
}
334+
if ($this->isCurrentScope($scopeCode)) {
335+
break;
336+
}
337+
}
338+
return $virtualTypes;
339+
}
340+
331341
/**
332342
* Whether scope code is current scope code
333343
*

lib/internal/Magento/Framework/ObjectManager/Definition/Runtime.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
namespace Magento\Framework\ObjectManager\Definition;
99

10+
/**
11+
* Class Runtime
12+
*
13+
* @package Magento\Framework\ObjectManager\Definition
14+
*/
1015
class Runtime implements \Magento\Framework\ObjectManager\DefinitionInterface
1116
{
1217
/**
@@ -45,7 +50,7 @@ public function __construct(\Magento\Framework\Code\Reader\ClassReaderInterface
4550
*/
4651
public function getParameters($className)
4752
{
48-
if (!array_key_exists($className, $this->_definitions)) {
53+
if (!isset($this->_definitions[$className])) {
4954
$this->_definitions[$className] = $this->_reader->getConstructor($className);
5055
}
5156
return $this->_definitions[$className];

0 commit comments

Comments
 (0)