Index: trunk/wp-blog-header.php
===================================================================
--- trunk/wp-blog-header.php	(revision 2476)
+++ trunk/wp-blog-header.php	(working copy)
@@ -169,7 +169,7 @@
 update_category_cache();
 
 // Call query posts to do the work.
-$posts = query_posts($query_string);
+$posts = & query_posts($query_string);
 
 // Extract updated query vars back into global namespace.
 extract($wp_query->query_vars);
Index: trunk/wp-includes/template-functions-general.php
===================================================================
--- trunk/wp-includes/template-functions-general.php	(revision 2476)
+++ trunk/wp-includes/template-functions-general.php	(working copy)
@@ -193,8 +193,8 @@
         if (!$p) {
             $p = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$name'");
         }
-        $post_data = get_postdata($p);
-        $title = $post_data['Title'];
+        $post = & get_post($p);
+        $title = $post->post_title;
         $title = apply_filters('single_post_title', $title);
         if ($display) {
             echo $prefix.strip_tags($title);
@@ -347,11 +347,11 @@
             }
         }
     } elseif ('postbypost' == $type) {
-        $arcresults = $wpdb->get_results("SELECT ID, post_date, post_title FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);
+        $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" . $limit);
         if ($arcresults) {
             foreach ($arcresults as $arcresult) {
                 if ($arcresult->post_date != '0000-00-00 00:00:00') {
-                    $url  = get_permalink($arcresult->ID);
+                    $url  = get_permalink($arcresult);
                     $arc_title = $arcresult->post_title;
                     if ($arc_title) {
                         $text = strip_tags($arc_title);
Index: trunk/wp-includes/template-functions-category.php
===================================================================
--- trunk/wp-includes/template-functions-category.php	(revision 2476)
+++ trunk/wp-includes/template-functions-category.php	(working copy)
@@ -6,16 +6,11 @@
 	if ( !$id )
 		$id = $post->ID;
 
-	if ( $category_cache[$id] ) {
-		$categories = $category_cache[$id];
-    } else {
-		$categories = $wpdb->get_results("
-		SELECT category_id, cat_name, category_nicename, category_description, category_parent
-		FROM  $wpdb->categories, $wpdb->post2cat
-		WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id'
-		");
-    }
+	if ( ! isset($category_cache[$id]) )
+		update_post_category_cache($id);
 
+	$categories = $category_cache[$id];
+
 	if (!empty($categories))
 		sort($categories);
 	else
@@ -25,19 +20,17 @@
 }
 
 function get_category_link($category_id) {
-	global $wpdb, $wp_rewrite, $querystring_start, $querystring_equal, $cache_categories;
+	global $wpdb, $wp_rewrite;
 	$catlink = $wp_rewrite->get_category_permastruct();
 
 	if ( empty($catlink) ) {
 		$file = get_settings('home') . '/' . get_settings('blogfilename');
 		$catlink = $file . '?cat=' . $category_id;
 	} else {
-		if ($cache_categories[$category_id]->category_nicename)
-			$category_nicename = $cache_categories[$category_id]->category_nicename;
-		else
-			$category_nicename = $wpdb->get_var('SELECT category_nicename FROM ' . $wpdb->categories . ' WHERE cat_ID=' . $category_id);
+		$category = &get_category($category_id);
+		$category_nicename = $category->category_nicename;
 
-		if ($parent = $cache_categories[$category_id]->category_parent) 
+		if ($parent = $category->category_parent) 
 			$category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/';
 
 		$catlink = str_replace('%category%', $category_nicename, $catlink);
@@ -108,20 +101,13 @@
 }
 
 function get_the_category_by_ID($cat_ID) {
-    global $cache_categories, $wpdb;
-    if ( !$cache_categories[$cat_ID] ) {
-        $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
-        $cache_categories[$cat_ID]->cat_name = $cat_name;
-    } else {
-        $cat_name = $cache_categories[$cat_ID]->cat_name;
-    }
-    return($cat_name);
+	$category = &get_category($cat_ID);
+	return $category->cat_name;
 }
 
 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
-    global $cache_categories;
     $chain = '';
-    $parent = $cache_categories[$id];
+		$parent = &get_category($id);
     if ($nicename) {
         $name = $parent->category_nicename;
     } else {
@@ -175,11 +161,10 @@
 }
 
 function category_description($category = 0) {
-    global $cat, $wpdb, $cache_categories;
+    global $cat, $wpdb;
     if (!$category) $category = $cat;
-    $category_description = $cache_categories[$category]->category_description;
-    $category_description = apply_filters('category_description', $category_description, $category);
-    return $category_description;
+		$category = & get_category($category);
+    return apply_filters('category_description', $category->category_description, $category->cat_ID);
 }
 
 // out of the WordPress loop
Index: trunk/wp-includes/classes.php
===================================================================
--- trunk/wp-includes/classes.php	(revision 2476)
+++ trunk/wp-includes/classes.php	(working copy)
@@ -217,7 +217,7 @@
 		$this->query_vars[$query_var] = $value;
 	}
 
-	function get_posts() {
+	function &get_posts() {
 		global $wpdb, $pagenow, $request, $user_ID;
 
 		// Shorthand.
@@ -595,7 +595,7 @@
 		}
 	}
     
-	function query($query) {
+	function &query($query) {
 		$this->parse_query($query);
 		return $this->get_posts();
 	}
@@ -609,11 +609,9 @@
 		$this->queried_object_id = 0;
 
 		if ($this->is_category) {
-			global $cache_categories;
-			if (isset($cache_categories[$this->get('cat')])) {
-				$this->queried_object = $cache_categories[$this->get('cat')];
-				$this->queried_object_id = $this->get('cat');
-			}
+			$category = &get_category($this->get('cat'));
+			$this->queried_object = &$category;
+			$this->queried_object_id = $this->get('cat');
 		} else if ($this->is_single) {
 			$this->queried_object = $this->post;
 			$this->queried_object_id = $this->post->ID;
Index: trunk/wp-includes/template-functions-links.php
===================================================================
--- trunk/wp-includes/template-functions-links.php	(revision 2476)
+++ trunk/wp-includes/template-functions-links.php	(working copy)
@@ -22,9 +22,7 @@
     }
 }
 
-function get_permalink($id = false) {
-	global $post, $wpdb;
-
+function get_permalink(&$id = null) {
 	$rewritecode = array(
 		'%year%',
 		'%monthnum%',
@@ -39,29 +37,24 @@
 		'%pagename%'
 	);
 
-	if ($id) {
-		$idpost = $wpdb->get_row("SELECT ID, post_date, post_name, post_status, post_author FROM $wpdb->posts WHERE ID = $id");
-	} else {
-		$idpost = $post;
+	$post = & get_post($id);
+	if ($post->post_status == 'static') {
+		return get_page_link($post->ID);
 	}
 
-	if ($idpost->post_status == 'static') {
-		return get_page_link($idpost->ID);
-	}
-
 	$permalink = get_settings('permalink_structure');
 
 	if ('' != $permalink) {
-		$unixtime = strtotime($idpost->post_date);
+		$unixtime = strtotime($post->post_date);
 
 		$category = '';
 		if (strstr($permalink, '%category%')) {
-			$cats = get_the_category($idpost->ID);
+			$cats = get_the_category($post->ID);
 			$category = $cats[0]->category_nicename;
 			if ($parent=$cats[0]->category_parent) $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
 		}
 
-		$authordata = get_userdata($idpost->post_author);
+		$authordata = get_userdata($post->post_author);
 		$author = $authordata->user_nicename;
 		$rewritereplace = 
 		array(
@@ -71,16 +64,16 @@
 			date('H', $unixtime),
 			date('i', $unixtime),
 			date('s', $unixtime),
-			$idpost->post_name,
-			$idpost->ID,
+			$post->post_name,
+			$post->ID,
 			$category,
 			$author,
-			$idpost->post_name,
+			$post->post_name,
 		);
-		return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $idpost);
+		return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
 	} else { // if they're not using the fancy permalink option
-		$permalink = get_settings('home') . '/?p=' . $idpost->ID;
-		return apply_filters('post_link', $permalink, $idpost);
+		$permalink = get_settings('home') . '/?p=' . $post->ID;
+		return apply_filters('post_link', $permalink, $post);
 	}
 }
 
Index: trunk/wp-includes/template-functions-post.php
===================================================================
--- trunk/wp-includes/template-functions-post.php	(revision 2476)
+++ trunk/wp-includes/template-functions-post.php	(working copy)
@@ -26,34 +26,23 @@
 }
 
 function get_the_title($id = 0) {
-	global $post, $wpdb;
-	
-	if ( 0 != $id ) {
-		$id_post = $wpdb->get_row("SELECT post_title, post_password FROM $wpdb->posts WHERE ID = $id");
-		$title = $id_post->post_title;
-		if (!empty($id_post->post_password))
-			$title = sprintf(__('Protected: %s'), $title);
-	}
-	else {
-		$title = $post->post_title;
-		if (!empty($post->post_password))
-			$title = sprintf(__('Protected: %s'), $title);
-	}
+	$post = &get_post($id);
+
+	$title = $post->post_title;
+	if (!empty($post->post_password))
+		$title = sprintf(__('Protected: %s'), $title);
+
 	return $title;
 }
 
 function get_the_guid( $id = 0 ) {
-	global $post, $wpdb;
-	$guid = $post->guid;
-
-	if ( 0 != $id )
-		$guid = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE ID = $id");
-	$guid = apply_filters('get_the_guid', $guid);
-	return $guid;
+	$post = &get_post($id);
+	
+	return apply_filters('get_the_guid', $post->guid);
 }
 
 function the_guid( $id = 0 ) {
-	echo get_the_guid();
+	echo get_the_guid($id);
 }
 
 
@@ -261,7 +250,7 @@
 // Pages
 //
 
-function get_pages($args = '') {
+function &get_pages($args = '') {
 	global $wpdb, $cache_pages;
 
 	parse_str($args, $r);
@@ -280,29 +269,20 @@
 		}
 	}
 
-	$dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified";
-	$dates .= ",UNIX_TIMESTAMP(post_date) AS time_created";
-	
 	$post_parent = '';
 	if ($r['child_of']) {
 		$post_parent = ' AND post_parent=' . $r['child_of'] . ' ';
 	}
 	
-	$pages = $wpdb->get_results("SELECT " .
-															"ID, post_title, post_name, post_parent " .
-															"$dates " .
+	$pages = $wpdb->get_results("SELECT * " .
 															"FROM $wpdb->posts " .
 															"WHERE post_status = 'static' " .
 															"$post_parent" .
 															"$exclusions " .
 															"ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
 
-	// Update page cache.
-	if (count($pages)) {
-		foreach($pages as $page) {
-			$cache_pages[$page->ID] = $page;
-		}
-	}
+	// Update cache.
+	update_post_cache($pages);
 
 	if ( empty($pages) )
 		$pages = array();
@@ -319,7 +299,7 @@
 
 
 	// Query pages.
-	$pages = get_pages($args);
+	$pages = & get_pages($args);
 	if ( $pages ) :
 
 	if ( $r['title_li'] )
@@ -338,9 +318,9 @@
 		// ts field.
 		if (! empty($r['show_date'])) {
 			if ('modified' == $r['show_date'])
-				$page_tree[$page->ID]['ts'] = $page->time_modified;
+				$page_tree[$page->ID]['ts'] = $page->post_modified;
 			else
-				$page_tree[$page->ID]['ts'] = $page->time_created;
+				$page_tree[$page->ID]['ts'] = $page->post_date;
 		}
 
 		// The tricky bit!!
@@ -383,7 +363,7 @@
 			$format = get_settings('date_format');
 			if(isset($args['date_format']))
 				$format = $args['date_format'];
-			echo " " . gmdate($format,$cur_page['ts']);
+			echo " " . mysql2date($format,$cur_page['ts']);
 		}
 		echo "\n";
 
Index: trunk/wp-includes/functions.php
===================================================================
--- trunk/wp-includes/functions.php	(revision 2476)
+++ trunk/wp-includes/functions.php	(working copy)
@@ -538,18 +538,44 @@
 	return $postdata;
 }
 
-function get_catname($cat_ID) {
-	global $cache_catnames, $wpdb;
-	if ( !$cache_catnames ) {
-        $results = $wpdb->get_results("SELECT * FROM $wpdb->categories") or die('Oops, couldn\'t query the db for categories.');
-		foreach ($results as $post) {
-			$cache_catnames[$post->cat_ID] = $post->cat_name;
-		}
+function &get_post(&$post = null) {
+	global $post_cache, $wpdb;
+
+	if ( empty($post) ) {
+		if ( isset($GLOBALS['post']) )
+			return $GLOBALS['post'];
+		else
+			return null;
+	} elseif (is_object($post) ) {
+		if (! isset($post_cache[$post->ID]))
+			$post_cache[$post->ID] = &$post;
+		return $post;
+	} else {
+		if ( isset($GLOBALS['post']) && ($post == $GLOBALS['post']->ID) )
+			return $GLOBALS['post'];
+
+		if (isset($post_cache[$post]))
+			return $post_cache[$post];
+
+		$query = "SELECT * FROM $wpdb->posts WHERE ID=$post";
+		$post_cache[$post] = & $wpdb->get_row($query);
+		return $post_cache[$post];
 	}
-	$cat_name = $cache_catnames[$cat_ID];
-	return $cat_name;
 }
 
+function &get_category(&$category) {
+	global $cache_categories;
+	if (is_object($category))
+		return $category;
+	else
+		return $cache_categories[$category];
+}
+
+function get_catname($cat_ID) {
+	$category = &get_category($cat_ID);
+	return $category->cat_name;
+}
+
 function gzip_compression() {
 	if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') ) return false;
 	if ( !get_settings('gzipcompression') ) return false;
@@ -995,30 +1021,16 @@
 }
 
 function get_page_uri($page_id) {
-	global $wpdb, $cache_pages;
-
-	$dates = ",UNIX_TIMESTAMP(post_modified) AS time_modified";
-	$dates .= ",UNIX_TIMESTAMP(post_date) AS time_created";
-
-	if (!isset($cache_pages[$page_id])) {
-		$cache_pages[$page_id] = $wpdb->get_row("SELECT ID, post_title, post_name, post_parent $dates FROM $wpdb->posts WHERE ID = '$page_id'");
-	}
-
-	$page = $cache_pages[$page_id];
+	$page = get_post($page_id);
 	$uri = urldecode($page->post_name);
 
 	// A page cannot be it's own parent.
 	if ($page->post_parent == $page->ID) {
 		return $uri;
 	}
-
+	
 	while ($page->post_parent != 0) {
-		if (isset($cache_pages[$page->post_parent])) {
-			$page = $cache_pages[$page->post_parent];
-		} else {
-			$page = $wpdb->get_row("SELECT ID, post_title, post_name, post_parent $dates FROM $wpdb->posts WHERE ID = '$page->post_parent'");
-			$cache_pages[$page->ID] = $page;
-		}
+		$page = get_post($page->post_parent);
 		$uri = urldecode($page->post_name) . "/" . $uri;
 	}
 
@@ -1049,35 +1061,60 @@
 	return $posts;
 }
 
-function query_posts($query) {
+function &query_posts($query) {
 	global $wp_query;
 	return $wp_query->query($query);
 }
 
-function update_post_caches($posts) {
-	global $category_cache, $comment_count_cache, $post_meta_cache;
-	global $wpdb;
-	
-	// No point in doing all this work if we didn't match any posts.
+function update_post_cache(&$posts) {
+	global $post_cache;
+
 	if ( !$posts )
 		return;
 
-	// Get the categories for all the posts
-	foreach ($posts as $post)
-		$post_id_list[] = $post->ID;
-	$post_id_list = implode(',', $post_id_list);
+	for ($i = 0; $i < count($posts); $i++) {
+		$post_cache[$posts[$i]->ID] = &$posts[$i];
+	}
+}
 
+function update_post_category_cache($post_ids) {
+	global $wpdb, $category_cache, $cache_categories;
+
+	if (empty($post_ids))
+		return;
+
+	if (is_array($post_ids))
+		$post_ids = implode(',', $post_ids);
+
 	$dogs = $wpdb->get_results("SELECT DISTINCT
-	post_id, category_id, cat_name, category_nicename, category_description, category_parent
-	FROM $wpdb->categories, $wpdb->post2cat
-	WHERE category_id = cat_ID AND post_id IN ($post_id_list)");
-    
+	post_id, category_id FROM $wpdb->categories, $wpdb->post2cat
+	WHERE category_id = cat_ID AND post_id IN ($post_ids)");
+
 	if ( !empty($dogs) ) {
 		foreach ($dogs as $catt) {
-			$category_cache[$catt->post_id][$catt->category_id] = $catt;
+			$category_cache[$catt->post_id][$catt->category_id] = &$cache_categories[$catt->category_id];
 		}
 	}
+}
 
+function update_post_caches(&$posts) {
+	global $post_cache, $category_cache, $comment_count_cache, $post_meta_cache;
+	global $wpdb;
+	
+	// No point in doing all this work if we didn't match any posts.
+	if ( !$posts )
+		return;
+
+	// Get the categories for all the posts
+	for ($i = 0; $i < count($posts); $i++) {
+		$post_id_list[] = $posts[$i]->ID;
+		$post_cache[$posts[$i]->ID] = &$posts[$i];
+	}
+
+	$post_id_list = implode(',', $post_id_list);
+	
+	update_post_category_cache($post_id_list);
+
 	// Do the same for comment numbers
 	$comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
 	FROM $wpdb->posts
@@ -1114,8 +1151,10 @@
 function update_category_cache() {
 	global $cache_categories, $wpdb;
 	$dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories");
-	foreach ($dogs as $catt)
+	foreach ($dogs as $catt) {
+		$catt->category_id = $catt->cat_ID; // Alias.
 		$cache_categories[$catt->cat_ID] = $catt;
+	}
 }
 
 function update_user_cache() {
