Skip to content

Huge serialize performance degradation Carbon >= 2.61.0 #2787

@zeriyoshi

Description

@zeriyoshi

Hello, Since 2.61.0, Carbon seems to be causing significant performance degradation in the serialization and unserialization process.

This can be reproduced with the following code and commands

<?php declare(strict_types=1);

use const DIRECTORY_SEPARATOR as DS;

require __DIR__ . DS . 'vendor' . DS . 'autoload.php';

const ITER = 100000;

$carbon = new \Carbon\Carbon('now', 'Asia/Tokyo');

$start_at = \hrtime(\true);
for ($i = 0; $i < ITER; $i++) {
    \unserialize(\serialize($carbon));
}
$end_at = \hrtime(\true);

$elapsed = $end_at - $start_at;

echo "runtime version: ", \PHP_VERSION . "\n";
echo "nesbot/carbon: " . \Composer\InstalledVersions::getVersion('nesbot/carbon') . "\n";
echo "elapsed: {$elapsed}ns\n";
echo "per iteration: " . ($elapsed / ITER) . "ns\n";
$ composer require nesbot/carbon:~2.61.0 --quiet && php bench.php 
runtime version: 8.1.18
nesbot/carbon: 2.61.0.0
elapsed: 512420709ns
per iteration: 5124.20709ns

$ composer require nesbot/carbon:~2.60.0 --quiet && php bench.php 
runtime version: 8.1.18
nesbot/carbon: 2.60.0.0
elapsed: 181720459ns
per iteration: 1817.20459ns

Carbon version: 2.60.0, 2.61.0 or later
PHP version: PHP 8.1.18

Thanks!

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions