From 9ad1cc67886972fbdb7e18d72c5631eeced25a5a Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Wed, 16 Dec 2009 18:57:51 +0000
Subject: [PATCH] New: First dev to try to use shared memory to optimize speed.

---
 build/exe/doliwamp/php.ini.install |  2 +-
 htdocs/comm/action/document.php    | 20 +------
 htdocs/comm/action/fiche.php       | 19 +------
 htdocs/comm/action/info.php        | 20 +------
 htdocs/lib/agenda.lib.php          | 38 +++++++++++--
 htdocs/lib/memory.lib.php          | 89 ++++++++++++++++++++++++++++++
 htdocs/translate.class.php         | 46 +++++++++------
 7 files changed, 160 insertions(+), 74 deletions(-)
 create mode 100644 htdocs/lib/memory.lib.php

diff --git a/build/exe/doliwamp/php.ini.install b/build/exe/doliwamp/php.ini.install
index dc7b8dd3428..b208c9af287 100644
--- a/build/exe/doliwamp/php.ini.install
+++ b/build/exe/doliwamp/php.ini.install
@@ -688,7 +688,7 @@ extension=php_pdo_mysql.dll
 ;extension=php_pdo_sqlite.dll
 ;extension=php_pgsql.dll
 ;extension=php_pspell.dll
-;extension=php_shmop.dll
+extension=php_shmop.dll
 ;extension=php_snmp.dll
 ;extension=php_soap.dll
 extension=php_sockets.dll
diff --git a/htdocs/comm/action/document.php b/htdocs/comm/action/document.php
index e55f82869ec..66576f8cef9 100755
--- a/htdocs/comm/action/document.php
+++ b/htdocs/comm/action/document.php
@@ -28,6 +28,7 @@
  */
 
 require_once("./pre.inc.php");
+require_once(DOL_DOCUMENT_ROOT."/lib/agenda.lib.php");
 require_once(DOL_DOCUMENT_ROOT."/contact.class.php");
 require_once(DOL_DOCUMENT_ROOT."/cactioncomm.class.php");
 require_once(DOL_DOCUMENT_ROOT."/actioncomm.class.php");
@@ -128,23 +129,8 @@ if ($objectid > 0)
 		$contact->fetch($act->contact->id);
 		$act->contact=$contact;
 
-		$h=0;
-
-		$head[$h][0] = DOL_URL_ROOT.'/comm/action/fiche.php?id='.$objectid;
-		$head[$h][1] = $langs->trans("CardAction");
-		$hselected=$h;
-		$h++;
-
-		$head[$h][0] = DOL_URL_ROOT.'/comm/action/document.php?id='.$objectid;
-		$head[$h][1] = $langs->trans('Documents');
-		$hselected=$h;
-		$h++;
-
-		$head[$h][0] = DOL_URL_ROOT.'/comm/action/info.php?id='.$objectid;
-		$head[$h][1] = $langs->trans('Info');
-		$h++;
-
-		dol_fiche_head($head, $hselected, $langs->trans("Action"),0,'task');
+		$head=actions_prepare_head();
+		dol_fiche_head($head, 'documents', $langs->trans("Action"),0,'task');
 
 		// Affichage fiche action en mode visu
 		print '<table class="border" width="100%"';
diff --git a/htdocs/comm/action/fiche.php b/htdocs/comm/action/fiche.php
index c616ffaafcb..d0ae723dcda 100644
--- a/htdocs/comm/action/fiche.php
+++ b/htdocs/comm/action/fiche.php
@@ -27,6 +27,7 @@
  */
 
 require_once("./pre.inc.php");
+require_once(DOL_DOCUMENT_ROOT."/lib/agenda.lib.php");
 require_once(DOL_DOCUMENT_ROOT."/contact.class.php");
 require_once(DOL_DOCUMENT_ROOT."/user.class.php");
 require_once(DOL_DOCUMENT_ROOT."/cactioncomm.class.php");
@@ -637,22 +638,8 @@ if ($_GET["id"])
 	 * Affichage onglets
 	 */
 
-	$h = 0;
-
-	$head[$h][0] = DOL_URL_ROOT.'/comm/action/fiche.php?id='.$_GET["id"];
-	$head[$h][1] = $langs->trans("CardAction");
-	$hselected=$h;
-	$h++;
-
-	$head[$h][0] = DOL_URL_ROOT.'/comm/action/document.php?id='.$_GET["id"];
-	$head[$h][1] = $langs->trans('Documents');
-	$h++;
-
-	$head[$h][0] = DOL_URL_ROOT.'/comm/action/info.php?id='.$_GET["id"];
-	$head[$h][1] = $langs->trans('Info');
-	$h++;
-
-	dol_fiche_head($head, $hselected, $langs->trans("Action"),0,'task');
+	$head=actions_prepare_head();
+	dol_fiche_head($head, 'card', $langs->trans("Action"),0,'task');
 
 	$now=gmmktime();
 	$delay_warning=$conf->global->MAIN_DELAY_ACTIONS_TODO*24*60*60;
