New: Optimize speed of loading lang files ith hidden option MAIN_OPTIMIZE_SPEED

This commit is contained in:
Laurent Destailleur 2009-12-16 21:42:42 +00:00
parent 9ad1cc6788
commit 987f4452b5
3 changed files with 49 additions and 36 deletions

View File

@ -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."<br>\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."<br>\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."<br>\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."<br>\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<br>\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));
}
}
?>

View File

@ -681,6 +681,7 @@ else
}
/**
* \brief Show HTML header
* \param head Optionnal head lines

View File

@ -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;
}