diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2017-11-20 10:53:18 +0300 |
---|---|---|
committer | Matthieu Aubry <mattab@users.noreply.github.com> | 2017-11-20 10:53:18 +0300 |
commit | 7846b9a6c4b9ee8684860fb8d4052a80ecc2d970 (patch) | |
tree | 04fc7673e36d2869f17bd66fac889bfd89bda75d /core/DataTable.php | |
parent | 82d8b5413465d241f0eab8f1a106909f3bdfd9d3 (diff) |
Improve serialize dataTable performance (#12289)
* Improve serialize dataTable performance
This seems to improve the performance of serializing a data table which is useful when archiving. In my case it made the archiving of a monthly archive twice as fast from about 120 seconds down to like 70seconds.
* remove no longer needed variable
* faster check whether an array key is set
It is known that array_key_exists is quite a bit slower than `isset`. As this is executed easily a few million times this makes a difference in performance but here `array_key_exists` and `isset` work the same in logic as we never set `$consecutiveSubtableIds[$id]=null`
* Assignment should be no longer needed as passing by reference
Diffstat (limited to 'core/DataTable.php')
-rw-r--r-- | core/DataTable.php | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/core/DataTable.php b/core/DataTable.php index e653470c6c..c94ddafb46 100644 --- a/core/DataTable.php +++ b/core/DataTable.php @@ -1227,6 +1227,7 @@ class DataTable implements DataTableInterface, \IteratorAggregate, \ArrayAccess * @param int $maximumRowsInDataTable If not null, defines the maximum number of rows allowed in the serialized DataTable. * @param int $maximumRowsInSubDataTable If not null, defines the maximum number of rows allowed in serialized subtables. * @param string $columnToSortByBeforeTruncation The column to sort by before truncating, eg, `Metrics::INDEX_NB_VISITS`. + * @param array $aSerializedDataTable Will contain all the output arrays * @return array The array of serialized DataTables: * * array( @@ -1244,7 +1245,8 @@ class DataTable implements DataTableInterface, \IteratorAggregate, \ArrayAccess */ public function getSerialized($maximumRowsInDataTable = null, $maximumRowsInSubDataTable = null, - $columnToSortByBeforeTruncation = null) + $columnToSortByBeforeTruncation = null, + &$aSerializedDataTable = array()) { static $depth = 0; // make sure subtableIds are consecutive from 1 to N @@ -1270,13 +1272,12 @@ class DataTable implements DataTableInterface, \IteratorAggregate, \ArrayAccess // For each row, get the serialized row // If it is associated to a sub table, get the serialized table recursively ; // but returns all serialized tables and subtable in an array of 1 dimension - $aSerializedDataTable = array(); foreach ($this->rows as $id => $row) { $subTable = $row->getSubtable(); if ($subTable) { $consecutiveSubtableIds[$id] = ++$subtableId; $depth++; - $aSerializedDataTable = $aSerializedDataTable + $subTable->getSerialized($maximumRowsInSubDataTable, $maximumRowsInSubDataTable, $columnToSortByBeforeTruncation); + $subTable->getSerialized($maximumRowsInSubDataTable, $maximumRowsInSubDataTable, $columnToSortByBeforeTruncation, $aSerializedDataTable); $depth--; } else { $row->removeSubtable(); @@ -1292,7 +1293,7 @@ class DataTable implements DataTableInterface, \IteratorAggregate, \ArrayAccess // we then serialize the rows and store them in the serialized dataTable $rows = array(); foreach ($this->rows as $id => $row) { - if (array_key_exists($id, $consecutiveSubtableIds)) { + if (isset($consecutiveSubtableIds[$id])) { $backup = $row->subtableId; $row->subtableId = $consecutiveSubtableIds[$id]; $rows[$id] = $row->export(); |