Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1231
diff -u -p -r1.1231 common.inc
--- includes/common.inc	3 Oct 2010 23:32:55 -0000	1.1231
+++ includes/common.inc	4 Oct 2010 17:28:40 -0000
@@ -2050,6 +2050,11 @@ function format_username($account) {
  *   A string containing a URL to the given path.
  */
 function url($path = NULL, array $options = array()) {
+  static $rewrite;
+  // Check for url rewrites, which can affect performance.
+  if (!isset($rewrite)) {
+    $rewrite = (bool) count(module_implements('url_outbound_alter'));
+  }
   // Merge in defaults.
   $options += array(
     'fragment' => '',
@@ -2072,8 +2077,27 @@ function url($path = NULL, array $option
   // Preserve the original path before altering or aliasing.
   $original_path = $path;
 
-  // Allow other modules to alter the outbound URL and options.
-  drupal_alter('url_outbound', $path, $options, $original_path);
+  // For performance, pass entity data to the alter hook.
+  if (!empty($rewrite)) {
+    if (!isset($options['entity_type'])) {
+      // No great hit here, as this is already cached.
+      $item = menu_get_item($path);
+      // There may be a better way to do this.
+      if (!empty($item['load_functions'])) {
+        $function = current($item['load_functions']);
+        if (substr_count($function, '_load') > 0) {
+          $type = str_replace('_load', '', $function);
+          $info = entity_get_info($type);
+          if (!empty($info)) {
+            $options['entity_type'] = $type;
+            $options['entity'] = current($item['page_arguments']);
+          }
+        }
+      }
+    }
+    // Allow other modules to alter the outbound URL and options.
+    drupal_alter('url_outbound', $path, $options, $original_path);
+  }
 
   if (isset($options['fragment']) && $options['fragment'] !== '') {
     $options['fragment'] = '#' . $options['fragment'];
