[Vtigercrm-developers] performance and profiling

Joe Bordes joe at tsolucio.com
Thu Jul 17 15:22:27 GMT 2014


Interesting.

I applied it and all seems to be working correctly. I'll keep an eye on it.

Just to be on the safe side I left the UTF8 check mainly thinking on 
cyrilic and similar western character sets (UTF-16,....) although I 
really don't know if that is used/useful.

function to_html($string, $encode=true) {
     global $doconvert,$default_charset;
     if ($doconvert == true) {
         if($default_charset == 'UTF-8')
             $string = htmlentities($string, ENT_QUOTES, $default_charset);
         else
             $string = preg_replace(array('/</', '/>/', '/"/'), 
array('<', '>', '"'), $string);
     }
     return $string;
}

https://github.com/tsolucio/corebos/commit/93df3a2613933b6a774a2af77db5013804abb674

Thanks
Joe
TSolucio


El 17/07/14 16:14, Alan Bell escribió:
> I have been doing some profiling of vtiger using xdebug and webgrind, 
> to make it a bit quicker to load pages and navigate about
>
> the to_html function in include/utils/utils.php 
> http://trac.vtiger.com/svn/vtiger/vtigercrm/branches/6.1.0/include/utils/utils.php 
> gets called a *lot* like 1700 times to load a page and accounts for 
> 20% of the page load time.
>
> This function does a bunch of decisions around what is in $_REQUEST to 
> see whether or not it should replace < with < and so on for 
> display. All those if statements get evaluated for every call, but as 
> it is always based on the same information it always comes to the same 
> result, which is either return the unmodified string or return 
> htmlentities($string,ENT_QUOTES,$default_charset) or do a preg_replace 
> if you are not using utf-8.
> This would be a lot faster if the decisions were done once, in 
> index.php or somewhere early in the process, then set a global on 
> whether it should or should not convert html so the to_html function 
> itself is much smaller and faster.
>
> I have an example patch below that I think works OK, it possibly 
> breaks support for not using utf-8, but seriously, who doesn't use 
> utf-8 these days? It basically decides *once* whether this is a 
> convert to HTML type request or not, and if it is going to be 
> converting it flings everything through htmlentities, without checking 
> to see if it is a string first and without doing much at all, no 
> converting charset names to upper case, no preg_match etc.
>
> This reduces the to_html function from 20% of the load time to just 
> under 10% of the load time for a detail view.
>
> Alan.
>
> Index: include/utils/utils.php
> ===================================================================
> --- include/utils/utils.php    (revision 14165)
> +++ include/utils/utils.php    (working copy)
> @@ -326,17 +326,29 @@
>      $log->debug("Exiting set_default_config method ...");
>  }
>
> -$toHtml = array(
> -        '"' => '"',
> -        '<' => '<',
> -        '>' => '>',
> -        '& ' => '& ',
> -        "'" =>  ''',
> -    '' => '\r',
> -        '\r\n'=>'\n',
> +//this is an optimisation of the to_html function, here we make the 
> decision
> +//decide once if we are going to convert things to html
>
> -);
> +function decide_to_html(){
> +    global $doconvert;
> +    $action = $_REQUEST['action'];
> +    $search = $_REQUEST['search'];
> +    if($_REQUEST['module'] != 'Settings' && $_REQUEST['file'] != 
> 'ListView' && $_REQUEST['module'] != 'Portal' && $_REQUEST['module'] 
> != "Reports")// && $_REQUEST['module'] != 'Emails')
> +        $ajax_action = $_REQUEST['module'].'Ajax';
>
> +    if($action != 'CustomView' && $action != 'Export' && $action != 
> $ajax_action && $action != 'LeadConvertToEntities' && $action != 
> 'CreatePDF' && $action != 'ConvertAsFAQ' && $_REQUEST['module'] != 
> 'Dashboard' && $action != 'CreateSOPDF' && $action != 'SendPDFMail' && 
> (!isset($_REQUEST['submode'])) )
> +    {
> +        $doconvert = true;
> +    }
> +    else if($search == true)
> +    {
> +        // Fix for tickets #4647, #4648. Conversion required in case 
> of search results also.
> +        $doconvert = true;
> +    }
> +
> +}
> +decide_to_html();//call the function once when loading
> +
>  /** Function to convert the given string to html
>    * @param $string -- string:: Type string
>    * @param $ecnode -- boolean:: Type boolean
> @@ -345,47 +357,15 @@
>         */
>  function to_html($string, $encode=true)
>  {
> -    global $log,$default_charset;
> -    //$log->debug("Entering to_html(".$string.",".$encode.") method 
> ...");
> -    global $toHtml;
> -    $action = $_REQUEST['action'];
> -    $search = $_REQUEST['search'];
> -
> -    $doconvert = false;
> -
> -    // For optimization - default_charset can be either upper / lower 
> case.
> -    static $inUTF8 = NULL;
> -    if ($inUTF8 === NULL) {
> -        $inUTF8 = (strtoupper($default_charset) == 'UTF-8');
> +    global $doconvert;
> +    global $default_charset;
> +        if($doconvert){
> +        return htmlentities($string, ENT_QUOTES, 
> $default_charset);//we don't care if it is a string or not, faster not 
> to care
> +    }else{
> +        return $string;
>      }
>
> -    if($_REQUEST['module'] != 'Settings' && $_REQUEST['file'] != 
> 'ListView' && $_REQUEST['module'] != 'Portal' && $_REQUEST['module'] 
> != "Reports")// && $_REQUEST['module'] != 'Emails')
> -        $ajax_action = $_REQUEST['module'].'Ajax';
>
> -    if(is_string($string))
> -    {
> -        if($action != 'CustomView' && $action != 'Export' && $action 
> != $ajax_action && $action != 'LeadConvertToEntities' && $action != 
> 'CreatePDF' && $action != 'ConvertAsFAQ' && $_REQUEST['module'] != 
> 'Dashboard' && $action != 'CreateSOPDF' && $action != 'SendPDFMail' && 
> (!isset($_REQUEST['submode'])) )
> -        {
> -            $doconvert = true;
> -        }
> -        else if($search == true)
> -        {
> -            // Fix for tickets #4647, #4648. Conversion required in 
> case of search results also.
> -            $doconvert = true;
> -        }
> -
> -        // In vtiger5 ajax request are treated specially and the data 
> is encoded
> -        if ($doconvert == true)
> -        {
> -            if($inUTF8)
> -                $string = htmlentities($string, ENT_QUOTES, 
> $default_charset);
> -            else
> -                $string = preg_replace(array('/</', '/>/', '/"/'), 
> array('<', '>', '"'), $string);
> -        }
> -    }
> -
> -    //$log->debug("Exiting to_html method ...");
> -    return $string;
>  }
>
>  /** Function to get the tablabel for a given id
>



More information about the vtigercrm-developers mailing list