WordPress.org

Make WordPress Core

#37643 closed enhancement (fixed)

Convert chr() function calls in remove_accents() to string literals for better performance.

Reported by: gitlost Owned by: wonderboymusic
Milestone: 4.7 Priority: normal
Severity: normal Version: 1.5
Component: Formatting Keywords: has-patch
Focuses: performance Cc:

Description

Changing the chr() function calls in remove_accents() to string literals gives a "for free" performance boost (twice as fast for PHP 5 and frice for PHP 7 based on rough tests), as well as being more readable I think. The patched file was generated programmatically using:

<?php

// Replace two to four chr() concatenations in a row with actual characters.
$out = preg_replace_callback(
	'/chr\((\d+)\)\.chr\((\d+)\)(?:\.chr\((\d+)\))?(?:\.chr\((\d+)\))? /',
	function ( $m ) {
		return "'" . implode( '', array_map( 'chr', array_slice( $m, 1 ) ) ) . "' ";
	},
	file_get_contents( $argv[1] )
);
// Replace standalone chr(NNN)s with "\xNN".
$out = preg_replace_callback( '/chr\((\d{3})\)/',
	function ( $m ) {
		return sprintf( '"\x%02x"', intval( $m[1] ) );
	},
	$out
);
// Combine any resulting concatenated strings ("\xNN"."\xNN" -> "\xNN\xNN").
$out = preg_replace( '/"\."\\\\x/', '\\x', $out );

echo $out;

Attachments (1)

37643.patch (17.1 KB) - added by gitlost 13 months ago.
chrs() -> string literals.

Download all attachments as: .zip

Change History (3)

@gitlost
13 months ago

chrs() -> string literals.

#1 @SergeyBiryukov
13 months ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 4.7

#2 @wonderboymusic
13 months ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 38359:

Formatting: for a performance boost in remove_accents(), convert chr() calls to string literals.

Props gitlost.
Fixes #37643.

Note: See TracTickets for help on using tickets.