[wp-trac] [WordPress Trac] #37643: Convert chr() function calls in remove_accents() to string literals for better performance.
WordPress Trac
noreply at wordpress.org
Fri Aug 12 12:02:16 UTC 2016
#37643: Convert chr() function calls in remove_accents() to string literals for
better performance.
-------------------------+-----------------------------
Reporter: gitlost | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Formatting | Version: 1.5
Severity: normal | Keywords:
Focuses: performance |
-------------------------+-----------------------------
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;
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/37643>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list