Comments (9)
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.
Can you show us the the performance numbers?
from chronos.
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)
- Empty index.php file: 9697
- With
vendor/autoload.php
, and no dependencies in composer.json: 4564
Note: I never expected that composer itself has such an impact.
- 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.
- 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.
- Then I removed the
class_alias
function calls insrc/carbon_compat.php
:
if (!\class_exists('Carbon\Carbon') && !\interface_exists('Carbon\CarbonInterface', false)) {
}
Result without class_alias
: 3764
This looks quite good now.
- 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.
Have you performed tests using composer with other dependencies (with and without chronos added)?
from chronos.
@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
: 2302cakephp/validation
: 2519symfony/console
: 1893 (also very slow)symfony/uid
: 3265fig/http-message-util
: 4256monolog/monolog
: 3764slim/slim
: 3368stripe/stripe-php
: 3168
Combinations:
cakephp/chronos
+cakephp/validation
+cakephp/database
: 1000
from chronos.
I'm not sure what the expectation here is other than composer should be as fast as not using composer.
from chronos.
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.
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.
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)
- Drop PHP 7.2? HOT 9
- Drop src/carbon_compat.php HOT 4
- Trim down the ChronosInterface's API HOT 8
- Chronos3: BC handling with native type/interface HOT 1
- ComparisonTrait::isSameDay() / ComparisonTrait::isSameMonth() HOT 2
- False Statement in 2.x Docs about Timezone Fixation HOT 2
- Hello, I would like to create a Java program for Windows 10 that has a button which, when clicked, starts a timer and that when it approaches minutes 10, 11, 12, and 13, opens a pop-up window with large text and an alarm sound. Can anyone help me with the code or point me in the right direction to get started? HOT 1
- Some deprecation warnings since 2.4.0 HOT 2
- Cannot construct a relative date that always points to the _next_ future date & time HOT 1
- [Question] Is there a way to have chronos properly validate the input against request format? HOT 4
- Missing Cake\Chronos\Date class HOT 3
- Reasoning around no longer extending native DateTimeImmutable HOT 14
- PHP Warning: Undefined array key "class" in /srv/www/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php on line 166
- Method endOfDay disregarding microseconds HOT 2
- RFC - Changing default time values for Chronos::create() HOT 1
- Method toNative() has gone missing HOT 5
- 2.4.x: b/c break in Cake\Chronos\Date::create() HOT 1
- 2.4.x: addSecond() is deprecated. Use addSeconds() HOT 3
- ChronosInterval HOT 4
- 2.4.x: diffInWeekdays() is 1 for same day HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chronos.