Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#37643 closed enhancement (fixed)

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

Reported by: gitlost's profile gitlost Owned by: wonderboymusic's profile 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 8 years ago.
chrs() -> string literals.

Download all attachments as: .zip

Change History (3)

@gitlost
8 years ago

chrs() -> string literals.

#1 @SergeyBiryukov
8 years ago

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

#2 @wonderboymusic
8 years 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.