Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add Factories::get() v2 #8600

Merged
merged 5 commits into from
Mar 7, 2024
Merged

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Mar 2, 2024

Description
Supersedes #8598

  • add Factories::get() method to get the shared instance fast.
  • config() uses Factories::get()

I benchmarked #8598 and this PR on the Welcome page and there were no significant differences in the numbers.

4.5:

$ wrk -t10 -d5s http://localhost/CodeIgniter4/
Running 5s test @ http://localhost/CodeIgniter4/
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.19ms    1.48ms  23.59ms   92.93%
    Req/Sec   492.21    112.11     1.11k    60.36%
  24613 requests in 5.10s, 410.38MB read
Requests/sec:   4826.10
Transfer/sec:     80.47MB

This PR:

$ wrk -t10 -d5s http://localhost/CodeIgniter4/
Running 5s test @ http://localhost/CodeIgniter4/
  10 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.19ms    1.56ms  29.09ms   92.07%
    Req/Sec   497.06    106.24   676.00     57.00%
  24766 requests in 5.01s, 412.93MB read
Requests/sec:   4941.45
Transfer/sec:     82.39MB
diff --git a/app/Config/Services.php b/app/Config/Services.php
index df7c8ad086..f32b15b38a 100644
--- a/app/Config/Services.php
+++ b/app/Config/Services.php
@@ -2,6 +2,8 @@
 
 namespace Config;
 
+use CodeIgniter\Autoloader\FileLocator;
+use CodeIgniter\Autoloader\FileLocatorCached;
 use CodeIgniter\Config\BaseService;
 
 /**
@@ -19,14 +21,16 @@ use CodeIgniter\Config\BaseService;
  */
 class Services extends BaseService
 {
-    /*
-     * public static function example($getShared = true)
-     * {
-     *     if ($getShared) {
-     *         return static::getSharedInstance('example');
-     *     }
-     *
-     *     return new \CodeIgniter\Example();
-     * }
-     */
+    public static function locator(bool $getShared = true)
+    {
+        if ($getShared) {
+            if (! isset(static::$instances['locator'])) {
+                static::$instances['locator'] = new FileLocatorCached(new FileLocator(static::autoloader()));
+            }
+
+            return static::$mocks['locator'] ?? static::$instances['locator'];
+        }
+
+        return new FileLocator(static::autoloader());
+    }
 }
diff --git a/preload.php b/preload.php
index 63c781c220..73ae062668 100644
--- a/preload.php
+++ b/preload.php
@@ -49,7 +49,7 @@ class preload
      */
     private array $paths = [
         [
-            'include' => __DIR__ . '/vendor/codeigniter4/framework/system',
+            'include' => __DIR__ . '/system',
             'exclude' => [
                 // Not needed if you don't use them.
                 '/system/Database/OCI8/',
@@ -69,6 +69,11 @@ class preload
                 '/system/ThirdParty/',
             ],
         ],
+        [
+            'include' => __DIR__ . '/vendor',
+            'exclude' => [],
+        ],
+
     ];
 
     public function __construct()
diff --git a/public/index.php b/public/index.php
index 8cf5ce347c..eacaec8791 100644
--- a/public/index.php
+++ b/public/index.php
@@ -49,8 +49,8 @@ if (! defined('ENVIRONMENT')) {
 }
 
 // Load Config Cache
-// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache();
-// $factoriesCache->load('config');
+$factoriesCache = new CodeIgniter\Cache\FactoriesCache();
+$factoriesCache->load('config');
 // ^^^ Uncomment these lines if you want to use Config Caching.
 
 /*
@@ -79,7 +79,7 @@ $app->setContext($context);
 $app->run();
 
 // Save Config Cache
-// $factoriesCache->save('config');
+$factoriesCache->save('config');
 // ^^^ Uncomment this line if you want to use Config Caching.
 
 // Exits the application, setting the exit code for CLI-based applications
$ sudo service apache2 restart
$ sudo service apache2 status
$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS"

$ apache2 -v
Server version: Apache/2.4.52 (Ubuntu)
Server built:   2023-10-26T13:44:44

$ php -v
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

We should use $options['component'] because it takes precedence.
See the code below to create a shared instance.
@kenjis kenjis added the 4.5 label Mar 2, 2024
@kenjis kenjis marked this pull request as draft March 2, 2024 08:15
@kenjis kenjis marked this pull request as ready for review March 4, 2024 02:37
@kenjis kenjis added the refactor Pull requests that refactor code label Mar 4, 2024
$this->assertSame($config, Factories::get('config', 'App'));
}

public function testGetNonexistentInstance()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really non-existent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Thanks!
Fixed the method name.

@kenjis kenjis merged commit 48e0bea into codeigniter4:4.5 Mar 7, 2024
46 checks passed
@kenjis kenjis deleted the feat-Factories-get-v2 branch March 7, 2024 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.5 refactor Pull requests that refactor code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants