-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Improve performance of getValueByPath() #126
Labels
Comments
samdark
added
type:enhancement
Enhancement
status:ready for adoption
Feel free to implement this issue.
labels
Mar 2, 2023
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 |
Seems, problem here: It's allow escape separate symbols. For example:
For boost may prepare key via |
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;
} |
Also $array = preg_split('~\\\\.(*SKIP)(*FAIL)|\|~s', $string); See https://stackoverflow.com/questions/6243778/split-string-by-delimiter-but-not-if-it-is-escaped |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
No description provided.
The text was updated successfully, but these errors were encountered: