PDA

View Full Version : Review game score processing code, and an updated award cache function



IdealWebTech
05-04-2010, 05:00 AM
I haven't spent a whole bunch of time looking at this yet (for improvements) but the game processing code with an arcade of 16k games the rebuild scores util takes 95m of memory (php memory limit) for it to actually process. I tested this overwriting the memory limit for the file till the script ran successfully and then backed it off to find where the limit was actually at for debugging purposes.


Also a 5mb game table querying it and then the act of serializing the data overflows 32mb of memory (doesnt overflow since the limit has been set to 64mb), so we need to see what we can do here to improve this. I know the datastore and the act of serializing it will create a big memory overhead at the point of serializing. It seems to me like we are storing some redundant data which isn't helping things. I also made a few other changes to improve its performance currently. Such as freeing the result set after we are done, and correcting the query to check for the right default value.



// ##### Cache Awards #########################################################
function build_arcade_award_cache()
{
global $vbulletin;

if (!$vbulletin->options['arcadeawards'])
{
build_datastore('v3a_awards', serialize(array()), 1);
return false;
}

// multi-color crowns
if ($vbulletin->options['arcadeawards'] == 3)
{
$crownimages = range(1, 8);
}

$cache = array();
//This checked on IS NOT NULL which with the current datatype/default value it will never be null. Below is the corrected one, this avoids building the cache for games we dont need.
$awards = $vbulletin->db->query_read("
SELECT gameid, title, highscorerid, highscore, miniimage, stdimage
FROM " . TABLE_PREFIX . "v3arcade_games
WHERE highscorerid <> 0.000
AND (gamepermissions & " . $vbulletin->bf_misc_gamepermissions['showaward'] . ")
ORDER BY gameid
");
while ($award = $vbulletin->db->fetch_array($awards))
{
// multi-color crowns
if ($vbulletin->options['arcadeawards'] == 3)
{
$award['imgname'] = 'crown' . current($crownimages) . '.gif';
if (!next($crownimages))
{
reset($crownimages);
}
}
//Particularly here where we store the highscore and gameid twice since its in the awards array but we obviously should have it at the point of access already, while it may not seem like much it does start to add up with alot of games.
$cache[$award['highscorerid']][$award['gameid']] = $award;
}

//Free result for memory
$vbulletin->db->free_result($awards);

build_datastore('v3a_awards', serialize($cache), 1);
}


//Original function
// ##### Cache Awards #########################################################
function build_arcade_award_cache()
{
global $vbulletin;

if (!$vbulletin->options['arcadeawards'])
{
build_datastore('v3a_awards', serialize(array()), 1);
return false;
}

// multi-color crowns
if ($vbulletin->options['arcadeawards'] == 3)
{
$crownimages = range(1, 8);
}

$cache = array();
$awards = $vbulletin->db->query_read("
SELECT gameid, title, highscorerid, highscore, miniimage, stdimage
FROM " . TABLE_PREFIX . "v3arcade_games
WHERE highscorerid IS NOT NULL
AND (gamepermissions & " . $vbulletin->bf_misc_gamepermissions['showaward'] . ")
ORDER BY gameid
");
while ($award = $vbulletin->db->fetch_array($awards))
{
// multi-color crowns
if ($vbulletin->options['arcadeawards'] == 3)
{
$award['imgname'] = 'crown' . current($crownimages) . '.gif';
if (!next($crownimages))
{
reset($crownimages);
}
}

$cache[$award['highscorerid']][$award['gameid']] = $award;
}

build_datastore('v3a_awards', serialize($cache), 1);
}


-Tim
Ideal Web Technologies (http://www.idealwebtech.com)
eXtreme Game Zone (http://www.extremegamezone.com)