wiki.techinc.nl/includes/Settings/Config/ArrayConfigBuilder.php
Tim Starling 6e40c79954 Config builder optimisations (combined)
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
2022-05-06 11:28:15 +02:00

79 lines
2.1 KiB
PHP

<?php
namespace MediaWiki\Settings\Config;
use Config;
use HashConfig;
use MediaWiki\Config\IterableConfig;
use function array_key_exists;
class ArrayConfigBuilder extends ConfigBuilderBase {
/** @var array */
protected $config = [];
protected function has( string $key ): bool {
return array_key_exists( $key, $this->config );
}
protected function get( string $key ) {
return $this->config[$key] ?? null;
}
protected function update( string $key, $value ) {
$this->config[$key] = $value;
}
public function setMulti( array $values, array $mergeStrategies = [] ): ConfigBuilder {
if ( !$mergeStrategies ) {
$this->config = array_merge( $this->config, $values );
return $this;
}
foreach ( $values as $key => $newValue ) {
// Optimization: Inlined logic from set() for performance
if ( array_key_exists( $key, $this->config ) ) {
$mergeStrategy = $mergeStrategies[$key] ?? null;
if ( $mergeStrategy && is_array( $newValue ) ) {
$oldValue = $this->config[$key];
if ( $oldValue && is_array( $oldValue ) ) {
$newValue = $mergeStrategy->merge( $oldValue, $newValue );
}
}
}
$this->config[$key] = $newValue;
}
return $this;
}
/**
* Build the configuration.
*
* @todo Once we can use PHP 7.4, change the return type declaration to IterableConfig.
* @return IterableConfig
*/
public function build(): Config {
return new HashConfig( $this->config );
}
public function setMultiDefault( $defaults, $mergeStrategies ): ConfigBuilder {
foreach ( $defaults as $key => $defaultValue ) {
// Optimization: Inlined logic from setDefault() for performance
if ( array_key_exists( $key, $this->config ) ) {
$mergeStrategy = $mergeStrategies[$key] ?? null;
if ( $mergeStrategy && $defaultValue && is_array( $defaultValue ) ) {
$customValue = $this->config[$key];
if ( is_array( $customValue ) ) {
$newValue = $mergeStrategy->merge( $defaultValue, $customValue );
$this->config[$key] = $newValue;
}
}
} else {
$this->config[$key] = $defaultValue;
}
}
return $this;
}
}