Skip to content

Commit

Permalink
Faster array merges.
Browse files Browse the repository at this point in the history
When dealing with masses of items, array_merge is useful but very slow (and slower the more items it has to work agains). Move to a faster merging algorithm in an attempt to help speed things along.
  • Loading branch information
mastacontrola committed Jan 1, 2017
1 parent 460d931 commit 4148e87
Show file tree
Hide file tree
Showing 41 changed files with 126 additions and 98 deletions.
2 changes: 1 addition & 1 deletion bin/installfog.sh
Expand Up @@ -407,7 +407,7 @@ if [[ -z $backupPath ]]; then
fi
[[ -z $bootfilename ]] && bootfilename="undionly.kpxe"
[[ ! $doupdate -eq 1 || ! $fogupdateloaded -eq 1 ]] && . ../lib/common/input.sh
fullrelease='1.3.0'
fullrelease=0
echo
echo " ######################################################################"
echo " # FOG now has everything it needs for this setup, but please #"
Expand Down
10 changes: 5 additions & 5 deletions packages/web/commons/schema.php
Expand Up @@ -2144,7 +2144,7 @@
. "WHERE `pxeName`='fog.approvehost'",
);
// 130
$this->schema[] = array_merge(
$this->schema[] = self::fastmerge(
array(
"ALTER TABLE `hostMAC` ADD COLUMN `hmPrimary` INT DEFAULT 0 NOT NULL",
"ALTER TABLE `hostMAC` ADD COLUMN `hmPending` INT DEFAULT 0 NOT NULL",
Expand Down Expand Up @@ -2227,7 +2227,7 @@
. "INT(11) NOT NULL AFTER msClients",
);
// 136
$this->schema[] = array_merge(
$this->schema[] = self::fastmerge(
array(
"ALTER TABLE `tasks` ADD COLUMN `taskImageID` "
. "INT(11) NOT NULL AFTER `taskHostID`",
Expand Down Expand Up @@ -2289,7 +2289,7 @@
. "'noreply@\$\{server-name\}.com','FOG Email Settings')",
);
// 140
$this->schema[] = array_merge(
$this->schema[] = self::fastmerge(
array(
"CREATE TABLE IF NOT EXISTS `snapinGroupAssoc` ("
. "`sgaID` mediumint(9) NOT NULL auto_increment,"
Expand Down Expand Up @@ -2436,7 +2436,7 @@
// 160
$this->schema[] = array();
// 161
$this->schema[] = array_merge(
$this->schema[] = self::fastmerge(
$tmpSchema->dropDuplicateData(
DATABASE_NAME,
array(
Expand Down Expand Up @@ -2920,7 +2920,7 @@
"UPDATE `nfsGroupMembers` SET `ngmWebroot`='/fog/'",
);
// 189
$this->schema[] = array_merge(
$this->schema[] = self::fastmerge(
$tmpSchema->dropDuplicateData(
DATABASE_NAME,
array(
Expand Down
2 changes: 1 addition & 1 deletion packages/web/lib/client/servicemodule.class.php
Expand Up @@ -99,7 +99,7 @@ public function send()
);
if (in_array(
$mod,
array_merge(
self::fastmerge(
(array)$globalDisabled,
(array)$hostDisabled
)
Expand Down
14 changes: 7 additions & 7 deletions packages/web/lib/client/snapinclient.class.php
Expand Up @@ -46,7 +46,7 @@ public function json()
->count(
array(
'jobID' => $SnapinJob->get('id'),
'stateID' => array_merge(
'stateID' => self::fastmerge(
$this->getQueuedStates(),
(array)$this->getProgressState()
)
Expand Down Expand Up @@ -81,7 +81,7 @@ public function json()
$snapinIDs = self::getSubObjectIDs(
'SnapinTask',
array(
'stateID' => array_merge(
'stateID' => self::fastmerge(
$this->getQueuedStates(),
(array)$this->getProgressState()
),
Expand Down Expand Up @@ -116,7 +116,7 @@ public function json()
array(
'snapinID' => $Snapin->get('id'),
'jobID' => $SnapinJob->get('id'),
'stateID' => array_merge(
'stateID' => self::fastmerge(
$this->getQueuedStates(),
(array)$this->getProgressState()
)
Expand Down Expand Up @@ -228,7 +228,7 @@ public function send()
->count(
array(
'jobID' => $SnapinJob->get('id'),
'stateID' => array_merge(
'stateID' => self::fastmerge(
$this->getQueuedStates(),
(array)$this->getProgressState()
)
Expand Down Expand Up @@ -260,7 +260,7 @@ public function send()
$snapinIDs = self::getSubObjectIDs(
'SnapinTask',
array(
'stateID' => array_merge(
'stateID' => self::fastmerge(
$this->getQueuedStates(),
(array)$this->getProgressState()
),
Expand Down Expand Up @@ -306,7 +306,7 @@ public function send()
array(
'snapinID' => $Snapin->get('id'),
'jobID' => $SnapinJob->get('id'),
'stateID' => array_merge(
'stateID' => self::fastmerge(
$this->getQueuedStates(),
(array)$this->getProgressState()
)
Expand Down Expand Up @@ -453,7 +453,7 @@ private function _closeout($Task, $SnapinJob, $date, $HostName)
->count(
array(
'jobID' => $SnapinJob->get('id'),
'stateID' => array_merge(
'stateID' => self::fastmerge(
$this->getQueuedStates(),
(array)$this->getProgressState()
)
Expand Down
16 changes: 8 additions & 8 deletions packages/web/lib/fog/bootmenu.class.php
Expand Up @@ -696,7 +696,7 @@ public function sesscheck()
{
$findWhere = array(
'name' => trim($_REQUEST['sessname']),
'stateID' => array_merge(
'stateID' => self::fastmerge(
$this->getQueuedStates(),
(array)$this->getProgressState()
),
Expand Down Expand Up @@ -1649,17 +1649,17 @@ private function _menuOpt($option, $type)
}
}
$params = trim(implode("\n", (array)$params));
$Send = array_merge($Send, array($params));
$Send = self::fastmerge($Send, array($params));
}
switch ($option->get('id')) {
case 1:
$Send = array_merge(
$Send = self::fastmerge(
$Send,
array("$this->_bootexittype || goto MENU")
);
break;
case 2:
$Send = array_merge(
$Send = self::fastmerge(
$Send,
array(
"$this->_memdisk iso raw",
Expand All @@ -1669,7 +1669,7 @@ private function _menuOpt($option, $type)
);
break;
case 11:
$Send = array_merge(
$Send = self::fastmerge(
$Send,
array(
"chain -ar $this->_booturl/ipxe/advanced.php || "
Expand All @@ -1679,7 +1679,7 @@ private function _menuOpt($option, $type)
break;
}
if (!$params) {
$Send = array_merge(
$Send = self::fastmerge(
$Send,
array(
"$this->_kernel $this->_loglevel $type",
Expand Down Expand Up @@ -1742,7 +1742,7 @@ public function printDefault()
false,
''
);
$Send['head'] = array_merge(
$Send['head'] = self::fastmerge(
array(
'cpuid --ext 29 && set arch x86_64 || set arch i386',
'goto get_console',
Expand Down Expand Up @@ -1772,7 +1772,7 @@ public function printDefault()
"registered as {$this->_Host->get(name)}!"
);
}
$Send['menustart'] = array_merge(
$Send['menustart'] = self::fastmerge(
array(
':MENU',
'menu',
Expand Down
29 changes: 27 additions & 2 deletions packages/web/lib/fog/fogbase.class.php
Expand Up @@ -1608,7 +1608,7 @@ public function parseMacList(
$existingMACs = array_filter($existingMACs);
$existingMACs = array_unique($existingMACs);
$existingMACs = array_values($existingMACs);
$MACs = array_merge((array) $MACs, (array) $existingMACs);
$MACs = self::fastmerge((array) $MACs, (array) $existingMACs);
$MACs = array_unique($MACs);
}
if ($client) {
Expand Down Expand Up @@ -2103,7 +2103,7 @@ protected static function getIPAddress()
};
$IPs = array_map($retIPs, (array) $IPs);
$Names = array_map($retNames, (array) $IPs);
$output = array_merge($IPs, $Names);
$output = self::fastmerge($IPs, $Names);
unset($IPs, $Names);
natcasesort($output);
self::$ips = array_values(array_filter(array_unique((array) $output)));
Expand Down Expand Up @@ -2219,4 +2219,29 @@ public function wakeUp($macs)
false
);
}
/**
* Faster array merge operation.
*
* @param array $array1 The array to merge with.
*
* @return array
*/
public static function fastmerge($array1)
{
$others = func_get_args();
array_shift($others);
foreach ((array)$others as &$other) {
foreach ((array)$other as $key => &$oth) {
if (is_numeric($key)) {
$array1[] = $oth;
continue;
}
unset($oth);
}
$array1 += $other;
unset($other);
}

return $array1;
}
}
2 changes: 1 addition & 1 deletion packages/web/lib/fog/fogcontroller.class.php
Expand Up @@ -1079,7 +1079,7 @@ public function setQuery(&$queryData)
unset($db_key, $obj_key);
}
}
$this->data = array_merge(
$this->data = self::fastmerge(
(array) $this->data,
(array) $classData
);
Expand Down

0 comments on commit 4148e87

Please sign in to comment.