Speed fix - takes memory - is it right?

Topics: Developer Forum
Nov 20, 2014 at 3:56 PM
Edited Nov 20, 2014 at 3:58 PM
Hello,

a quick (dirty) fix to greatly speedup styles assignment. What do you think about it?

The method getCellXfByHashCode() is awfully slow, so I tried to replace it with a cache array.

version: 1.8.0
file: PHPExcel/Style.php
method: applyFromArray()

new code:
global $__cache;
if (!isset($__cache)) {
    $__cache = array();
}

        $hashCode = $newStyle->getHashCode();
        if (isset($__cache[$hashCode])) {
            $newXfIndexes[$oldXfIndex] = $__cache[$hashCode];
        } else {
            // we don't have such a cell Xf, need to add
            $workbook->addCellXf($newStyle);
            $newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
            $__cache[$hashCode] = $newStyle->getIndex();
        }
old code:
if ($existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode())) {
    $newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
} else {
    // we don't have such a cell Xf, need to add
    $workbook->addCellXf($newStyle);
    $newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
}
Coordinator
Nov 23, 2014 at 9:26 PM
Edited Nov 23, 2014 at 9:26 PM
The biggest problem with this is the use of a global for the cache, which would break if more than one workbook was loaded at the same time as they would both be sharing the same cache space... to make this work, the cache would need to be a workbook property so that it was unique to each loaded workbook

And yes, it would add memory overhead