yiisoft / arrays

Yii Array Helper
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
52 stars 28 forks source link

Improve performance of getValueByPath() #126

Open samdark opened 1 year ago

butschster commented 1 year ago

Here is a benchamark code

<?php

declare(strict_types=1);

namespace App\Endpoint\Console;

use Illuminate\Support\Arr;
use Spiral\Console\Command;
use DragonCode\Benchmark\Benchmark;
use Spiral\Http\Request\InputBag;
use Yiisoft\Arrays\ArrayHelper;

class BenchCommand extends Command
{
    protected const NAME = 'bench';
    protected const DESCRIPTION = '';

    private const ARR_PATH = 'foo.quux.corge.grault.garply.xyzzy.quux.corge.grault.garply.xyzzy.quux.corge.grault.garply.xyzzy.quux.corge.grault.garply.xyzzy.thud';

    protected function perform(): void
    {
        $array = $this->getArray();

        $bag = new InputBag($array);

        (new Benchmark())
            ->iterations(100000)
            ->withoutData()
            ->compare([
                'Spiral' => fn () => $bag->get(self::ARR_PATH),
                'Yii' => fn () => ArrayHelper::getValueByPath(
                    $array,
                    self::ARR_PATH,
                ),
                'Laravel' => fn () => Arr::get(
                    $array,
                    self::ARR_PATH,
                ),
            ]);
    }

    private function getArray(): array
    {
        return [
            'foo' => [
                'bar' => [
                    'baz' => 'qux',
                ],
                'quux' => [
                    'corge' => [
                        'grault' => [
                            'garply' => [
                                'waldo' => [
                                    'fred' => 'plugh',
                                ],
                                'xyzzy' => [
                                    'quux' => [
                                        'corge' => [
                                            'grault' => [
                                                'garply' => [
                                                    'waldo' => [
                                                        'fred' => 'plugh',
                                                    ],
                                                    'xyzzy' => [
                                                        'quux' => [
                                                            'corge' => [
                                                                'grault' => [
                                                                    'garply' => [
                                                                        'waldo' => [
                                                                            'fred' => 'plugh',
                                                                        ],
                                                                        'xyzzy' => [
                                                                            'quux' => [
                                                                                'corge' => [
                                                                                    'grault' => [
                                                                                        'garply' => [
                                                                                            'waldo' => [
                                                                                                'fred' => 'plugh',
                                                                                            ],
                                                                                            'xyzzy' => [
                                                                                                'thud' => 'Hello world!',
                                                                                            ],
                                                                                        ],
                                                                                    ],
                                                                                ],
                                                                            ],
                                                                        ],
                                                                    ],
                                                                ],
                                                            ],
                                                        ],
                                                    ],
                                                ],
                                            ],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            'garply' => [
                'waldo' => [
                    'fred' => 'plugh',
                ],
                'xyzzy' => [
                    'thud' => 'thud',
                ],
            ],
            'thud' => [
                'thud' => [
                    'thud' => 'thud',
                ],
            ],
        ];
    }
}

And results

image

vjik commented 1 year ago

Seems, problem here:

https://github.com/yiisoft/strings/blob/b35142c92c8108a872b0ed5d83d2c62426a056dd/src/StringHelper.php#L538

It's allow escape separate symbols. For example:

array: ['x']['a.b']['c']
path: x.a\.b.c

For boost may prepare key via explode('.', self::ARR_PATH).

samdark commented 1 year ago

Need to try this:

<?php

$key = 'x.a\.b.c';

$parts = splitIt($key);
var_dump($parts);

function splitIt(string $key, string $delimiter = '.', $escape = '\\')
{
    $prev = 0;
    $parts = [];

    for ($i = 0, $len = strlen($key); $i < $len; $i++) {
        if ($key[$i] === $delimiter && ($i === 0 || $key[$i-1] !== $escape)) {
            $parts[] = str_replace($escape, '', substr($key, $prev, $i - $prev));
            $prev = $i + 1;
        }
    }
    $parts[] = substr($key, $prev, $i - $prev);
    return $parts;
}
samdark commented 1 year ago

Also

$array = preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $string);

See https://stackoverflow.com/questions/6243778/split-string-by-delimiter-but-not-if-it-is-escaped