Index: modules/field/modules/text/text.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.module,v
retrieving revision 1.8
diff -u -p -r1.8 text.module
--- modules/field/modules/text/text.module	27 May 2009 18:33:56 -0000	1.8
+++ modules/field/modules/text/text.module	28 May 2009 09:42:40 -0000
@@ -110,18 +110,60 @@ function text_field_validate($obj_type, 
   }
 }
 
+/**
+ * Implement hook_field_load().
+ *
+ * Where possible, generate the sanitized version of each field early so that
+ * it is cached in the field cache. This avoids looking up from the filter cache
+ * separately.
+ * @see text_field_sanitize().
+ */
+function text_field_load($obj_type, $objects, $field, $instances, &$items) {
+  global $language;
+
+  foreach ($objects as $id => $object) {
+    foreach ($items[$id] as $delta => $item) {
+      // TODO D7 : this code is really node-related.
+      if (!empty($instances[$id]['settings']['text_processing'])) {
+        $check = is_null($object) || (isset($object->build_mode) && $object->build_mode == NODE_BUILD_PREVIEW);
+
+        // Only process items with a cacheable format, the rest will be
+        // handled by text_field_sanitize().
+        if (filter_format_allowcache($item['format'])) {
+          $text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language->language, $check, FALSE) : '';
+        }
+      }
+      else {
+        $text = check_plain($item['value']);
+      }
+      if (isset($text)) {
+        $items[$id][$delta]['safe'] = $text;
+      }
+    }
+  }
+}
+
+/**
+ * Implement hook_field_sanitize().
+ *
+ * @see text_field_load()
+ */
 function text_field_sanitize($obj_type, $object, $field, $instance, &$items) {
   global $language;
   foreach ($items as $delta => $item) {
-    // TODO D7 : this code is really node-related.
-    if (!empty($instance['settings']['text_processing'])) {
-      $check = is_null($object) || (isset($object->build_mode) && $object->build_mode == NODE_BUILD_PREVIEW);
-      $text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language->language, $check) : '';
-    }
-    else {
-      $text = check_plain($item['value']);
+    // Only sanitize items which were not already processed inside
+    // text_field_load(), i.e. items with uncacheable text formats.
+    if (!isset($items[$delta]['safe'])) {
+      // TODO D7 : this code is really node-related.
+      if (!empty($instance['settings']['text_processing'])) {
+        $check = is_null($object) || (isset($object->build_mode) && $object->build_mode == NODE_BUILD_PREVIEW);
+        $text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language->language, $check) : '';
+      }
+      else {
+        $text = check_plain($item['value']);
+      }
+      $items[$delta]['safe'] = $text;
     }
-    $items[$delta]['safe'] = $text;
   }
 }
 
Index: modules/filter/filter.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v
retrieving revision 1.258
diff -u -p -r1.258 filter.module
--- modules/filter/filter.module	28 May 2009 02:40:45 -0000	1.258
+++ modules/filter/filter.module	28 May 2009 09:42:41 -0000
@@ -423,15 +423,19 @@ function filter_list_format($format) {
  *   should specify $check = FALSE when viewing other people's content. When
  *   showing content that is not (yet) stored in the database (eg. upon preview),
  *   set to TRUE so the user's permissions are checked.
+ * @param $cache
+ *   Boolean whether to cache the filtered output in the {cache_filter} table.
+ *   The caller may set this to FALSE when the output is already cached
+ *   elsewhere to avoid duplicate cache lookups and storage.
  */
-function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $langcode = '', $check = TRUE) {
+function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $langcode = '', $check = TRUE, $cache = TRUE) {
   // When $check = TRUE, do an access check on $format.
   if (isset($text) && (!$check || filter_access($format))) {
     $format = filter_resolve_format($format);
 
     // Check for a cached version of this piece of text.
     $cache_id = $format . ':' . $langcode . ':' . md5($text);
-    if ($cached = cache_get($cache_id, 'cache_filter')) {
+    if ($cache && $cached = cache_get($cache_id, 'cache_filter')) {
       return $cached->data;
     }
 
@@ -453,7 +457,7 @@ function check_markup($text, $format = F
     }
 
     // Store in cache with a minimum expiration time of 1 day.
-    if (filter_format_allowcache($format)) {
+    if ($cache && filter_format_allowcache($format)) {
       cache_set($cache_id, $text, 'cache_filter', REQUEST_TIME + (60 * 60 * 24));
     }
   }
