Skip to content

Commit

Permalink
improved FormatCache::formatExists performance, added getter for a ha…
Browse files Browse the repository at this point in the history
…sh set of format names
  • Loading branch information
eceltov committed Feb 27, 2025
1 parent 311e9fa commit ec70d27
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion app/helpers/MetaFormats/FormatCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
*/
class FormatCache
{
// do not access the following three arrays directly, use the getter methods instead
// (there is no guarantee that the arrays are initialized)
private static ?array $formatNames = null;
private static ?array $formatNamesHashSet = null;
private static ?array $formatToFieldFormatsMap = null;

/**
Expand Down Expand Up @@ -40,9 +43,24 @@ public static function getFormatNames(): array
return self::$formatNames;
}

/**
* @return array Returns a hash set of all defined formats (actually a dictionary with arbitrary values).
*/
public static function getFormatNamesHashSet(): array
{
if (self::$formatNamesHashSet == null) {
$formatNames = self::getFormatNames();
self::$formatNamesHashSet = [];
foreach ($formatNames as $formatName) {
self::$formatNamesHashSet[$formatName] = true;
}
}
return self::$formatNamesHashSet;
}

public static function formatExists(string $format): bool
{
return in_array($format, self::getFormatNames());
return array_key_exists($format, self::getFormatNamesHashSet());
}

/**
Expand Down

0 comments on commit ec70d27

Please sign in to comment.