diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 5ce3ba9..f3d29d6 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -414,40 +414,6 @@ function comment_node_view_alter(array &$build, EntityInterface $node, EntityVie
 }
 
 /**
- * Calculates the indentation level of each comment in a comment thread.
- *
- * This function loops over an array representing a comment thread. For each
- * comment, the function calculates the indentation level and saves it in the
- * 'divs' property of the comment object.
- *
- * @param array $comments
- *   An array of comment objects, keyed by comment ID.
- */
-function comment_prepare_thread(&$comments) {
-  // A counter that helps track how indented we are.
-  $divs = 0;
-
-  foreach ($comments as $key => &$comment) {
-    // The $divs element instructs #prefix whether to add an indent div or
-    // close existing divs (a negative value).
-    $comment->depth = count(explode('.', $comment->getThread())) - 1;
-    if ($comment->depth > $divs) {
-      $comment->divs = 1;
-      $divs++;
-    }
-    else {
-      $comment->divs = $comment->depth - $divs;
-      while ($comment->depth < $divs) {
-        $divs--;
-      }
-    }
-  }
-
-  // The final comment must close up some hanging divs
-  $comments[$key]->divs_final = $divs;
-}
-
-/**
  * Generates an array for rendering a comment.
  *
  * @param \Drupal\comment\CommentInterface $comment
@@ -661,7 +627,6 @@ function comment_node_update_index(EntityInterface $node, $langcode) {
         $comments = \Drupal::entityManager()->getStorage('comment')
           ->loadThread($node, $field_name, $mode, $comments_per_page);
         if ($comments) {
-          comment_prepare_thread($comments);
           $build[] = \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple($comments);
         }
       }
diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php
index 6a4760b..838168e 100644
--- a/core/modules/comment/src/CommentViewBuilder.php
+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -16,6 +16,7 @@
 use Drupal\Core\Entity\EntityViewBuilder;
 use Drupal\entity\Entity\EntityViewDisplay;
 use Drupal\Core\Language\LanguageManagerInterface;
+use Drupal\Core\Render\Element;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
@@ -88,6 +89,8 @@ public function buildComponents(array &$build, array $entities, array $displays,
     }
     $this->entityManager->getStorage('user')->loadMultiple(array_unique($uids));
 
+    self::buildDepth($build, $entities);
+
     parent::buildComponents($build, $entities, $displays, $view_mode, $langcode);
 
     // Load all the entities that have comments attached.
@@ -281,6 +284,48 @@ protected static function buildLinks(CommentInterface $entity, EntityInterface $
   }
 
   /**
+   * Calculates the indentation level of each comment in a comment thread.
+   */
+  protected static function buildDepth(array &$build, array $comments) {
+    // '#comment_divs' properties are set to 1, 0 or a negative value,
+    // instructing $this->alterBuild() to add or close divs for indentation.
+
+    // Do this only once for a render array:
+    if (empty($build['#comment_divs_built'])) {
+
+      $current_depth = 0;
+      $current_key = '';
+      foreach (Element::children($build, TRUE) as $key) {
+        if (isset($comments[$key])) {
+          $comment_depth = count(explode('.', $comments[$key]->getThread())) - 1;
+          if ($comment_depth > $current_depth) {
+            // Set 1 to indent this from the previous one (its parent). Set only
+            // one extra level of indenting even if the difference in depth is
+            // higher.
+            $build[$key]['#comment_divs'] = 1;
+            $current_depth++;
+          }
+          else {
+            // Set zero if this comment is on the same level as the previous one
+            // or (negative) amount of divs to close to get to this comment's
+            // indent level.
+            $build[$key]['#comment_divs'] = $comment_depth - $current_depth;
+            $current_depth = $comment_depth;
+          }
+
+          $current_key = $key;
+        }
+      }
+      if ($current_key) {
+        // The final comment must close up some hanging divs.
+        $build[$current_key]['#comment_divs_final'] = $current_depth;
+      }
+
+      $build['#comment_divs_built'] = TRUE;
+    }
+  }
+
+  /**
    * {@inheritdoc}
    */
   protected function alterBuild(array &$build, EntityInterface $comment, EntityViewDisplayInterface $display, $view_mode, $langcode = NULL) {
@@ -289,13 +334,14 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie
       $prefix = '';
       $commented_entity = $comment->getCommentedEntity();
       $field_definition = $this->entityManager->getFieldDefinitions($commented_entity->getEntityTypeId(), $commented_entity->bundle())[$comment->getFieldName()];
-      $is_threaded = isset($comment->divs)
+      $is_threaded = isset($build['#comment_divs'])
+        && is_numeric($build['#comment_divs'])
         && $field_definition->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_THREADED;
 
       // Add indentation div or close open divs as needed.
       if ($is_threaded) {
         $build['#attached']['css'][] = drupal_get_path('module', 'comment') . '/css/comment.theme.css';
-        $prefix .= $comment->divs <= 0 ? str_repeat('</div>', abs($comment->divs)) : "\n" . '<div class="indented">';
+        $prefix .= $build['#comment_divs'] <= 0 ? str_repeat('</div>', (int) abs($build['#comment_divs'])) : "\n" . '<div class="indented">';
       }
 
       // Add anchor for each comment.
@@ -303,8 +349,10 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie
       $build['#prefix'] = $prefix;
 
       // Close all open divs.
-      if ($is_threaded && !empty($comment->divs_final)) {
-        $build['#suffix'] = str_repeat('</div>', $comment->divs_final);
+      if ($is_threaded
+          && !empty($build['#comment_divs_final'])
+          && is_numeric($build['#comment_divs_final'])) {
+        $build['#suffix'] = str_repeat('</div>', (int) $build['#comment_divs_final']);
       }
     }
   }
diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
index e5ad723..2756772 100644
--- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
+++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
@@ -156,7 +156,6 @@ public function viewElements(FieldItemListInterface $items) {
         $comments_per_page = $comment_settings['per_page'];
         $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'));
         if ($comments) {
-          comment_prepare_thread($comments);
           $build = $this->viewBuilder->viewMultiple($comments);
           $build['pager']['#theme'] = 'pager';
           if ($this->getSetting('pager_id')) {
diff --git a/core/modules/comment/src/Plugin/views/row/Rss.php b/core/modules/comment/src/Plugin/views/row/Rss.php
index d24eefe..b287f1c 100644
--- a/core/modules/comment/src/Plugin/views/row/Rss.php
+++ b/core/modules/comment/src/Plugin/views/row/Rss.php
@@ -53,20 +53,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
     );
   }
 
-  public function preRender($result) {
-    $cids = array();
-
-    foreach ($result as $row) {
-      $cids[] = $row->cid;
-    }
-
-    $this->comments = entity_load_multiple('comment', $cids);
-    foreach ($this->comments as $comment) {
-      $comment->depth = count(explode('.', $comment->getThread())) - 1;
-    }
-
-  }
-
   /**
    * Return the main options, which are shown in the summary title
    *
