Any way to reduce the use of htmlspecialchars () CPU?

I have a php 5.4 / mysql website with 5 million views per day, running on a linux server with nginx and php-fpm . The database is located on a separate server.

I noticed that in peak times my web server load reaches 15, instead of the usual 4 for a quad-core processor. I profiled my php application with xdebug and xhprof and saw that 90% of the CPU work is done by htmlspecialchars() in the Twig templates that I use to display the data. Sometimes from 100 to 1000 htmlspecialchars() calls per page. I tried to reduce unnecessary shielding, but this cannot be avoided.

Is there a way to reduce CPU usage by htmlspecialchars() ? Maybe there is some kind of caching in php for this? Or is there another way?

+6
source share
1 answer

Do not use Twig. Just use php files with this code:

 <?php // Load a php-file and use it as a template function template($tpl_file, $vars=array()) { $dir='/usr/local/app/view/'.$tpl_file.'.php'; if(file_exists($dir)){ // Make variables from the array easily accessible in the view extract($vars); // Start collecting output in a buffer ob_start(); require($dir); // Get the contents of the buffer $applied_template = ob_get_contents(); // Flush the buffer ob_end_clean(); return $applied_template; } } 
+1
source

All Articles