|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the sj-i/php-profiler package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace PhpProfiler\Lib\PhpProcessReader; |
|
15
|
|
|
|
|
16
|
|
|
use PhpProfiler\Lib\Process\Pointer\Dereferencer; |
|
17
|
|
|
use PhpProfiler\Lib\Process\Pointer\Pointer; |
|
18
|
|
|
|
|
19
|
|
|
final class TraceCache |
|
20
|
|
|
{ |
|
21
|
|
|
/** @param array<string, array<int, \PhpProfiler\Lib\Process\Pointer\Dereferencable>> $cache */ |
|
22
|
|
|
public function __construct( |
|
23
|
|
|
private float $key = 0, |
|
24
|
|
|
private array $cache = [], |
|
25
|
|
|
) { |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function clearCache(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->cache = []; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function updateCacheKey(float $key): void |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->key !== $key) { |
|
36
|
|
|
$this->key = $key; |
|
37
|
|
|
$this->clearCache(); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getDereferencer(Dereferencer $dereferencer): Dereferencer |
|
42
|
|
|
{ |
|
43
|
|
|
return new class ($dereferencer, $this) implements Dereferencer { |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
private Dereferencer $dereferencer, |
|
46
|
|
|
private TraceCache $trace_cache, |
|
47
|
|
|
) { |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function deref(Pointer $pointer): mixed |
|
51
|
|
|
{ |
|
52
|
|
|
$item = $this->trace_cache->getCache($pointer); |
|
53
|
|
|
if (is_null($item)) { |
|
54
|
|
|
$item = $this->dereferencer->deref($pointer); |
|
55
|
|
|
$this->trace_cache->setCache( |
|
56
|
|
|
$pointer, |
|
57
|
|
|
$item, |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
return $item; |
|
61
|
|
|
} |
|
62
|
|
|
}; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @template T of \PhpProfiler\Lib\Process\Pointer\Dereferencable |
|
67
|
|
|
* @param Pointer<T> $pointer |
|
68
|
|
|
* @param T $item |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setCache(Pointer $pointer, mixed $item): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->cache[$pointer->type][$pointer->address] = $item; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @template T of \PhpProfiler\Lib\Process\Pointer\Dereferencable |
|
77
|
|
|
* @param Pointer<T> $pointer |
|
78
|
|
|
* @return T|null |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getCache(Pointer $pointer): mixed |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var T|null */ |
|
83
|
|
|
return $this->cache[$pointer->type][$pointer->address] ?? null; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|