Skip to content

Commit 9d98565

Browse files
authored
Feature/Optimizations (#38)
* Cache attributes in memory to optimize performance * Keep cached string keys list in memory cache to optimize performance * Keep cached invariants list in memory cache to optimize performance Co-authored-by: Dmytro Zasiadko <dmytro@2amigos.us>
1 parent 3c8058c commit 9d98565

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

src/Domain/Traits/HasAttributes.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
*/
1515
trait HasAttributes
1616
{
17+
/**
18+
* Static property to keep cached attributes list to optimize performance.
19+
* @var array<array<string, mixed>>
20+
*/
21+
private static array $_attributesCache = [];
22+
23+
/**
24+
* Static property to keep cached string keys to optimize performance
25+
* @var array<string, string>
26+
*/
27+
private static array $_stringKeysCache = [];
28+
1729
/**
1830
* Return the list of attributes of the current class.
1931
* Properties starting with "_" will be considered as internal use only.
@@ -22,10 +34,13 @@ trait HasAttributes
2234
*/
2335
final public static function attributes(): array
2436
{
25-
return array_filter(
26-
array_keys(get_class_vars(static::class)),
27-
fn(string $item) => strpos($item, '_') !== 0
28-
);
37+
if (empty(static::$_attributesCache[static::class])) {
38+
static::$_attributesCache[static::class] = array_filter(
39+
array_keys(get_class_vars(static::class)),
40+
fn(string $item) => strpos($item, '_') !== 0
41+
);
42+
}
43+
return static::$_attributesCache[static::class];
2944
}
3045

3146
/**
@@ -108,12 +123,16 @@ final protected function set(string $attribute, $value): void
108123
*/
109124
protected function getStringKey(string $id, string $prefix = '', string $suffix = ''): string
110125
{
111-
return sprintf(
112-
'%s%s%s',
113-
$prefix,
114-
implode('', map(fn(string $chunk) => ucfirst($chunk), explode('_', $id))),
115-
$suffix
116-
);
126+
$cacheKey = implode('-', [$id, $prefix, $suffix]);
127+
if (empty(static::$_stringKeysCache[$cacheKey])) {
128+
static::$_stringKeysCache[$cacheKey] = sprintf(
129+
'%s%s%s',
130+
$prefix,
131+
implode('', map(fn(string $chunk) => ucfirst($chunk), explode('_', $id))),
132+
$suffix
133+
);
134+
}
135+
return static::$_stringKeysCache[$cacheKey];
117136
}
118137

119138
/**

src/Domain/Traits/HasInvariants.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
*/
1919
trait HasInvariants
2020
{
21+
/**
22+
* Static property to keep cached invariants list to optimize performance.
23+
* @var array<string, <string, mixed>>
24+
*/
25+
private static $_invariantsCache = [];
26+
2127
/**
2228
* Invariant configuration.
2329
*
@@ -40,20 +46,25 @@ trait HasInvariants
4046
*/
4147
final public static function invariants(): array
4248
{
43-
$invariants = [];
44-
foreach (get_class_methods(static::class) as $invariant) {
45-
if (strpos($invariant, 'invariant') === 0 && $invariant !== 'invariants') {
46-
$invariants[$invariant] = str_replace(
47-
'invariant ',
48-
'',
49-
strtolower(
50-
preg_replace('/[A-Z]([A-Z](?![a-z]))*/', ' $0', $invariant)
51-
)
52-
);
49+
if (empty(static::$_invariantsCache[static::class])) {
50+
$invariants = [];
51+
52+
foreach (get_class_methods(static::class) as $invariant) {
53+
if (strpos($invariant, 'invariant') === 0 && $invariant !== 'invariants') {
54+
$invariants[$invariant] = str_replace(
55+
'invariant ',
56+
'',
57+
strtolower(
58+
preg_replace('/[A-Z]([A-Z](?![a-z]))*/', ' $0', $invariant)
59+
)
60+
);
61+
}
5362
}
63+
64+
static::$_invariantsCache[static::class] = $invariants;
5465
}
5566

56-
return $invariants;
67+
return static::$_invariantsCache[static::class];
5768
}
5869

5970
/**

0 commit comments

Comments
 (0)