diff --git a/htdocs/comm/action/info.php b/htdocs/comm/action/info.php
index 37039a39656..aff261b9bea 100644
--- a/htdocs/comm/action/info.php
+++ b/htdocs/comm/action/info.php
@@ -25,6 +25,7 @@
  */
 
 require("./pre.inc.php");
+require_once(DOL_DOCUMENT_ROOT."/lib/agenda.lib.php");
 require_once(DOL_DOCUMENT_ROOT."/lib/functions2.lib.php");
 require_once(DOL_DOCUMENT_ROOT."/contact.class.php");
 require_once(DOL_DOCUMENT_ROOT."/cactioncomm.class.php");
@@ -51,23 +52,8 @@ $act = new ActionComm($db);
 $act->fetch($_GET["id"]);
 $act->info($_GET["id"]);
 
-$h=0;
-
-$head[$h][0] = DOL_URL_ROOT.'/comm/action/fiche.php?id='.$_GET["id"];
-$head[$h][1] = $langs->trans("CardAction");
-$hselected=$h;
-$h++;
-
-$head[$h][0] = DOL_URL_ROOT.'/comm/action/document.php?id='.$_GET["id"];
-$head[$h][1] = $langs->trans('Documents');
-$h++;
-
-$head[$h][0] = DOL_URL_ROOT.'/comm/action/info.php?id='.$_GET["id"];
-$head[$h][1] = $langs->trans('Info');
-$hselected=$h;
-$h++;
-
-dol_fiche_head($head, $hselected, $langs->trans("Action"),0,'task');
+$head=actions_prepare_head();
+dol_fiche_head($head, 'info', $langs->trans("Action"),0,'task');
 
 
 print '<table width="100%"><tr><td>';
diff --git a/htdocs/lib/agenda.lib.php b/htdocs/lib/agenda.lib.php
index a5470fc7210..5b85d1cf4c0 100644
--- a/htdocs/lib/agenda.lib.php
+++ b/htdocs/lib/agenda.lib.php
@@ -1,5 +1,5 @@
 <?php
-/* Copyright (C) 2008      Laurent Destailleur  <eldy@users.sourceforge.net>
+/* Copyright (C) 2008-2009 Laurent Destailleur  <eldy@users.sourceforge.net>
  * Copyright (C) 2005-2009 Regis Houssin        <regis@dolibarr.fr>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -197,10 +197,9 @@ function show_array_last_actions_done($max=5)
 
 
 /**
-   \brief      	Define head array for tabs of agenda setup pages
-   \return		Array of head
-   \version    	$Id$
-*/
+ *  \brief     	Define head array for tabs of agenda setup pages
+ *  \return		Array of head
+ */
 function agenda_prepare_head()
 {
 	global $langs, $conf, $user;
@@ -220,4 +219,33 @@ function agenda_prepare_head()
 	return $head;
 }
 
+/**
+ *  \brief     	Define head array for tabs of agenda setup pages
+ *  \return		Array of head
+ */
+function actions_prepare_head()
+{
+	global $langs, $conf, $user;
+
+	$h = 0;
+	$head = array();
+
+	$head[$h][0] = DOL_URL_ROOT.'/comm/action/fiche.php?id='.$_GET["id"];
+	$head[$h][1] = $langs->trans("CardAction");
+	$head[$h][2] = 'card';
+	$h++;
+
+	$head[$h][0] = DOL_URL_ROOT.'/comm/action/document.php?id='.$_GET["id"];
+	$head[$h][1] = $langs->trans('Documents');
+	$head[$h][2] = 'documents';
+	$h++;
+
+	$head[$h][0] = DOL_URL_ROOT.'/comm/action/info.php?id='.$_GET["id"];
+	$head[$h][1] = $langs->trans('Info');
+	$head[$h][2] = 'info';
+	$h++;
+
+	return $head;
+}
+
 ?>
