This is a modified rebase of a patch by Tim, see I75f405930a7b14561389c59d147640e870146bec. Some benchmark results (from my laptop): Loading defaults from config-schema.php: - Master: 115/sec ( 8.7ms) - I75f4059: (Tim): 575/sec ( 1.7ms) - Id9dd0bf: (Daniel): 1120/sec ( 0.9ms) - This (Tim+Daniel): 1420/sec ( 0.7ms) Loading defaults and merging settings (worst case): - Master: 80/sec (12.4ms) - I75f4059: (Tim): 93/sec (10.8ms) - Id9dd0bf: (Daniel): 200/sec ( 4.9ms) - This (Tim+Daniel): 682/sec ( 1.5ms) Original commit message by Tim: * Explicitly import function array_key_exists to activate the special opcode * Batch creation of MergeStrategy objects * Batch default assignment to ArrayConfigBuilder The batches mostly help by allowing more inlining, eliminating some function calls. Reduced time for apply/finalize benchmark from 540µs to 170µs. Co-Authored-By: Tim Starling <tstarling@wikimedia.org> Change-Id: I3d4dd685eaaa4351801b3bac6ce1592eea925c5f
117 lines
2.8 KiB
PHP
117 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Settings\Config;
|
|
|
|
use MediaWiki\Settings\SettingsBuilderException;
|
|
use function array_key_exists;
|
|
|
|
class MergeStrategy {
|
|
|
|
/** @var string */
|
|
public const ARRAY_MERGE_RECURSIVE = 'array_merge_recursive';
|
|
|
|
/** @var string */
|
|
public const ARRAY_REPLACE_RECURSIVE = 'array_replace_recursive';
|
|
|
|
/** @var string */
|
|
public const ARRAY_PLUS_2D = 'array_plus_2d';
|
|
|
|
/** @var string */
|
|
public const ARRAY_PLUS = 'array_plus';
|
|
|
|
/** @var string */
|
|
public const ARRAY_MERGE = 'array_merge';
|
|
|
|
/** @var string */
|
|
public const REPLACE = 'replace';
|
|
|
|
/** @var string */
|
|
private $name;
|
|
|
|
/** @var bool */
|
|
private $reversed;
|
|
|
|
/** @var MergeStrategy[] */
|
|
private static $strategies = [];
|
|
|
|
/** @var MergeStrategy[] */
|
|
private static $reversedStrategies = [];
|
|
|
|
/**
|
|
* @param string $name
|
|
* @return static
|
|
*/
|
|
public static function newFromName( string $name ): self {
|
|
if ( !array_key_exists( $name, self::$strategies ) ) {
|
|
self::$strategies[$name] = new MergeStrategy( $name );
|
|
}
|
|
return self::$strategies[$name];
|
|
}
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param bool $reversed
|
|
*/
|
|
private function __construct( string $name, bool $reversed = false ) {
|
|
$this->name = $name;
|
|
$this->reversed = $reversed;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getName(): string {
|
|
return $this->name;
|
|
}
|
|
|
|
/**
|
|
* Merge $source into $destination.
|
|
*
|
|
* @note For all merge strategies except self::ARRAY_MERGE_RECURSIVE,
|
|
* for the values that have the same key, the value from $source will
|
|
* override the value in the $destination.
|
|
*
|
|
* @param array $destination
|
|
* @param array $source
|
|
* @return array
|
|
*/
|
|
public function merge( array $destination, array $source ): array {
|
|
if ( $this->reversed ) {
|
|
[ $destination, $source ] = [ $source, $destination ];
|
|
}
|
|
|
|
switch ( $this->name ) {
|
|
case self::REPLACE:
|
|
return $source;
|
|
case self::ARRAY_MERGE_RECURSIVE:
|
|
return array_merge_recursive( $destination, $source );
|
|
case self::ARRAY_REPLACE_RECURSIVE:
|
|
return array_replace_recursive( $destination, $source );
|
|
case self::ARRAY_PLUS_2D:
|
|
return wfArrayPlus2d( $source, $destination );
|
|
case self::ARRAY_PLUS:
|
|
return $source + $destination;
|
|
case self::ARRAY_MERGE:
|
|
return array_merge( $destination, $source );
|
|
default:
|
|
throw new SettingsBuilderException(
|
|
'Unknown merge strategy {name}',
|
|
[ 'name' => $this->name ]
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a reversed merge strategy, which will merge $destination into $source
|
|
* instead of $source into $destination.
|
|
*
|
|
* @see self::merge
|
|
* @return MergeStrategy
|
|
*/
|
|
public function reverse(): self {
|
|
if ( !array_key_exists( $this->name, self::$reversedStrategies ) ) {
|
|
self::$reversedStrategies[$this->name] = new self( $this->name, !$this->reversed );
|
|
}
|
|
return self::$reversedStrategies[$this->name];
|
|
}
|
|
}
|