From 987f4452b52f17e98fe29bd580f62b75ccc9913d Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 16 Dec 2009 21:42:42 +0000 Subject: [PATCH] New: Optimize speed of loading lang files ith hidden option MAIN_OPTIMIZE_SPEED --- htdocs/lib/memory.lib.php | 63 +++++++++++++++++++++----------------- htdocs/main.inc.php | 1 + htdocs/translate.class.php | 21 ++++++++----- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/htdocs/lib/memory.lib.php b/htdocs/lib/memory.lib.php index ff100376367..9db2530679b 100644 --- a/htdocs/lib/memory.lib.php +++ b/htdocs/lib/memory.lib.php @@ -23,22 +23,39 @@ * \version $Id$ */ +global $shmkeys,$shmoffset; + +$shmkeys=array('main'=>1,'admin'=>2,'dict'=>3,'companies'=>4,'suppliers'=>5,'products'=>6, + 'commercial'=>7,'compta'=>8,'projects'=>9,'cashdesk'=>10,'agenda'=>11,'bills'=>12, + 'propal'=>13,'boxes'=>14,'banks'=>15,'other'=>16,'errors'=>17,'members'=>18,'ecm'=>19, + 'orders'=>20,'users'=>21,'help'=>22,'stocks'=>23,'interventions'=>24, + 'donations'=>25,'contracts'=>26); +$shmoffset=100; + /** \brief Read a memory area shared by all users, all sessions on server * \param $memoryid Memory id of shared area * \return int 0=Nothing is done, <0 if KO, >0 if OK */ -function dol_getshmop($memoryid,$size) +function dol_getshmop($memoryid) { - $shmkey = ftok($memoryid, 'D'); - print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."
\n"; - if (! function_exists("shmop_open")) return 0; + global $shmkeys,$shmoffset; + + if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_open")) return 0; + $shmkey=($shmkeys[$memoryid]+$shmoffset); + //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."
\n"; $handle=@shmop_open($shmkey,'a',0,0); if ($handle) { - $data=unserialize(shmop_read($handle,0,$size)); + $size=trim(shmop_read($handle,0,6)); + if ($size) $data=unserialize(shmop_read($handle,6,$size)); + else return -1; shmop_close($handle); } + else + { + return -2; + } return $data; } @@ -47,23 +64,27 @@ function dol_getshmop($memoryid,$size) * \param $data Data to save * \return int <0 if KO, Nb of bytes written if OK */ -function dol_setshmop($memoryid,$data,$size=0) +function dol_setshmop($memoryid,$data) { - $shmkey = ftok($memoryid, 'D'); + global $shmkeys,$shmoffset; + + //print 'dol_setshmop memoryid='.$memoryid."
\n"; + if (empty($shmkeys[$memoryid]) || ! function_exists("shmop_write")) return 0; + $shmkey=$shmkeys[$memoryid]+$shmoffset; $newdata=serialize($data); - if (! $size) $size=strlen($newdata); - print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".strlen($newdata)."bytes size=".$size."
\n"; - if (! function_exists("shmop_write")) return 0; - $handle=shmop_open($shmkey,'c',0644,$size); + $size=strlen($newdata); + //print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes
\n"; + $handle=shmop_open($shmkey,'c',0644,6+$size); if ($handle) { - $shm_bytes_written=shmop_write($handle,$newdata,0); - if ($shm_bytes_written != strlen($newdata)) + $shm_bytes_written1=shmop_write($handle,str_pad($size,6),0); + $shm_bytes_written2=shmop_write($handle,$newdata,6); + if (($shm_bytes_written1 + $shm_bytes_written2) != (6+strlen($newdata))) { print "Couldn't write the entire length of data\n"; } shmop_close($handle); - return $shm_bytes_written; + return ($shm_bytes_written1+$shm_bytes_written2); } else { @@ -72,18 +93,4 @@ function dol_setshmop($memoryid,$data,$size=0) } } - -/** - * Declare function ftok - */ -if( !function_exists('ftok') ) -{ - function ftok($filename = "", $proj = "") - { - $filename = $filename . $proj; - for ($key = array(); sizeof($key) < strlen($filename); $key[] = ord(substr($filename, sizeof($key), 1))); - return dechex(array_sum($key)); - } -} - ?> diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index 63c2487967a..54a0add0d26 100644 --- a/htdocs/main.inc.php +++ b/htdocs/main.inc.php @@ -681,6 +681,7 @@ else } + /** * \brief Show HTML header * \param head Optionnal head lines diff --git a/htdocs/translate.class.php b/htdocs/translate.class.php index 3c4ee29279a..06f2dd4bd37 100644 --- a/htdocs/translate.class.php +++ b/htdocs/translate.class.php @@ -226,15 +226,20 @@ class Translate { // Enable cache of lang file in memory (faster but need more memory) // Speed gain: 40ms - Memory overusage: 200ko (Size of session cache file) - $enablelangcacheinmemory=false; - if ($enablelangcacheinmemory) + $enablelangcacheinmemory=$conf->global->MAIN_OPTIMIZE_SPEED; + //$enablelangcacheinmemory=true; + + if ($alt == 2 && $enablelangcacheinmemory) { require_once(DOL_DOCUMENT_ROOT ."/lib/memory.lib.php"); - $tmparray=dol_getshmop('DOL_LANG_'.DOL_VERSION.'_'.$newdomain,65536); + $tmparray=dol_getshmop($newdomain); if (is_array($tmparray) && sizeof($tmparray)) { - $this->tab_translate=$tmparray; + $this->tab_translate=array_merge($this->tab_translate,$tmparray); + //print $newdomain."\n"; + //var_dump($this->tab_translate); $this->tab_loaded[$newdomain]=3; // Set this file as loaded from cache in session + $fileread=1; $found=true; } } @@ -287,12 +292,12 @@ class Translate { $fileread=1; // To save lang in session - if ($enablelangcacheinmemory && sizeof($tabtranslatedomain)) + if ($alt == 2 && $enablelangcacheinmemory && sizeof($tabtranslatedomain)) { require_once(DOL_DOCUMENT_ROOT ."/lib/memory.lib.php"); - $size=dol_setshmop('DOL_LANG_'.DOL_VERSION.'_'.$newdomain,$tabtranslatedomain,65536); + $size=dol_setshmop($newdomain,$tabtranslatedomain); } -//exit; + //exit; break; // Break loop on each root dir } } @@ -341,7 +346,7 @@ class Translate { // Clear SeparatorDecimal, SeparatorThousand if ($this->tab_translate["SeparatorDecimal"] == $this->tab_translate["SeparatorThousand"]) $this->tab_translate["SeparatorThousand"]=''; - + return 1; }