Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1031
diff -u -p -r1.1031 common.inc
--- includes/common.inc	27 Oct 2009 19:29:12 -0000	1.1031
+++ includes/common.inc	28 Oct 2009 17:45:01 -0000
@@ -2555,6 +2555,7 @@ function drupal_attributes(array $attrib
  */
 function l($text, $path, array $options = array()) {
   global $language_url;
+  static $themeable = NULL;
 
   // Merge in defaults.
   $options += array(
@@ -2574,7 +2575,32 @@ function l($text, $path, array $options 
     $options['attributes']['title'] = strip_tags($options['attributes']['title']);
   }
 
-  return '<a href="' . check_plain(url($path, $options)) . '"' . drupal_attributes($options['attributes']) . '>' . ($options['html'] ? $text : check_plain($text)) . '</a>';
+  // Determine if rendering of the link is to be done with a theme function
+  // or the inline default. Inline is faster, but if the theme system has been
+  // loaded and a module or theme has registered a 'link' theme hook, then
+  // use it. Preliminary benchmarks indicate that using a theme function can
+  // slow down the l() function by 10% or more, and that some of the link-heavy
+  // Drupal pages can spend more than 10% of the total page request time in the
+  // l() function, so modules and themes should only register a 'link' theme
+  // hook if the need to customize link markup is worth slowing down pages by
+  // up to 1% or more.
+  if (!isset($themeable) && function_exists('theme')) {
+    drupal_theme_initialize();
+    $registry = theme_get_registry();
+    $themeable = isset($registry['link']);
+  }
+  if ($themeable) {
+    return theme('link', array(
+      'content' => ($options['html'] ? $text : check_plain($text)), 
+      'href' => check_plain(url($path, $options)), 
+      'attributes' => $options['attributes'], 
+      'path' => $path, 
+      'options' => $options,
+    ));
+  }
+  else {
+    return '<a href="' . check_plain(url($path, $options)) . '"' . drupal_attributes($options['attributes']) . '>' . ($options['html'] ? $text : check_plain($text)) . '</a>';
+  }
 }
 
 /**
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.85
diff -u -p -r1.85 common.test
--- modules/simpletest/tests/common.test	27 Oct 2009 19:29:12 -0000	1.85
+++ modules/simpletest/tests/common.test	28 Oct 2009 17:45:02 -0000
@@ -57,7 +57,7 @@ class DrupalAlterTestCase extends Drupal
 /**
  * Tests for URL generation functions.
  */
-class CommonURLUnitTest extends DrupalUnitTestCase {
+class CommonURLUnitTest extends DrupalWebTestCase {
   public static function getInfo() {
     return array(
       'name' => 'URL generation tests',
