10000 [Cache] Inline some hot function calls · symfony/symfony@52b4bfc · GitHub
[go: up one dir, main page]

Skip to content

Commit 52b4bfc

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

15 files changed

+86
-77
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 24 additions & 15 deletions
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,21 @@ function ($adapter, $item) use ($maxLifetime) {
7680
*/
7781
public function getItem($key)
7882
{
79-
$saveUp = $this->saveUp;
83+
$syncItem = $this->syncItem;
84+
$misses = array();
8085

8186
foreach ($this->adapters as $i => $adapter) {
8287
$item = $adapter->getItem($key);
8388

8489
if ($item->isHit()) {
8590
while (0 <= --$i) {
86-
$saveUp($this->adapters[$i], $item);
91+
$this->adapters[$i]->save($syncItem($item, $misses[$i]));
8792
}
8893

8994
return $item;
9095
}
96+
97+
$misses[$i] = $item;
9198
}
9299

93100
return $item;
@@ -104,6 +111,7 @@ public function getItems(array $keys = array())
104111
private function generateItems($items, $adapterIndex)
105112
{
106113
$missing = array();
114+
$misses = array();
107115
$nextAdapterIndex = $adapterIndex + 1;
108116
$nextAdapter = isset($this->adapters[$nextAdapterIndex]) ? $this->adapters[$nextAdapterIndex] : null;
109117

@@ -112,17 +120,18 @@ private function generateItems($items, $adapterIndex)
112120
yield $k => $item;
113121
} else {
114122
$missing[] = $k;
123+
$misses[$k] = $item;
115124
}
116125
}
117126

118127
if ($missing) {
119-
$saveUp = $this->saveUp;
128+
$syncItem = $this->syncItem;
120129
$adapter = $this->adapters[$adapterIndex];
121130
$items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);
122131

123132
foreach ($items as $k => $item) {
124133
if ($item->isHit()) {
125-
$saveUp($adapter, $item);
134+
$adapter->save($syncItem($item, $misses[$k]));
126135
}
127136

128137
yield $k => $item;

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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function expiresAfter($time)
8989
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
9090
} elseif ($time instanceof \DateInterval) {
9191
$this->expiry = (int) \DateTime::createFromFormat('U', time())->add($time)->format('U');
92-
} elseif (is_int($time)) {
92+
} elseif (\is_int($time)) {
9393
$this->expiry = $time + time();
9494
} else {
9595
throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', is_object($time) ? get_class($time) : gettype($time)));
@@ -109,17 +109,17 @@ public function expiresAfter($time)
109109
*/
110110
public function tag($tags)
111111
{
112-
if (!is_array($tags)) {
112+
if (!\is_array($tags)) {
113113
$tags = array($tags);
114114
}
115115
foreach ($tags as $tag) {
116-
if (!is_string($tag)) {
116+
if (!\is_string($tag)) {
117117
throw new InvalidArgumentException(sprintf('Cache tag must be string, "%s" given', is_object($tag) ? get_class($tag) : gettype($tag)));
118118
}
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/AbstractCache.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getMultiple($keys, $default = null)
7979
{
8080
if ($keys instanceof \Traversable) {
8181
$keys = iterator_to_array($keys, false);
82-
} elseif (!is_array($keys)) {
82+
} elseif (!\is_array($keys)) {
8383
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
8484
}
8585
$ids = array();
@@ -103,13 +103,13 @@ public function getMultiple($keys, $default = null)
103103
*/
104104
public function setMultiple($values, $ttl = null)
105105
{
106-
if (!is_array($values) && !$values instanceof \Traversable) {
106+
if (!\is_array($values) && !$values instanceof \Traversable) {
107107
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values)));
108108
}
109109
$valuesById = array();
110110

111111
foreach ($values as $key => $value) {
112-
if (is_int($key)) {
112+
if (\is_int($key)) {
113113
$key = (string) $key;
114114
}
115115
$valuesById[$this->getId($key)] = $value;
@@ -126,7 +126,7 @@ public function setMultiple($values, $ttl = null)
126126
return true;
127127
}
128128
$keys = array();
129-
foreach (is_array($e) ? $e : array_keys($valuesById) as $id) {
129+
foreach (\is_array($e) ? $e : array_keys($valuesById) as $id) {
130130
$keys[] = substr($id, strlen($this->namespace));
131131
}
132132
CacheItem::log($this->logger, 'Failed to save values', array('keys' => $keys, 'exception' => $e instanceof \Exception ? $e : null));
@@ -141,7 +141,7 @@ public function deleteMultiple($keys)
141141
{
142142
if ($keys instanceof \Traversable) {
143143
$keys = iterator_to_array($keys, false);
144-
} elseif (!is_array($keys)) {
144+
} elseif (!\is_array($keys)) {
145145
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
146146
}
147147

@@ -156,7 +156,7 @@ private function normalizeTtl($ttl)
156156
if ($ttl instanceof \DateInterval) {
157157
$ttl = (int) \DateTime::createFromFormat('U', 0)->add($ttl)->format('U');
158158
}
159-
if (is_int($ttl)) {
159+
if (\is_int($ttl)) {
160160
return 0 < $ttl ? $ttl : false;
161161
}
162162

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function getMultiple($keys, $default = null)
5757
{
5858
if ($keys instanceof \Traversable) {
5959
$keys = iterator_to_array($keys, false);
60-
} elseif (!is_array($keys)) {
60+
} elseif (!\is_array($keys)) {
6161
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
6262
}
6363
foreach ($keys as $key) {
@@ -72,7 +72,7 @@ public function getMultiple($keys, $default = null)
7272
*/
7373
public function deleteMultiple($keys)
7474
{
75-
if (!is_array($keys) && !$keys instanceof \Traversable) {
75+
if (!\is_array($keys) && !$keys instanceof \Traversable) {
7676
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys)));
7777
}
7878
foreach ($keys as $key) {
@@ -97,13 +97,13 @@ public function set($key, $value, $ttl = null)
9797
*/
9898
public function setMultiple($values, $ttl = null)
9999
{
100-
if (!is_array($values) && !$values instanceof \Traversable) {
100+
if (!\is_array($values) && !$values instanceof \Traversable) {
101101
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values)));
102102
}
103103
$valuesArray = array();
104104

105105
foreach ($values as $key => $value) {
106-
is_int($key) || CacheItem::validateKey($key);
106+
\is_int($key) || CacheItem::validateKey($key);
107107
$valuesArray[$key] = $value;
108108
}
109109
if (false === $ttl = $this->normalizeTtl($ttl)) {
@@ -139,7 +139,7 @@ private function normalizeTtl($ttl)
139139
if ($ttl instanceof \DateInterval) {
140140
$ttl = (int) \DateTime::createFromFormat('U', 0)->add($ttl)->format('U');
141141
}
142-
if (is_int($ttl)) {
142+
if (\is_int($ttl)) {
143143
return 0 < $ttl ? $ttl : false;
144144
}
145145

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(array $caches, $defaultLifetime = 0)
5858
*/
5959
public function get($key, $default = null)
6060
{
61-
$miss = null !== $default && is_object($default) ? $default : $this->miss;
61+
$miss = null !== $default && \is_object($default) ? $default : $this->miss;
6262

6363
foreach ($this->caches as $i => $cache) {
6464
$value = $cache->get($key, $miss);
@@ -80,7 +80,7 @@ public function get($key, $default = null)
8080
*/
8181
public function getMultiple($keys, $default = null)
8282
{
83-
$miss = null !== $default && is_object($default) ? $default : $this->miss;
83+
$miss = null !== $default && \is_object($default) ? $default : $this->miss;
8484

8585
return $this->generateItems($this->caches[0]->getMultiple($keys, $miss), 0, $miss, $default);
8686
}

0 commit comments

Comments
 (0)
0