\ No newline at end of file
diff --git a/htdocs/lib/memory.lib.php b/htdocs/lib/memory.lib.php
new file mode 100644
index 00000000000..ff100376367
--- /dev/null
+++ b/htdocs/lib/memory.lib.php
@@ -0,0 +1,89 @@
+<?php
+/* Copyright (C) 2009 Laurent Destailleur  <eldy@users.sourceforge.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * or see http://www.gnu.org/
+ */
+
+/**
+ *  \file		htdocs/lib/memory.lib.php
+ *  \brief		Set of function for memory management
+ *  \version	$Id$
+ */
+
+
+/**	\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)
+{
+	$shmkey = ftok($memoryid, 'D');
+	print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."<br>\n";
+	if (! function_exists("shmop_open")) return 0;
+	$handle=@shmop_open($shmkey,'a',0,0);
+	if ($handle)
+	{
+		$data=unserialize(shmop_read($handle,0,$size));
+		shmop_close($handle);
+	}
+	return $data;
+}
+
+/**	\brief      Save data into a memory area shared by all users, all sessions on server
+ *  \param      $memoryid		Memory id of shared area
+ * 	\param		$data			Data to save
+ * 	\return		int				<0 if KO, Nb of bytes written if OK
+ */
+function dol_setshmop($memoryid,$data,$size=0)
+{
+	$shmkey = ftok($memoryid, 'D');
+	$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);
+	if ($handle)
+	{
+		$shm_bytes_written=shmop_write($handle,$newdata,0);
+		if ($shm_bytes_written != strlen($newdata)) 
+		{
+   			print "Couldn't write the entire length of data\n";
+		}
+		shmop_close($handle);
+		return $shm_bytes_written;
+	}
+	else
+	{
+		print 'Error in shmop_open';
+		return -1;
+	}
+}
+
+
+/**
+ * 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/translate.class.php b/htdocs/translate.class.php
index 6b7967f82b3..3c4ee29279a 100644
--- a/htdocs/translate.class.php
+++ b/htdocs/translate.class.php
@@ -38,8 +38,8 @@ class Translate {
 	var $defaultlang;				// Langue courante en vigueur de l'utilisateur
 	var $direction = 'ltr';			// Left to right or Right to left
 
-	var $tab_loaded=array();		// Tableau pour signaler les fichiers deja charges
 	var $tab_translate=array();		// Tableau des traductions
+	var $tab_loaded=array();		// Array to store result after loading each language file
 
 	var $cache_labels=array();		// Cache for labels
 
@@ -215,31 +215,37 @@ class Translate {
 			// Directory of translation files
 			$scandir = $searchdir."/".$langofdir;
 			$file_lang =  $scandir . "/".$newdomain.".lang";
-			$filelangexists=is_file($file_lang);
+			$file_lang_osencoded=dol_osencode($file_lang);
+			$filelangexists=is_file($file_lang_osencoded);
 
 			//dol_syslog('Translate::Load Try to read for alt='.$alt.' langofdir='.$langofdir.' file_lang='.$file_lang." => ".$filelangexists);
 
 			if ($filelangexists)
 			{
-				// Enable cache of lang file in session (faster but need more memory)
+				$found=false;
+				
+				// Enable cache of lang file in memory (faster but need more memory)
 				// Speed gain: 40ms - Memory overusage: 200ko (Size of session cache file)
-				$enablelangcacheinsession=false;
-
-				if ($enablelangcacheinsession && isset($_SESSION['lang_'.$newdomain]))
+				$enablelangcacheinmemory=false;
+				if ($enablelangcacheinmemory)
 				{
-					foreach($_SESSION['lang_'.$newdomain] as $key => $value)
+					require_once(DOL_DOCUMENT_ROOT ."/lib/memory.lib.php");
+					$tmparray=dol_getshmop('DOL_LANG_'.DOL_VERSION.'_'.$newdomain,65536);
+					if (is_array($tmparray) && sizeof($tmparray))
 					{
-						$this->tab_translate[$key]=$value;
-						$this->tab_loaded[$newdomain]=3;           // Set this file as loaded from cache in session
+						$this->tab_translate=$tmparray;
+						$this->tab_loaded[$newdomain]=3;    // Set this file as loaded from cache in session
+						$found=true;
 					}
 				}
-				else
+
+				if (! $found)
 				{
 					if ($fp = @fopen($file_lang,"rt"))
 					{
-						if ($enablelangcacheinsession) $tabtranslatedomain=array();	// To save lang in session
-						$finded = 0;
-						while (($ligne = fgets($fp,4096)) && ($finded == 0))
+						if ($enablelangcacheinmemory) $tabtranslatedomain=array();	// To save lang in session
+						
+						while ($ligne = fgets($fp,4096))	// Ex: Need 225ms for all fgets on all lang file for Third party page. Same speed than file_get_contents
 						{
 							if ($ligne[0] != "\n" && $ligne[0] != " " && $ligne[0] != "#")
 							{
@@ -272,17 +278,21 @@ class Translate {
 										//print 'XX'.$key;
 										$this->tab_translate[$key]=$value;
 
-										if ($enablelangcacheinsession) $tabtranslatedomain[$key]=$value;	// To save lang in session
+										if ($enablelangcacheinmemory) $tabtranslatedomain[$key]=$value;	// To save lang in session
 									}
 								}
 							}
 						}
-						$fileread=1;
 						fclose($fp);
-
+						$fileread=1;
+						
 						// To save lang in session
-						if ($enablelangcacheinsession && sizeof($tabtranslatedomain)) $_SESSION['lang_'.$newdomain]=$tabtranslatedomain;
-
+						if ($enablelangcacheinmemory && sizeof($tabtranslatedomain))
+						{
+							require_once(DOL_DOCUMENT_ROOT ."/lib/memory.lib.php");
+							$size=dol_setshmop('DOL_LANG_'.DOL_VERSION.'_'.$newdomain,$tabtranslatedomain,65536);
+						}
+//exit;
 						break;		// Break loop on each root dir
 					}
 				}
-- 
GitLab