Skip to content

Commit 84f88d5

Browse files
mytskineFrançois Gannaz
andauthored
Yii2: declare components with functions, not arrays (#5668)
This reduces the cost of Dependency Injection in Yii2. For a request like /site/json, the calls to the method Container::getDependencies() went down from 7 to 3. This method still has the 3rd highest exclusive time (was 2nd). Co-authored-by: François Gannaz <[email protected]>
1 parent 19cece7 commit 84f88d5

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

frameworks/PHP/yii2/app/index.php

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
11
<?php
22

3-
// comment out the following two lines when deployed to production
4-
//defined('YII_DEBUG') or define('YII_DEBUG', false);
5-
//defined('YII_ENV') or define('YII_ENV', 'prod');
6-
//error_reporting(E_ALL);
7-
//ini_set('display_errors','on');
3+
// uncomment the following block to debug this app
4+
/*
5+
defined('YII_DEBUG') or define('YII_DEBUG', true);
6+
defined('YII_ENV') or define('YII_ENV', 'dev');
7+
error_reporting(E_ALL);
8+
ini_set('display_errors','on');
9+
*/
810

9-
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
11+
define('YII_ENABLE_ERROR_HANDLER', false);
12+
13+
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
1014

1115
$config = [
12-
'id' => 'basic',
16+
'id' => 'benchmark',
1317
'basePath' => __DIR__,
1418
'components' => [
15-
'db' => [
16-
'class' => 'yii\db\Connection',
17-
'dsn' => 'mysql:host=tfb-database;dbname=hello_world',
18-
'username' => 'benchmarkdbuser',
19-
'password' => 'benchmarkdbpass',
20-
'charset' => 'utf8',
21-
'attributes' => [
22-
PDO::ATTR_PERSISTENT => true,
23-
],
24-
'enableLogging' => false,
25-
'enableProfiling' => false,
26-
'enableSchemaCache' => true,
27-
'schemaCache' => 'cache',
28-
'schemaCacheDuration' => 3600,
29-
],
30-
'cache' => [
31-
'class' => 'yii\caching\FileCache',
32-
'cachePath' => '/tmp/yii2-cache',
33-
],
34-
'urlManager' => [
35-
'enablePrettyUrl' => true,
36-
],
19+
// Functions are faster than array declarations,
20+
// since they avoid the cost of Dependency Injection.
21+
'db' => function() {
22+
return new yii\db\Connection([
23+
'dsn' => 'mysql:host=tfb-database;dbname=hello_world',
24+
'username' => 'benchmarkdbuser',
25+
'password' => 'benchmarkdbpass',
26+
'charset' => 'utf8',
27+
'attributes' => [
28+
PDO::ATTR_PERSISTENT => true,
29+
],
30+
'enableLogging' => false,
31+
'enableProfiling' => false,
32+
'enableSchemaCache' => true,
33+
'schemaCache' => 'cache',
34+
'schemaCacheDuration' => 3600,
35+
]);
36+
},
37+
'cache' => function() {
38+
return new yii\caching\FileCache([
39+
'cachePath' => '/tmp/yii2-cache',
40+
'gcProbability' => 0,
41+
]);
42+
},
43+
'urlManager' => function() {
44+
return new yii\web\UrlManager([
45+
'enablePrettyUrl' => true,
46+
]);
47+
},
48+
// These components are overloaded for a small gain in performance (no DI)
49+
'request' => function() { return new yii\web\Request(); },
50+
'response' => function() { return new yii\web\Response(); },
3751
],
3852
];
3953

0 commit comments

Comments
 (0)