Skip to content

[Cache] boost perf by wrapping keys validity checks with assert() #40317

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

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
protected function __construct(string $namespace = '', int $defaultLifetime = 0)
{
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::NS_SEPARATOR;
$this->defaultLifetime = $defaultLifetime;
if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) {
throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace));
}
$this->createCacheItem = \Closure::bind(
self::$createCacheItem ?? self::$createCacheItem = \Closure::bind(
static function ($key, $value, $isHit) {
$item = new CacheItem();
$item->key = $key;
Expand All @@ -63,9 +64,8 @@ static function ($key, $value, $isHit) {
null,
CacheItem::class
);
$getId = \Closure::fromCallable([$this, 'getId']);
$this->mergeByLifetime = \Closure::bind(
static function ($deferred, $namespace, &$expiredIds) use ($getId, $defaultLifetime) {
self::$mergeByLifetime ?? self::$mergeByLifetime = \Closure::bind(
static function ($deferred, $namespace, &$expiredIds, $getId, $defaultLifetime) {
$byLifetime = [];
$now = microtime(true);
$expiredIds = [];
Expand Down Expand Up @@ -147,8 +147,7 @@ public static function createConnection(string $dsn, array $options = [])
public function commit()
{
$ok = true;
$byLifetime = $this->mergeByLifetime;
$byLifetime = $byLifetime($this->deferred, $this->namespace, $expiredIds);
$byLifetime = (self::$mergeByLifetime)($this->deferred, $this->namespace, $expiredIds, \Closure::fromCallable([$this, 'getId']), $this->defaultLifetime);
$retry = $this->deferred = [];

if ($expiredIds) {
Expand Down
12 changes: 5 additions & 7 deletions src/Symfony/Component/Cache/Adapter/AbstractTagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagA
protected function __construct(string $namespace = '', int $defaultLifetime = 0)
{
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':';
$this->defaultLifetime = $defaultLifetime;
if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) {
throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace));
}
$this->createCacheItem = \Closure::bind(
self::$createCacheItem ?? self::$createCacheItem = \Closure::bind(
static function ($key, $value, $isHit) {
$item = new CacheItem();
$item->key = $key;
Expand All @@ -68,10 +69,8 @@ static function ($key, $value, $isHit) {
null,
CacheItem::class
);
$getId = \Closure::fromCallable([$this, 'getId']);
$tagPrefix = self::TAGS_PREFIX;
$this->mergeByLifetime = \Closure::bind(
static function ($deferred, &$expiredIds) use ($getId, $tagPrefix, $defaultLifetime) {
self::$mergeByLifetime ?? self::$mergeByLifetime = \Closure::bind(
static function ($deferred, &$expiredIds, $getId, $tagPrefix, $defaultLifetime) {
$byLifetime = [];
$now = microtime(true);
$expiredIds = [];
Expand Down Expand Up @@ -175,8 +174,7 @@ protected function doDeleteYieldTags(array $ids): iterable
public function commit(): bool
{
$ok = true;
$byLifetime = $this->mergeByLifetime;
$byLifetime = $byLifetime($this->deferred, $expiredIds);
$byLifetime = (self::$mergeByLifetime)($this->deferred, $expiredIds, \Closure::fromCallable([$this, 'getId']), self::TAGS_PREFIX, $this->defaultLifetime);
$retry = $this->deferred = [];

if ($expiredIds) {
Expand Down
33 changes: 19 additions & 14 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
private $storeSerialized;
private $values = [];
private $expiries = [];
private $createCacheItem;
private $defaultLifetime;
private $maxLifetime;
private $maxItems;

private static $createCacheItem;

/**
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
*/
Expand All @@ -55,7 +56,7 @@ public function __construct(int $defaultLifetime = 0, bool $storeSerialized = tr
$this->storeSerialized = $storeSerialized;
$this->maxLifetime = $maxLifetime;
$this->maxItems = $maxItems;
$this->createCacheItem = \Closure::bind(
self::$createCacheItem ?? self::$createCacheItem = \Closure::bind(
static function ($key, $value, $isHit) {
$item = new CacheItem();
$item->key = $key;
Expand Down Expand Up @@ -111,7 +112,7 @@ public function hasItem($key)

return true;
}
CacheItem::validateKey($key);
\assert('' !== CacheItem::validateKey($key));

return isset($this->expiries[$key]) && !$this->deleteItem($key);
}
Expand All @@ -131,23 +132,18 @@ public function getItem($key)
} else {
$value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
}
$f = $this->createCacheItem;

return $f($key, $value, $isHit);
return (self::$createCacheItem)($key, $value, $isHit);
}

/**
* {@inheritdoc}
*/
public function getItems(array $keys = [])
{
foreach ($keys as $key) {
if (!\is_string($key) || !isset($this->expiries[$key])) {
CacheItem::validateKey($key);
}
}
\assert(self::validateKeys($keys));

return $this->generateItems($keys, microtime(true), $this->createCacheItem);
return $this->generateItems($keys, microtime(true), self::$createCacheItem);
}

/**
Expand All @@ -157,9 +153,7 @@ public function getItems(array $keys = [])
*/
public function deleteItem($key)
{
if (!\is_string($key) || !isset($this->expiries[$key])) {
CacheItem::validateKey($key);
}
\assert('' !== CacheItem::validateKey($key));
unset($this->values[$key], $this->expiries[$key]);

return true;
Expand Down Expand Up @@ -395,4 +389,15 @@ private function unfreeze(string $key, bool &$isHit)

return $value;
}

private function validateKeys(array $keys): bool
{
foreach ($keys as $key) {
if (!\is_string($key) || !isset($this->expiries[$key])) {
CacheItem::validateKey($key);
}
}

return true;
}
}
19 changes: 11 additions & 8 deletions src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterfa

private $adapters = [];
private $adapterCount;
private $syncItem;
private $defaultLifetime;

private static $syncItem;

/**
* @param CacheItemPoolInterface[] $adapters The ordered list of adapters used to fetch cached items
Expand All @@ -62,9 +64,10 @@ public function __construct(array $adapters, int $defaultLifetime = 0)
}
}
$this->adapterCount = \count($this->adapters);
$this->defaultLifetime = $defaultLifetime;

$this->syncItem = \Closure::bind(
static function ($sourceItem, $item, $sourceMetadata = null) use ($defaultLifetime) {
self::$syncItem ?? self::$syncItem = \Closure::bind(
static function ($sourceItem, $item, $defaultLifetime, $sourceMetadata = null) {
$sourceItem->isTaggable = false;
$sourceMetadata = $sourceMetadata ?? $sourceItem->metadata;
unset($sourceMetadata[CacheItem::METADATA_TAGS]);
Expand Down Expand Up @@ -105,7 +108,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
$value = $this->doGet($adapter, $key, $callback, $beta, $metadata);
}
if (null !== $item) {
($this->syncItem)($lastItem = $lastItem ?? $item, $item, $metadata);
(self::$syncItem)($lastItem = $lastItem ?? $item, $item, $this->defaultLifetime, $metadata);
}

return $value;
Expand All @@ -119,15 +122,15 @@ public function get(string $key, callable $callback, float $beta = null, array &
*/
public function getItem($key)
{
$syncItem = $this->syncItem;
$syncItem = self::$syncItem;
$misses = [];

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

if ($item->isHit()) {
while (0 <= --$i) {
$this->adapters[$i]->save($syncItem($item, $misses[$i]));
$this->adapters[$i]->save($syncItem($item, $misses[$i], $this->defaultLifetime));
}

return $item;
Expand Down Expand Up @@ -164,13 +167,13 @@ private function generateItems(iterable $items, int $adapterIndex)
}

if ($missing) {
$syncItem = $this->syncItem;
$syncItem = self::$syncItem;
$adapter = $this->adapters[$adapterIndex];
$items = $this->generateItems($nextAdapter->getItems($missing), $nextAdapterIndex);

foreach ($items as $k => $item) {
if ($item->isHit()) {
$adapter->save($syncItem($item, $misses[$k]));
$adapter->save($syncItem($item, $misses[$k], $this->defaultLifetime));
}

yield $k => $item;
Expand Down
16 changes: 7 additions & 9 deletions src/Symfony/Component/Cache/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
*/
class NullAdapter implements AdapterInterface, CacheInterface
{
private $createCacheItem;
private static $createCacheItem;

public function __construct()
{
$this->createCacheItem = \Closure::bind(
function ($key) {
self::$createCacheItem ?? self::$createCacheItem = \Closure::bind(
static function ($key) {
$item = new CacheItem();
$item->key = $key;
$item->isHit = false;

return $item;
},
$this,
null,
CacheItem::class
);
}
Expand All @@ -44,17 +44,15 @@ public function get(string $key, callable $callback, float $beta = null, array &
{
$save = true;

return $callback(($this->createCacheItem)($key), $save);
return $callback((self::$createCacheItem)($key), $save);
}

/**
* {@inheritdoc}
*/
public function getItem($key)
{
$f = $this->createCacheItem;

return $f($key);
return (self::$createCacheItem)($key);
}

/**
Expand Down Expand Up @@ -145,7 +143,7 @@ public function delete(string $key): bool

private function generateItems(array $keys)
{
$f = $this->createCacheItem;
$f = self::$createCacheItem;

foreach ($keys as $key) {
yield $key => $f($key);
Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class PhpArrayAdapter implements AdapterInterface, CacheInterface, PruneableInte
private $file;
private $keys;
private $values;
private $createCacheItem;

private static $createCacheItem;
private static $valuesCache = [];

/**
Expand All @@ -49,7 +49,7 @@ public function __construct(string $file, AdapterInterface $fallbackPool)
{
$this->file = $file;
$this->pool = $fallbackPool;
$this->createCacheItem = \Closure::bind(
self::$createCacheItem ?? self::$createCacheItem = \Closure::bind(
static function ($key, $value, $isHit) {
$item = new CacheItem();
$item->key = $key;
Expand Down Expand Up @@ -142,9 +142,7 @@ public function getItem($key)
}
}

$f = $this->createCacheItem;

return $f($key, $value, $isHit);
return (self::$createCacheItem)($key, $value, $isHit);
}

/**
Expand Down Expand Up @@ -407,7 +405,7 @@ private function initialize()

private function generateItems(array $keys): \Generator
{
$f = $this->createCacheItem;
$f = self::$createCacheItem;
$fallbackKeys = [];

foreach ($keys as $key) {
Expand Down
Loading