commit add610dd2d8c1dee45da70f8efc309c29227c738
Author: Adam Franco <afranco@middlebury.edu>
Date:   Thu Jul 28 11:09:22 2011 -0400

    Issue #1232498 Avoid loading all tag data on every page load.
    
    Because _nodewords_detect_type_and_ids() makes a call to
    _nodewords_get_pages_data(), nodewords_load_tags() gets called for
    every single page in the nodewords_custom table. For a site with 1000
    entries in the nodewords_custom table this results in an extra 1000
    queries on every page load, just to find out if a path in
    nodewords_custom matches the current path.
    
    Loading just the content of the nodewords_custom table when
    determining path matches will greatly cut down on unneeded queries.

diff --git a/nodewords.module b/nodewords.module
index 5a62035..8841e65 100644
--- a/nodewords.module
+++ b/nodewords.module
@@ -1232,11 +1232,9 @@ function _nodewords_detect_type_and_id() {
     return array('type' => NODEWORDS_TYPE_FRONTPAGE, 'id' => 0);
   }
 
-  foreach (_nodewords_get_custom_pages_data() as $page) {
-    $path = $page->path;
-
+  foreach (_nodewords_get_pages_paths() as $pid => $path) {
     if (drupal_match_path($_GET['q'], $path)) {
-      return array('type' => NODEWORDS_TYPE_PAGE, 'id' => $page->pid);
+      return array('type' => NODEWORDS_TYPE_PAGE, 'id' => $pid);
     }
 
     $bool = (
@@ -1245,7 +1243,7 @@ function _nodewords_detect_type_and_id() {
     );
 
     if ($bool) {
-      return array('type' => NODEWORDS_TYPE_PAGE, 'id' => $page->pid);
+      return array('type' => NODEWORDS_TYPE_PAGE, 'id' => $pid);
     }
   }
 
@@ -1297,6 +1295,26 @@ function _nodewords_get_custom_pages_data($id = NULL) {
 }
 
 /**
+ * Answer a listing of page paths.
+ *
+ */
+function _nodewords_get_pages_paths() {
+  static $paths;
+
+  if (!isset($paths)) {
+    $paths = array();
+    $result = db_query("SELECT pid, path FROM {nodewords_custom} ORDER BY weight ASC");
+
+    while ($page = db_fetch_object($result)) {
+      $paths[$page->pid] = $page->path;
+    }
+  }
+
+  return $paths;
+}
+
+
+/**
  * Load the files in the directory "includes" basing on the enabled modules.
  *
  */
