From fa9faabbf711ee31788f75f72231fced13ff7d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20Tu=CC=88rich?= Date: Sat, 22 May 2021 17:22:32 +0200 Subject: [PATCH] add caches to validator's DocumentUtils --- src/Validation/DocumentUtils.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Validation/DocumentUtils.php b/src/Validation/DocumentUtils.php index 35e20a6..7be5b95 100644 --- a/src/Validation/DocumentUtils.php +++ b/src/Validation/DocumentUtils.php @@ -9,6 +9,9 @@ */ class DocumentUtils { + private static $cacheNodesOfKey = []; + private static $cacheNodesOfKind = []; + /** * Returns all FragmentDefinitions * @param array $document @@ -29,6 +32,10 @@ public static function getFragmentDefinitions(array $document): array */ public static function getAllNodesOfKind(array $document, string $kind): array { + $hashKey = self::getHashKey($document, $kind); + if ((self::$cacheNodesOfKind[$hashKey] ?? null) !== null) + return self::$cacheNodesOfKind[$hashKey]; + $keys = array_keys($document); $nodes = []; @@ -42,6 +49,8 @@ public static function getAllNodesOfKind(array $document, string $kind): array } } + self::$cacheNodesOfKind[$hashKey] = $nodes; + return $nodes; } @@ -53,6 +62,10 @@ public static function getAllNodesOfKind(array $document, string $kind): array */ public static function getAllNodesOfKey(array $document, string $key): array { + $hashKey = self::getHashKey($document, $key); + if ((self::$cacheNodesOfKey[$hashKey] ?? null) !== null) + return self::$cacheNodesOfKey[$hashKey]; + $keys = array_keys($document); $nodes = []; @@ -66,7 +79,25 @@ public static function getAllNodesOfKey(array $document, string $key): array } } + self::$cacheNodesOfKey[$hashKey] = $nodes; + return $nodes; } + + /** + * Builds an unique hash-key based on a document and an identifier. + * + * @param array $document + * @param string $identifier + * @return string + */ + private static function getHashKey(array $document, string $identifier): string + { + return crc32( + serialize( + $document + ) + ) . $identifier; + } }