Html::openElement() micro-optimisations


Html::openElement() micro-optimisations

  • Use isset() instead of in_array()

In dropDefaults():

  • Remove strtolower(), since the performance cost of running strtolower() on all attribute names far outweighs the performance benefit of dropping default values for attributes with non-canonical capitalisation. Attribute names with non-canonical case are extremely rare in the MediaWiki ecosystem. The change is harmless in the sense that a default attribute is semantically equivalent to an omitted attribute.
  • Handle common cases first.
  • Avoid value normalization when the attribute name is unknown.
  • For class attributes, check if the value is empty using strict comparison rather than normalization.

In expandAttributes():

  • If the value is an array, explode each element and add the parts to an array rather than doing implode() then explode().
  • If the value is an array, check for spaces in the loop rather than calling array_diff().
  • Replace $quote with a literal
  • Skip the check for boolean attributes if we already know it is a space-separated attribute.
  • Inline Sanitizer::encodeAttribute

Measuring instruction count per iteration with perf stat, averaged over
10M iterations, PS1. Test case:
Html::openElement('a', [ 'class' => [ 'foo', 'bar' ] ] )

  • Baseline: 11160.7265433
  • in_array(): 10390.3837233
  • dropDefaults() changes: 9674.1248824
  • expandAttributes() misc: 9248.1947500
  • implode/explode and space check: 8318.9800417
  • Sanitizer inline: 8021.7371794

Change-Id: I7680b8939da50430316d77d42ebc1987752e99dc


tstarlingAuthored on Jul 29 2021, 11:19 AM
KrinkleCommitted on Aug 2 2021, 6:10 PM
rMWe82c5e52d50a: Merge "MWDebug:detectDeprecatedOverride - soft deprecate if no version"