Fork me on GitHub

Comment by quy (2013-10-20 18:55:52)

Edited by quy (2013-10-20 20:14:47)

 1:  1:
 Here is a proposed change. Instead of adding all new topics into the array, only forums with new topics will be added per forum. In this test case of 30,000 topics, potentially up to 30,000 can be added vs. adding maximum # of forums (20 in this case)...big difference. Here is a proposed change. Instead of adding all new topics into the array, only forums with new topics will be added per forum. In this test case of 30,000 topics, potentially up to 30,000 can be added vs. adding maximum # of forums (20 in this case)...big difference.
  
 Generation times are approximately the same but memory usage is less. Generation times are approximately the same but memory usage is less.
-Before = Memory usage: 3.97 MiB (Peak: 5.63 MiB)] +Before = [ Generated in 2.785 seconds, 9 queries executed - Memory usage: 3.97 MiB (Peak: 5.63 MiB) ] 
-After = Memory usage: 709.87 KiB (Peak: 5.09 MiB]+After = [ Generated in 2.570 seconds, 9 queries executed - Memory usage: 709.87 KiB (Peak: 5.09 MiB]
  
 find: find:
 [code]if (!$pun_user['is_guest'])  [code]if (!$pun_user['is_guest'])
 {  {
  $result = $db->query('SELECT t.forum_id, t.id, t.last_post FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.$pun_user['last_visit'].' AND t.moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error());   $result = $db->query('SELECT t.forum_id, t.id, t.last_post FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.$pun_user['last_visit'].' AND t.moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error());
   
  $new_topics = array();   $new_topics = array();
  while ($cur_topic = $db->fetch_assoc($result))   while ($cur_topic = $db->fetch_assoc($result))
  $new_topics[$cur_topic['forum_id']][$cur_topic['id']] = $cur_topic['last_post'];   $new_topics[$cur_topic['forum_id']][$cur_topic['id']] = $cur_topic['last_post'];
   
  $tracked_topics = get_tracked_topics();   $tracked_topics = get_tracked_topics();
 }[/code] }[/code]
  
 change: change:
 [code]if (!$pun_user['is_guest'])  [code]if (!$pun_user['is_guest'])
 {  {
  $tracked_topics = get_tracked_topics();   $tracked_topics = get_tracked_topics();
   
  $result = $db->query('SELECT t.forum_id, t.id, t.last_post, f.last_post AS forum_last_post FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.$pun_user['last_visit'].' AND t.moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error());   $result = $db->query('SELECT t.forum_id, t.id, t.last_post, f.last_post AS forum_last_post FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.$pun_user['last_visit'].' AND t.moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error());
   
  $new_topics = array();   $new_topics = array();
  while ($cur_topic = $db->fetch_assoc($result))   while ($cur_topic = $db->fetch_assoc($result))
  {   {
  if (!isset($new_topics[$cur_topic['forum_id']]) && (!isset($tracked_topics['topics'][$cur_topic['id']]) || $tracked_topics['topics'][$cur_topic['id']] < $cur_topic['last_post']) && (!isset($tracked_topics['forums'][$cur_topic['forum_id']]) || $tracked_topics['forums'][$cur_topic['forum_id']] < $cur_topic['forum_last_post']))   if (!isset($new_topics[$cur_topic['forum_id']]) && (!isset($tracked_topics['topics'][$cur_topic['id']]) || $tracked_topics['topics'][$cur_topic['id']] < $cur_topic['last_post']) && (!isset($tracked_topics['forums'][$cur_topic['forum_id']]) || $tracked_topics['forums'][$cur_topic['forum_id']] < $cur_topic['forum_last_post']))
  $new_topics[$cur_topic['forum_id']] = 1;   $new_topics[$cur_topic['forum_id']] = 1;
  }   }
 }[/code] }[/code]
  
 find: find:
 [code] if (!$pun_user['is_guest'] && $cur_forum['last_post'] > $pun_user['last_visit'] && (empty($tracked_topics['forums'][$cur_forum['fid']]) || $cur_forum['last_post'] > $tracked_topics['forums'][$cur_forum['fid']]))  [code] if (!$pun_user['is_guest'] && $cur_forum['last_post'] > $pun_user['last_visit'] && (empty($tracked_topics['forums'][$cur_forum['fid']]) || $cur_forum['last_post'] > $tracked_topics['forums'][$cur_forum['fid']]))
  {   {
  // There are new posts in this forum, but have we read all of them already?   // There are new posts in this forum, but have we read all of them already?
  foreach ($new_topics[$cur_forum['fid']] as $check_topic_id => $check_last_post)   foreach ($new_topics[$cur_forum['fid']] as $check_topic_id => $check_last_post)
  {   {
  if ((empty($tracked_topics['topics'][$check_topic_id]) || $tracked_topics['topics'][$check_topic_id] < $check_last_post) && (empty($tracked_topics['forums'][$cur_forum['fid']]) || $tracked_topics['forums'][$cur_forum['fid']] < $check_last_post))   if ((empty($tracked_topics['topics'][$check_topic_id]) || $tracked_topics['topics'][$check_topic_id] < $check_last_post) && (empty($tracked_topics['forums'][$cur_forum['fid']]) || $tracked_topics['forums'][$cur_forum['fid']] < $check_last_post))
  {   {
  $item_status .= ' inew';   $item_status .= ' inew';
  $forum_field_new = '<span class="newtext">[ <a href="search.php?action=show_new&fid='.$cur_forum['fid'].'">'.$lang_common['New posts'].'</a> ]</span>';   $forum_field_new = '<span class="newtext">[ <a href="search.php?action=show_new&fid='.$cur_forum['fid'].'">'.$lang_common['New posts'].'</a> ]</span>';
  $icon_type = 'icon icon-new';   $icon_type = 'icon icon-new';
   
  break;   break;
  }   }
  }   }
  }[/code]  }[/code]
                  
 change: change:
 [code] if (!$pun_user['is_guest'] && isset($new_topics[$cur_forum['fid']]))  [code] if (!$pun_user['is_guest'] && isset($new_topics[$cur_forum['fid']]))
  {   {
  $item_status .= ' inew';   $item_status .= ' inew';
  $forum_field_new = '<span class="newtext">[ <a href="search.php?action=show_new&fid='.$cur_forum['fid'].'">'.$lang_common['New posts'].'</a> ]</span>';   $forum_field_new = '<span class="newtext">[ <a href="search.php?action=show_new&fid='.$cur_forum['fid'].'">'.$lang_common['New posts'].'</a> ]</span>';
  $icon_type = 'icon icon-new';   $icon_type = 'icon icon-new';
  }[/code]  }[/code]