Minor memory and performance improvements

This commit is contained in:
Mark Baker 2013-01-31 12:13:31 +00:00
parent 0baa487442
commit 9a085abd7d
2 changed files with 65 additions and 116 deletions

View File

@ -114,15 +114,6 @@ class PHPExcel_Calculation {
private static $_calculationCacheEnabled = true;
/**
* Calculation cache expiration time
*
* @access private
* @var float
*/
private static $_calculationCacheExpirationTime = 15;
/**
* List of operators that can be used within formulae
* The true/false value indicates whether it is a binary operator or a unary operator
@ -1841,28 +1832,6 @@ class PHPExcel_Calculation {
} // function clearCalculationCache()
/**
* Get calculation cache expiration time
*
* @return float
*/
public function getCalculationCacheExpirationTime() {
return self::$_calculationCacheExpirationTime;
} // getCalculationCacheExpirationTime()
/**
* Set calculation cache expiration time
*
* @param float $pValue
*/
public function setCalculationCacheExpirationTime($pValue = 15) {
self::$_calculationCacheExpirationTime = $pValue;
} // function setCalculationCacheExpirationTime()
/**
* Get the currently defined locale code
*
@ -2280,32 +2249,20 @@ class PHPExcel_Calculation {
}
}
// Is calculation cacheing enabled?
if ($cellID !== NULL) {
if (self::$_calculationCacheEnabled) {
if (($cellID !== NULL) && (self::$_calculationCacheEnabled)) {
// Is the value present in calculation cache?
// echo 'Testing cache value<br />';
$this->_writeDebug('Testing cache for cell ', $cellID);
if (isset(self::$_calculationCache[$wsTitle][$cellID])) {
// echo 'Value is in cache<br />';
$this->_writeDebug('Testing cache value for cell '.$cellID);
// Is cache still valid?
if ((microtime(true) - self::$_calculationCache[$wsTitle][$cellID]['time']) < self::$_calculationCacheExpirationTime) {
// echo 'Cache time is still valid<br />';
$this->_writeDebug('Retrieving value for '.$cellID.' from cache');
$this->_writeDebug('Retrieving value for ', $cellID, ' from cache');
// Return the cached result
$returnValue = self::$_calculationCache[$wsTitle][$cellID]['data'];
$returnValue = self::$_calculationCache[$wsTitle][$cellID];
// echo 'Retrieving data value of '.$returnValue.' for '.$cellID.' from cache<br />';
if (is_array($returnValue)) {
$returnValue = PHPExcel_Calculation_Functions::flattenArray($returnValue);
return array_shift($returnValue);
}
return $returnValue;
} else {
// echo 'Cache has expired<br />';
$this->_writeDebug('Cache value for '.$cellID.' has expired');
// Clear the cache if it's no longer valid
unset(self::$_calculationCache[$wsTitle][$cellID]);
}
}
}
}
@ -2333,11 +2290,8 @@ class PHPExcel_Calculation {
array_pop($this->debugLogStack);
// Save to calculation cache
if ($cellID !== NULL) {
if (self::$_calculationCacheEnabled) {
self::$_calculationCache[$wsTitle][$cellID]['time'] = microtime(true);
self::$_calculationCache[$wsTitle][$cellID]['data'] = $cellValue;
}
if (($cellID !== NULL) && (self::$_calculationCacheEnabled)) {
self::$_calculationCache[$wsTitle][$cellID] = $cellValue;
}
// Return the calculated value
@ -3032,9 +2986,9 @@ class PHPExcel_Calculation {
// Log what we're doing
if ($token == ':') {
$this->_writeDebug('Evaluating Range '.$this->_showValue($operand1Data['reference']).$token.$this->_showValue($operand2Data['reference']));
$this->_writeDebug('Evaluating Range ', $this->_showValue($operand1Data['reference']), $token, $this->_showValue($operand2Data['reference']));
} else {
$this->_writeDebug('Evaluating '.$this->_showValue($operand1).' '.$token.' '.$this->_showValue($operand2));
$this->_writeDebug('Evaluating ', $this->_showValue($operand1), ' ', $token, ' ', $this->_showValue($operand2));
}
// Process the operation in the appropriate manner
@ -3135,13 +3089,13 @@ class PHPExcel_Calculation {
$matrixResult = $matrix->concat($operand2);
$result = $matrixResult->getArray();
} catch (PHPExcel_Exception $ex) {
$this->_writeDebug('JAMA Matrix Exception: '.$ex->getMessage());
$this->_writeDebug('JAMA Matrix Exception: ', $ex->getMessage());
$result = '#VALUE!';
}
} else {
$result = '"'.str_replace('""','"',self::_unwrapResult($operand1,'"').self::_unwrapResult($operand2,'"')).'"';
}
$this->_writeDebug('Evaluation Result is '.$this->_showTypeDetails($result));
$this->_writeDebug('Evaluation Result is ', $this->_showTypeDetails($result));
$stack->push('Value',$result);
break;
case '|' : // Intersect
@ -3155,7 +3109,7 @@ class PHPExcel_Calculation {
}
}
$cellRef = PHPExcel_Cell::stringFromColumnIndex(min($oCol)).min($oRow).':'.PHPExcel_Cell::stringFromColumnIndex(max($oCol)).max($oRow);
$this->_writeDebug('Evaluation Result is '.$this->_showTypeDetails($cellIntersect));
$this->_writeDebug('Evaluation Result is ', $this->_showTypeDetails($cellIntersect));
$stack->push('Value',$cellIntersect,$cellRef);
break;
}
@ -3167,11 +3121,11 @@ class PHPExcel_Calculation {
$arg = $arg['value'];
if ($token === '~') {
// echo 'Token is a negation operator<br />';
$this->_writeDebug('Evaluating Negation of '.$this->_showValue($arg));
$this->_writeDebug('Evaluating Negation of ', $this->_showValue($arg));
$multiplier = -1;
} else {
// echo 'Token is a percentile operator<br />';
$this->_writeDebug('Evaluating Percentile of '.$this->_showValue($arg));
$this->_writeDebug('Evaluating Percentile of ', $this->_showValue($arg));
$multiplier = 0.01;
}
if (is_array($arg)) {
@ -3181,10 +3135,10 @@ class PHPExcel_Calculation {
$matrixResult = $matrix1->arrayTimesEquals($multiplier);
$result = $matrixResult->getArray();
} catch (PHPExcel_Exception $ex) {
$this->_writeDebug('JAMA Matrix Exception: '.$ex->getMessage());
$this->_writeDebug('JAMA Matrix Exception: ', $ex->getMessage());
$result = '#VALUE!';
}
$this->_writeDebug('Evaluation Result is '.$this->_showTypeDetails($result));
$this->_writeDebug('Evaluation Result is ', $this->_showTypeDetails($result));
$stack->push('Value',$result);
} else {
$this->_executeNumericBinaryOperation($cellID,$multiplier,$arg,'*','arrayTimesEquals',$stack);
@ -3208,23 +3162,23 @@ class PHPExcel_Calculation {
}
$matches[2] = trim($matches[2],"\"'");
// echo '$cellRef='.$cellRef.' in worksheet '.$matches[2].'<br />';
$this->_writeDebug('Evaluating Cell Range '.$cellRef.' in worksheet '.$matches[2]);
$this->_writeDebug('Evaluating Cell Range ', $cellRef, ' in worksheet ', $matches[2]);
if ($pCellParent !== NULL) {
$cellValue = $this->extractCellRange($cellRef, $pCellParent->getParent()->getSheetByName($matches[2]), false);
} else {
return $this->_raiseFormulaError('Unable to access Cell Reference');
}
$this->_writeDebug('Evaluation Result for cells '.$cellRef.' in worksheet '.$matches[2].' is '.$this->_showTypeDetails($cellValue));
$this->_writeDebug('Evaluation Result for cells ', $cellRef, ' in worksheet ', $matches[2], ' is ', $this->_showTypeDetails($cellValue));
// $cellRef = $matches[2].'!'.$cellRef;
} else {
// echo '$cellRef='.$cellRef.' in current worksheet<br />';
$this->_writeDebug('Evaluating Cell Range '.$cellRef.' in current worksheet');
$this->_writeDebug('Evaluating Cell Range ', $cellRef, ' in current worksheet');
if ($pCellParent !== NULL) {
$cellValue = $this->extractCellRange($cellRef, $pCellParent, false);
} else {
return $this->_raiseFormulaError('Unable to access Cell Reference');
}
$this->_writeDebug('Evaluation Result for cells '.$cellRef.' is '.$this->_showTypeDetails($cellValue));
$this->_writeDebug('Evaluation Result for cells ', $cellRef, ' is ', $this->_showTypeDetails($cellValue));
}
}
} else {
@ -3241,7 +3195,7 @@ class PHPExcel_Calculation {
return $this->_raiseFormulaError('Unable to access External Workbook');
}
// echo '$cellRef='.$cellRef.' in worksheet '.$matches[2].'<br />';
$this->_writeDebug('Evaluating Cell '.$cellRef.' in worksheet '.$matches[2]);
$this->_writeDebug('Evaluating Cell ', $cellRef, ' in worksheet ', $matches[2]);
if ($pCellParent !== NULL) {
if ($pCellParent->getParent()->getSheetByName($matches[2])->cellExists($cellRef)) {
$cellValue = $this->extractCellRange($cellRef, $pCellParent->getParent()->getSheetByName($matches[2]), false);
@ -3252,18 +3206,18 @@ class PHPExcel_Calculation {
} else {
return $this->_raiseFormulaError('Unable to access Cell Reference');
}
$this->_writeDebug('Evaluation Result for cell '.$cellRef.' in worksheet '.$matches[2].' is '.$this->_showTypeDetails($cellValue));
$this->_writeDebug('Evaluation Result for cell ', $cellRef, ' in worksheet ', $matches[2], ' is ', $this->_showTypeDetails($cellValue));
// $cellRef = $matches[2].'!'.$cellRef;
} else {
// echo '$cellRef='.$cellRef.' in current worksheet<br />';
$this->_writeDebug('Evaluating Cell '.$cellRef.' in current worksheet');
$this->_writeDebug('Evaluating Cell ', $cellRef, ' in current worksheet');
if ($pCellParent->cellExists($cellRef)) {
$cellValue = $this->extractCellRange($cellRef, $pCellParent, false);
$pCell->attach($pCellParent);
} else {
$cellValue = null;
}
$this->_writeDebug('Evaluation Result for cell '.$cellRef.' is '.$this->_showTypeDetails($cellValue));
$this->_writeDebug('Evaluation Result for cell ', $cellRef, ' is ', $this->_showTypeDetails($cellValue));
}
}
}
@ -3276,7 +3230,7 @@ class PHPExcel_Calculation {
$argCount = $stack->pop();
$argCount = $argCount['value'];
if ($functionName != 'MKMATRIX') {
$this->_writeDebug('Evaluating Function '.self::_localeFunc($functionName).'() with '.(($argCount == 0) ? 'no' : $argCount).' argument'.(($argCount == 1) ? '' : 's'));
$this->_writeDebug('Evaluating Function ', self::_localeFunc($functionName), '() with ', (($argCount == 0) ? 'no' : $argCount), ' argument', (($argCount == 1) ? '' : 's'));
}
if ((isset(self::$_PHPExcelFunctions[$functionName])) || (isset(self::$_controlFunctions[$functionName]))) { // function
if (isset(self::$_PHPExcelFunctions[$functionName])) {
@ -3321,28 +3275,28 @@ class PHPExcel_Calculation {
if ($functionName != 'MKMATRIX') {
if ($this->writeDebugLog) {
krsort($argArrayVals);
$this->_writeDebug('Evaluating '. self::_localeFunc($functionName).'( '.implode(self::$_localeArgumentSeparator.' ',PHPExcel_Calculation_Functions::flattenArray($argArrayVals)).' )');
$this->_writeDebug('Evaluating ', self::_localeFunc($functionName), '( '.implode(self::$_localeArgumentSeparator.' ',PHPExcel_Calculation_Functions::flattenArray($argArrayVals)).' )');
}
}
// Process each argument in turn, building the return value as an array
// if (($argCount == 1) && (is_array($args[1])) && ($functionName != 'MKMATRIX')) {
// $operand1 = $args[1];
// $this->_writeDebug('Argument is a matrix: '.$this->_showValue($operand1));
// $this->_writeDebug('Argument is a matrix: ', $this->_showValue($operand1));
// $result = array();
// $row = 0;
// foreach($operand1 as $args) {
// if (is_array($args)) {
// foreach($args as $arg) {
// $this->_writeDebug('Evaluating '.self::_localeFunc($functionName).'( '.$this->_showValue($arg).' )');
// $this->_writeDebug('Evaluating ', self::_localeFunc($functionName), '( '.$this->_showValue($arg), ' )');
// $r = call_user_func_array($functionCall,$arg);
// $this->_writeDebug('Evaluation Result for '.self::_localeFunc($functionName).'() function call is '.$this->_showTypeDetails($r));
// $this->_writeDebug('Evaluation Result for ', self::_localeFunc($functionName), '() function call is ', $this->_showTypeDetails($r));
// $result[$row][] = $r;
// }
// ++$row;
// } else {
// $this->_writeDebug('Evaluating '.self::_localeFunc($functionName).'( '.$this->_showValue($args).' )');
// $this->_writeDebug('Evaluating ', self::_localeFunc($functionName), '( '.$this->_showValue($args), ' )');
// $r = call_user_func_array($functionCall,$args);
// $this->_writeDebug('Evaluation Result for '.self::_localeFunc($functionName).'() function call is '.$this->_showTypeDetails($r));
// $this->_writeDebug('Evaluation Result for ', self::_localeFunc($functionName), '() function call is ', $this->_showTypeDetails($r));
// $result[] = $r;
// }
// }
@ -3362,7 +3316,7 @@ class PHPExcel_Calculation {
}
// }
if ($functionName != 'MKMATRIX') {
$this->_writeDebug('Evaluation Result for '.self::_localeFunc($functionName).'() function call is '.$this->_showTypeDetails($result));
$this->_writeDebug('Evaluation Result for ', self::_localeFunc($functionName), '() function call is ', $this->_showTypeDetails($result));
}
$stack->push('Value',self::_wrapResult($result));
}
@ -3373,7 +3327,7 @@ class PHPExcel_Calculation {
$excelConstant = strtoupper($token);
// echo 'Token is a PHPExcel constant: '.$excelConstant.'<br />';
$stack->push('Constant Value',self::$_ExcelConstants[$excelConstant]);
$this->_writeDebug('Evaluating Constant '.$excelConstant.' as '.$this->_showTypeDetails(self::$_ExcelConstants[$excelConstant]));
$this->_writeDebug('Evaluating Constant ', $excelConstant, ' as ', $this->_showTypeDetails(self::$_ExcelConstants[$excelConstant]));
} elseif ((is_numeric($token)) || ($token === NULL) || (is_bool($token)) || ($token == '') || ($token{0} == '"') || ($token{0} == '#')) {
// echo 'Token is a number, boolean, string, null or an Excel error<br />';
$stack->push('Value',$token);
@ -3382,10 +3336,10 @@ class PHPExcel_Calculation {
// echo 'Token is a named range<br />';
$namedRange = $matches[6];
// echo 'Named Range is '.$namedRange.'<br />';
$this->_writeDebug('Evaluating Named Range '.$namedRange);
$this->_writeDebug('Evaluating Named Range ', $namedRange);
$cellValue = $this->extractNamedRange($namedRange, ((null !== $pCell) ? $pCellParent : null), false);
$pCell->attach($pCellParent);
$this->_writeDebug('Evaluation Result for named range '.$namedRange.' is '.$this->_showTypeDetails($cellValue));
$this->_writeDebug('Evaluation Result for named range ', $namedRange, ' is ', $this->_showTypeDetails($cellValue));
$stack->push('Named Range',$cellValue,$namedRange);
} else {
return $this->_raiseFormulaError("undefined variable '$token'");
@ -3415,12 +3369,12 @@ class PHPExcel_Calculation {
// If not a numeric, test to see if the value is an Excel error, and so can't be used in normal binary operations
if ($operand > '' && $operand{0} == '#') {
$stack->push('Value', $operand);
$this->_writeDebug('Evaluation Result is '.$this->_showTypeDetails($operand));
$this->_writeDebug('Evaluation Result is ', $this->_showTypeDetails($operand));
return false;
} elseif (!PHPExcel_Shared_String::convertToNumberIfFraction($operand)) {
// If not a numeric or a fraction, then it's a text string, and so can't be used in mathematical binary operations
$stack->push('Value', '#VALUE!');
$this->_writeDebug('Evaluation Result is a '.$this->_showTypeDetails('#VALUE!'));
$this->_writeDebug('Evaluation Result is a ', $this->_showTypeDetails('#VALUE!'));
return false;
}
}
@ -3437,14 +3391,14 @@ class PHPExcel_Calculation {
$result = array();
if ((is_array($operand1)) && (!is_array($operand2))) {
foreach($operand1 as $x => $operandData) {
$this->_writeDebug('Evaluating Comparison '.$this->_showValue($operandData).' '.$operation.' '.$this->_showValue($operand2));
$this->_writeDebug('Evaluating Comparison ', $this->_showValue($operandData), ' ', $operation, ' ', $this->_showValue($operand2));
$this->_executeBinaryComparisonOperation($cellID,$operandData,$operand2,$operation,$stack);
$r = $stack->pop();
$result[$x] = $r['value'];
}
} elseif ((!is_array($operand1)) && (is_array($operand2))) {
foreach($operand2 as $x => $operandData) {
$this->_writeDebug('Evaluating Comparison '.$this->_showValue($operand1).' '.$operation.' '.$this->_showValue($operandData));
$this->_writeDebug('Evaluating Comparison ', $this->_showValue($operand1), ' ', $operation, ' ', $this->_showValue($operandData));
$this->_executeBinaryComparisonOperation($cellID,$operand1,$operandData,$operation,$stack);
$r = $stack->pop();
$result[$x] = $r['value'];
@ -3452,14 +3406,14 @@ class PHPExcel_Calculation {
} else {
if (!$recursingArrays) { self::_checkMatrixOperands($operand1,$operand2,2); }
foreach($operand1 as $x => $operandData) {
$this->_writeDebug('Evaluating Comparison '.$this->_showValue($operandData).' '.$operation.' '.$this->_showValue($operand2[$x]));
$this->_writeDebug('Evaluating Comparison ', $this->_showValue($operandData), ' ', $operation, ' ', $this->_showValue($operand2[$x]));
$this->_executeBinaryComparisonOperation($cellID,$operandData,$operand2[$x],$operation,$stack,true);
$r = $stack->pop();
$result[$x] = $r['value'];
}
}
// Log the result details
$this->_writeDebug('Comparison Evaluation Result is '.$this->_showTypeDetails($result));
$this->_writeDebug('Comparison Evaluation Result is ', $this->_showTypeDetails($result));
// And push the result onto the stack
$stack->push('Array',$result);
return true;
@ -3498,7 +3452,7 @@ class PHPExcel_Calculation {
}
// Log the result details
$this->_writeDebug('Evaluation Result is '.$this->_showTypeDetails($result));
$this->_writeDebug('Evaluation Result is ', $this->_showTypeDetails($result));
// And push the result onto the stack
$stack->push('Value',$result);
return true;
@ -3536,7 +3490,7 @@ class PHPExcel_Calculation {
$matrixResult = $matrix->$matrixFunction($operand2);
$result = $matrixResult->getArray();
} catch (PHPExcel_Exception $ex) {
$this->_writeDebug('JAMA Matrix Exception: '.$ex->getMessage());
$this->_writeDebug('JAMA Matrix Exception: ', $ex->getMessage());
$result = '#VALUE!';
}
} else {
@ -3563,7 +3517,7 @@ class PHPExcel_Calculation {
if ($operand2 == 0) {
// Trap for Divide by Zero error
$stack->push('Value','#DIV/0!');
$this->_writeDebug('Evaluation Result is '.$this->_showTypeDetails('#DIV/0!'));
$this->_writeDebug('Evaluation Result is ', $this->_showTypeDetails('#DIV/0!'));
return false;
} else {
$result = $operand1/$operand2;
@ -3578,20 +3532,21 @@ class PHPExcel_Calculation {
}
// Log the result details
$this->_writeDebug('Evaluation Result is '.$this->_showTypeDetails($result));
$this->_writeDebug('Evaluation Result is ', $this->_showTypeDetails($result));
// And push the result onto the stack
$stack->push('Value',$result);
return true;
} // function _executeNumericBinaryOperation()
private function _writeDebug($message) {
private function _writeDebug() {
// Only write the debug log if logging is enabled
if ($this->writeDebugLog) {
$message = implode('', func_get_args());
if ($this->echoDebugLog) {
echo implode(' -> ',$this->debugLogStack).' -> '.$message,'<br />';
echo implode(' -> ', $this->debugLogStack) . ' -> ' . $message , PHP_EOL;
}
$this->debugLog[] = implode(' -> ',$this->debugLogStack).' -> '.$message;
$this->debugLog[] = implode(' -> ', $this->debugLogStack) . ' -> ' . $message;
}
} // function _writeDebug()

View File

@ -51,18 +51,11 @@ class PHPExcel_Cell
private static $_valueBinder = NULL;
/**
* Column of the cell
* Cell Address (e.g. A1)
*
* @var string
*/
private $_column;
/**
* Row of the cell
*
* @var int
*/
private $_row;
private $_coordinate;
/**
* Value of the cell
@ -143,8 +136,7 @@ class PHPExcel_Cell
public function __construct($pColumn = 'A', $pRow = 1, $pValue = NULL, $pDataType = NULL, PHPExcel_Worksheet $pSheet = NULL)
{
// Initialise cell coordinate
$this->_column = strtoupper($pColumn);
$this->_row = $pRow;
$this->_coordinate = strtoupper($pColumn) . $pRow;
// Initialise cell value
$this->_value = $pValue;
@ -174,7 +166,8 @@ class PHPExcel_Cell
*/
public function getColumn()
{
return $this->_column;
list($column) = sscanf($this->_coordinate, '%[A-Z]%d');
return $column;
}
/**
@ -184,7 +177,8 @@ class PHPExcel_Cell
*/
public function getRow()
{
return $this->_row;
list(,$row) = sscanf($this->_coordinate, '%[A-Z]%d');
return $row;
}
/**
@ -194,7 +188,7 @@ class PHPExcel_Cell
*/
public function getCoordinate()
{
return $this->_column . $this->_row;
return $this->_coordinate;
}
/**