GithubHelp home page GithubHelp logo

Comments (9)

markstory avatar markstory commented on June 1, 2024

Interesting that the additional class_exists() checks are so expensive. Usually those kinds of checks are low cost.
I wonder if using the class_exists($class, false) form would help with performance issues?

from chronos.

othercorey avatar othercorey commented on June 1, 2024

Can you show us the the performance numbers?

from chronos.

odan avatar odan commented on June 1, 2024

Here are some numbers.

Used commands per test:

composer update --no-dev -o
ab -n 5000 -c 100 -k http://localhost/chronos-demo/index.php

System: PHP 8.0.1, XDebug is disabled

Requests per second [#/sec] (mean) - (More is better)

  1. Empty index.php file: 9697
  2. With vendor/autoload.php, and no dependencies in composer.json: 4564

Note: I never expected that composer itself has such an impact.

  1. composer.json with "cakephp/chronos": "^2.3" as single dependency: 1345

As you can see, the difference between test 2 and test 3 is quite significant.

  1. Then I tested it with class_exists($class, false):
if (!\class_exists('Carbon\Carbon', false) && !\interface_exists('Carbon\CarbonInterface', false)) {
    class_alias('Cake\Chronos\Chronos', 'Carbon\MutableDateTime');
    class_alias('Cake\Chronos\ChronosInterface', 'Carbon\CarbonInterface');
}

Result with class_exists($class, false): 1333

You see, there is no real performance difference using false here.

  1. Then I removed the class_alias function calls in src/carbon_compat.php:
if (!\class_exists('Carbon\Carbon') && !\interface_exists('Carbon\CarbonInterface', false)) {
}

Result without class_alias: 3764

This looks quite good now.

  1. Then I cleared the file src/carbon_compat.php completely.

Result with empty file (without class_exists and without class_alias): 3855 (wow!)

This means that class_alias is the most problematic function call from a performance perspective:

class_alias('Cake\Chronos\Chronos', 'Carbon\MutableDateTime');
class_alias('Cake\Chronos\ChronosInterface', 'Carbon\CarbonInterface');

from chronos.

othercorey avatar othercorey commented on June 1, 2024

Have you performed tests using composer with other dependencies (with and without chronos added)?

from chronos.

odan avatar odan commented on June 1, 2024

@othercorey Yes, I have also tested other packages. Here are some results.

Result is [requests/sec]. More is better.

  • No dependencies: 4564 (maximum)
  • cakephp/database: 2302
  • cakephp/validation: 2519
  • symfony/console: 1893 (also very slow)
  • symfony/uid: 3265
  • fig/http-message-util: 4256
  • monolog/monolog: 3764
  • slim/slim: 3368
  • stripe/stripe-php: 3168

Combinations:

  • cakephp/chronos + cakephp/validation + cakephp/database: 1000

from chronos.

othercorey avatar othercorey commented on June 1, 2024

I'm not sure what the expectation here is other than composer should be as fast as not using composer.

from chronos.

odan avatar odan commented on June 1, 2024

Sorry, I guess my actual question was not clear.

I don't need the Carbon class alias, and it turns out that class_alias in src/carbon_compat.php slows down the composer autoloader around 5 to 10 ms per http request.

My question was, how can I turn off or disable the Carbon class alias to get more performance?

I suggest a simple check for a special constant may help here:

if (
    !\defined('CHRONOS_CARBON_COMPAT_OFF') &&
    !\class_exists('Carbon\Carbon') &&
    !\interface_exists('Carbon\CarbonInterface', false)) {
    \class_alias('Cake\Chronos\Chronos', 'Carbon\MutableDateTime');
    \class_alias('Cake\Chronos\ChronosInterface', 'Carbon\CarbonInterface');
}

To disable the Carbon alias, I could then define this constant before loading the composer autoload.php file to disable this feature.

define('CHRONOS_CARBON_COMPAT_OFF', 1);

require_once __DIR__ . '/vendor/autoload.php';

from chronos.

markstory avatar markstory commented on June 1, 2024

My question was, how can I turn off or disable the Carbon class alias to get more performance?

You can't. Adding the stub class to prevent the class_alias calls is your best bet. You'll find other class_alias usage in CakePHP that can't be removed as easily though.

I suggest a simple check for a special constant may help here:

I guess that's an option. We'll likely remove the class_aliasing in a future major release. An additional constant would provide a short term solution though.

from chronos.

odan avatar odan commented on June 1, 2024

Ok, thanks for all your feedback and critical questions. I think I'll wait until this alias is completely removed in the future. Until then, I will probably be able to solve it with the class stub.

from chronos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.