Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1017
diff -u -p -r1.1017 common.inc
--- includes/common.inc	13 Oct 2009 21:16:42 -0000	1.1017
+++ includes/common.inc	14 Oct 2009 05:04:36 -0000
@@ -2569,6 +2569,8 @@ function drupal_attributes(array $attrib
  */
 function l($text, $path, array $options = array()) {
   global $language_url;
+  static $bootstrapped = FALSE;
+  static $themeable = FALSE;
 
   // Merge in defaults.
   $options += array(
@@ -2588,7 +2590,28 @@ 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>';
+  $content = $options['html'] ? $text : check_plain($text);
+  $href = check_plain(url($path, $options));
+
+  // Determine if rendering of the link is to be done with a theme function
+  // or the inline default. Can't theme until bootstrapped. Once bootstrapped,
+  // check if a module or theme has registered a 'link' theme hook.
+  if (!$bootstrapped && (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL)) {
+    $bootstrapped = TRUE;
+    drupal_theme_initialize();
+    $registry = theme_get_registry();
+    if (isset($registry['link'])) {
+      $themeable = TRUE;
+    }
+  }
+  if ($themeable) {
+    return theme('link', array('content' => $content, 'href' => $href, 'attributes' => $options['attributes'], 'path' => $path, 'options' => $options));
+  }
+  else {
+    // Since l() is called very often during a single page request, inline
+    // instead of registering a default theme_link() function.
+    return '<a href="' . $href . '"' . drupal_attributes($options['attributes']) . '>' . $content . '</a>';
+  }
 }
 
 /**
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.84
diff -u -p -r1.84 common.test
--- modules/simpletest/tests/common.test	13 Oct 2009 16:38:43 -0000	1.84
+++ modules/simpletest/tests/common.test	14 Oct 2009 05:04:37 -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',
