Skip to content

Commit d4b13ff

Browse files
[Cache] Inline some hot function calls
1 parent e984546 commit d4b13ff

File tree

8 files changed

+56
-48
lines changed

8 files changed

+56
-48
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public function commit()
237237
if (true === $e || array() === $e) {
238238
continue;
239239
}
240-
if (is_array($e) || 1 === count($values)) {
240+
if (\is_array($e) || 1 === \count($values)) {
241241
foreach (is_array($e) ? $e : array_keys($values) as $id) {
242242
$ok = false;
243243
$v = $values[$id];

src/Symfony/Component/Cache/Adapter/ChainAdapter.php

+24-16
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ class ChainAdapter implements AdapterInterface, PruneableInterface, ResettableIn
3030
{
3131
private $adapters = array();
3232
private $adapterCount;
33-
private $saveUp;
33+
private $syncItem;
3434

3535
/**
36-
* @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items
37-
* @param int $maxLifetime The max lifetime of items propagated from lower adapters to upper ones
36+
* @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items
37+
* @param int $defaultLifetime The default lifetime of items propagated from lower adapters to upper ones
3838
*/
39-
public function __construct(array $adapters, $maxLifetime = 0)
39+
public function __construct(array $adapters, $defaultLifetime = 0)
4040
{
4141
if (!$adapters) {
4242
throw new InvalidArgumentException('At least one adapter must be specified.');
@@ -55,16 +55,20 @@ public function __construct(array $adapters, $maxLifetime = 0)
5555
}
5656
$this->adapterCount = count($this->adapters);
5757

58-
$this->saveUp = \Closure::bind(
59-
function ($adapter, $item) use ($maxLifetime) {
60-
$origDefaultLifetime = $item->defaultLifetime;
58+
$this->syncItem = \Closure::bind(
59+
function ($sourceItem, $item) use ($defaultLifetime) {
60+
$item->value = $sourceItem->value;
61+
$item->expiry = $sourceItem->expiry;
62+
$item->isHit = $sourceItem->isHit;
6163

62-
if (0 < $maxLifetime && ($origDefaultLifetime <= 0 || $maxLifetime < $origDefaultLifetime)) {
63-
$item->defaultLifetime = $maxLifetime;
64+
if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) {
65+
$defaultLifetime = $sourceItem->defaultLifetime;
66+
}
67+
if (0 < $defaultLifetime && ($item->defaultLifetime <= 0 || $defaultLifetime < $item->defaultLifetime)) {
68+
$item->defaultLifetime = $defaultLifetime;
6469
}
6570

66-
$adapter->save($item);
67-
$item->defaultLifetime = $origDefaultLifetime;
71+
return $item;
6872
},
6973
null,
7074
CacheItem::class
@@ -76,18 +80,20 @@ function ($adapter, $item) use ($maxLifetime) {
7680
*/
7781
public function getItem($key)
7882
{
79-
$saveUp = $this->saveUp;
80-
83+
$syncItem = $this->syncItem;
84+
$misses = array();
8185
foreach ($this->adapters as $i => $adapter) {
8286
$item = $adapter->getItem($key);
8387

8488
if ($item->isHit()) {
8589
while (0 <= --$i) {
86-
$saveUp($this->adapters[$i], $item);
90+
$this->adapters[$i]->save($syncItem($item, $misses[$i]));
8791
}
8892

8993
return $item;
9094
}
95+
96+
$misses[$i] = $item;
9197
}
9298

9399
return $item;
@@ -104,6 +110,7 @@ public function getItems(array $keys = array())
104110
private function generateItems($items, $adapterIndex)
105111
{
106112
$missing = array();
113+
$misses = array();
107114
$nextAdapterIndex = $adapterIndex + 1;
108115
$nextAdapter = isset($this->adapters[$nextAdapterIndex]) ? $this->adapters[$nextAdapterIndex] : null;
109116

@@ -112,17 +119,18 @@ private function generateItems($items, $adapterIndex)
112119
yield $k => $item;
113120
} else {
114121
$missing[] = $k;
122+
$misses[$k] = $item;
115123
}
116124
}
117125

118126
if ($missing) {
119-
$saveUp = $this->saveUp;
127+
$syncItem = $this->syncItem;
120128
$adapter = $this->adapters[$adapterIndex];
121129
$items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);
122130

123131
foreach ($items as $k => $item) {
124132
if ($item->isHit()) {
125-
$saveUp($adapter, $item);
133+
$adapter->save($syncItem($item, $misses[$k]));
126134
}
127135

128136
yield $k => $item;

src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static function create($file, CacheItemPoolInterface $fallbackPool)
8484
*/
8585
public function getItem($key)
8686
{
87-
if (!is_string($key)) {
87+
if (!\is_string($key)) {
8888
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
8989
}
9090
if (null === $this->values) {
@@ -99,7 +99,7 @@ public function getItem($key)
9999

100100
if ('N;' === $value) {
101101
$value = null;
102-
} elseif (is_string($value) && isset($value[2]) && ':' === $value[1]) {
102+
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
103103
try {
104104
$e = null;
105105
$value = unserialize($value);
@@ -123,7 +123,7 @@ public function getItem($key)
123123
public function getItems(array $keys = array())
124124
{
125125
foreach ($keys as $key) {
126-
if (!is_string($key)) {
126+
if (!\is_string($key)) {
127127
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
128128
}
129129
}
@@ -139,7 +139,7 @@ public function getItems(array $keys = array())
139139
*/
140140
public function hasItem($key)
141141
{
142-
if (!is_string($key)) {
142+
if (!\is_string($key)) {
143143
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
144144
}
145145
if (null === $this->values) {
@@ -154,7 +154,7 @@ public function hasItem($key)
154154
*/
155155
public function deleteItem($key)
156156
{
157-
if (!is_string($key)) {
157+
if (!\is_string($key)) {
158158
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
159159
}
160160
if (null === $this->values) {
@@ -173,7 +173,7 @@ public function deleteItems(array $keys)
173173
$fallbackKeys = array();
174174

175175
foreach ($keys as $key) {
176-
if (!is_string($key)) {
176+
if (!\is_string($key)) {
177177
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
178178
}
179179

@@ -240,7 +240,7 @@ private function generateItems(array $keys)
240240

241241
if ('N;' === $value) {
242242
yield $key => $f($key, null, true);
243-
} elseif (is_string($value) && isset($value[2]) && ':' === $value[1]) {
243+
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
244244
try {
245245
yield $key => $f($key, unserialize($value), true);
246246
} catch (\Error $e) {

src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function (AdapterInterface $tagsAdapter, array $tags) {
107107
public function invalidateTags(array $tags)
108108
{
109109
foreach ($tags as $k => $tag) {
110-
if ('' !== $tag && is_string($tag)) {
110+
if ('' !== $tag && \is_string($tag)) {
111111
$tags[$k] = $tag.static::TAGS_PREFIX;
112112
}
113113
}
@@ -161,7 +161,7 @@ public function getItems(array $keys = array())
161161
$tagKeys = array();
162162

163163
foreach ($keys as $key) {
164-
if ('' !== $key && is_string($key)) {
164+
if ('' !== $key && \is_string($key)) {
165165
$key = static::TAGS_PREFIX.$key;
166166
$tagKeys[$key] = $key;
167167
}
@@ -202,7 +202,7 @@ public function deleteItem($key)
202202
public function deleteItems(array $keys)
203203
{
204204
foreach ($keys as $key) {
205-
if ('' !== $key && is_string($key)) {
205+
if ('' !== $key && \is_string($key)) {
206206
$keys[] = static::TAGS_PREFIX.$key;
207207
}
208208
}

src/Symfony/Component/Cache/CacheItem.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function tag($tags)
119119
if (isset($this->tags[$tag])) {
120120
continue;
121121
}
122-
if (!isset($tag[0])) {
122+
if ('' === $tag) {
123123
throw new InvalidArgumentException('Cache tag length must be greater than zero');
124124
}
125125
if (false !== strpbrk($tag, '{}()/\@:')) {
@@ -152,10 +152,10 @@ public function getPreviousTags()
152152
*/
153153
public static function validateKey($key)
154154
{
155-
if (!is_string($key)) {
155+
if (!\is_string($key)) {
156156
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
157157
}
158-
if (!isset($key[0])) {
158+
if ('' === $key) {
159159
throw new InvalidArgumentException('Cache key length must be greater than zero');
160160
}
161161
if (false !== strpbrk($key, '{}()/\@:')) {

src/Symfony/Component/Cache/Simple/PhpArrayCache.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function create($file, CacheInterface $fallbackPool)
6363
*/
6464
public function get($key, $default = null)
6565
{
66-
if (!is_string($key)) {
66+
if (!\is_string($key)) {
6767
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
6868
}
6969
if (null === $this->values) {
@@ -77,7 +77,7 @@ public function get($key, $default = null)
7777

7878
if ('N;' === $value) {
7979
$value = null;
80-
} elseif (is_string($value) && isset($value[2]) && ':' === $value[1]) {
80+
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
8181
try {
8282
$e = null;
8383
$value = unserialize($value);
@@ -99,11 +99,11 @@ public function getMultiple($keys, $default = null)
9999
{
100100
if ($keys instanceof \Traversable) {
101101
$keys = iterator_to_array($keys, false);
102-
} elseif (!is_array($keys)) {
102+
} elseif (!\is_array($keys)) {
103103
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
104104
}
105105
foreach ($keys as $key) {
106-
if (!is_string($key)) {
106+
if (!\is_string($key)) {
107107
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
108108
}
109109
}
@@ -119,7 +119,7 @@ public function getMultiple($keys, $default = null)
119119
*/
120120
public function has($key)
121121
{
122-
if (!is_string($key)) {
122+
if (!\is_string($key)) {
123123
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
124124
}
125125
if (null === $this->values) {
@@ -134,7 +134,7 @@ public function has($key)
134134
*/
135135
public function delete($key)
136136
{
137-
if (!is_string($key)) {
137+
if (!\is_string($key)) {
138138
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
139139
}
140140
if (null === $this->values) {
@@ -149,15 +149,15 @@ public function delete($key)
149149
*/
150150
public function deleteMultiple($keys)
151151
{
152-
if (!is_array($keys) && !$keys instanceof \Traversable) {
152+
if (!\is_array($keys) && !$keys instanceof \Traversable) {
153153
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
154154
}
155155

156156
$deleted = true;
157157
$fallbackKeys = array();
158158

159159
foreach ($keys as $key) {
160-
if (!is_string($key)) {
160+
if (!\is_string($key)) {
161161
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
162162
}
163163

@@ -183,7 +183,7 @@ public function deleteMultiple($keys)
183183
*/
184184
public function set($key, $value, $ttl = null)
185185
{
186-
if (!is_string($key)) {
186+
if (!\is_string($key)) {
187187
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
188188
}
189189
if (null === $this->values) {
@@ -198,15 +198,15 @@ public function set($key, $value, $ttl = null)
198198
*/
199199
public function setMultiple($values, $ttl = null)
200200
{
201-
if (!is_array($values) && !$values instanceof \Traversable) {
201+
if (!\is_array($values) && !$values instanceof \Traversable) {
202202
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values)));
203203
}
204204

205205
$saved = true;
206206
$fallbackValues = array();
207207

208208
foreach ($values as $key => $value) {
209-
if (!is_string($key) && !is_int($key)) {
209+
if (!\is_string($key) && !\is_int($key)) {
210210
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
211211
}
212212

@@ -234,7 +234,7 @@ private function generateItems(array $keys, $default)
234234

235235
if ('N;' === $value) {
236236
yield $key => null;
237-
} elseif (is_string($value) && isset($value[2]) && ':' === $value[1]) {
237+
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
238238
try {
239239
yield $key => unserialize($value);
240240
} catch (\Error $e) {

src/Symfony/Component/Cache/Traits/PhpArrayTrait.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public function warmUp(array $values)
6666
EOF;
6767

6868
foreach ($values as $key => $value) {
69-
CacheItem::validateKey(is_int($key) ? (string) $key : $key);
69+
CacheItem::validateKey(\is_int($key) ? (string) $key : $key);
7070

71-
if (null === $value || is_object($value)) {
71+
if (null === $value || \is_object($value)) {
7272
try {
7373
$value = serialize($value);
7474
} catch (\Exception $e) {
7575
throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, get_class($value)), 0, $e);
7676
}
77-
} elseif (is_array($value)) {
77+
} elseif (\is_array($value)) {
7878
try {
7979
$serialized = serialize($value);
8080
$unserialized = unserialize($serialized);
@@ -85,12 +85,12 @@ public function warmUp(array $values)
8585
if ($unserialized !== $value || (false !== strpos($serialized, ';R:') && preg_match('/;R:[1-9]/', $serialized))) {
8686
$value = $serialized;
8787
}
88-
} elseif (is_string($value)) {
88+
} elseif (\is_string($value)) {
8989
// Serialize strings if they could be confused with serialized objects or arrays
9090
if ('N;' === $value || (isset($value[2]) && ':' === $value[1])) {
9191
$value = serialize($value);
9292
}
93-
} elseif (!is_scalar($value)) {
93+
} elseif (!\is_scalar($value)) {
9494
throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, gettype($value)));
9595
}
9696

src/Symfony/Component/Cache/Traits/PhpFilesTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function doFetch(array $ids)
9696
foreach ($values as $id => $value) {
9797
if ('N;' === $value) {
9898
$values[$id] = null;
99-
} elseif (is_string($value) && isset($value[2]) && ':' === $value[1]) {
99+
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
100100
$values[$id] = parent::unserialize($value);
101101
}
102102
}

0 commit comments

Comments
 (0)