diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index e4b674f..544fabd 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -238,40 +238,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
@@ -484,7 +450,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 b903250..140e418 100644
--- a/core/modules/comment/src/CommentViewBuilder.php
+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -17,6 +17,7 @@
 use Drupal\Core\Entity\EntityViewBuilder;
 use Drupal\Core\Entity\Entity\EntityViewDisplay;
 use Drupal\Core\Language\LanguageManagerInterface;
+use Drupal\Core\Render\Element;
 use Drupal\Core\Url;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -44,10 +45,7 @@ class CommentViewBuilder extends EntityViewBuilder {
    */
   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
     return new static(
-      $entity_type,
-      $container->get('entity.manager'),
-      $container->get('language_manager'),
-      $container->get('csrf_token')
+        $entity_type, $container->get('entity.manager'), $container->get('language_manager'), $container->get('csrf_token')
     );
   }
 
@@ -106,6 +104,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
       $uids[] = $entity->getOwnerId();
     }
     $this->entityManager->getStorage('user')->loadMultiple(array_unique($uids));
+    self::buildDepth($build, $entities);
 
     parent::buildComponents($build, $entities, $displays, $view_mode, $langcode);
 
@@ -167,7 +166,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
         $build[$id]['#attached'] = array();
       }
       $build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer';
-      if ($this->moduleHandler->moduleExists('history') &&  \Drupal::currentUser()->isAuthenticated()) {
+      if ($this->moduleHandler->moduleExists('history') && \Drupal::currentUser()->isAuthenticated()) {
         $build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator';
 
         // Embed the metadata for the comment "new" indicators on this node.
@@ -302,6 +301,47 @@ 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) {
@@ -310,13 +350,12 @@ 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)
-        && $field_definition->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_THREADED;
+      $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']['library'][] = 'comment/drupal.comment.threaded';
-        $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.
@@ -324,8 +363,8 @@ 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']);
       }
     }
   }
@@ -349,18 +388,18 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie
   public static function attachNewCommentsLinkMetadata(array $element, array $context) {
     // Build "X new comments" link metadata.
     $new = \Drupal::service('comment.manager')
-      ->getCountNewComments(entity_load($context['entity_type'], $context['entity_id']));
+        ->getCountNewComments(entity_load($context['entity_type'], $context['entity_id']));
     // Early-return if there are zero new comments for the current user.
     if ($new === 0) {
       return $element;
     }
     $entity = \Drupal::entityManager()
-      ->getStorage($context['entity_type'])
-      ->load($context['entity_id']);
+        ->getStorage($context['entity_type'])
+        ->load($context['entity_id']);
     $field_name = $context['field_name'];
     $page_number = \Drupal::entityManager()
-      ->getStorage('comment')
-      ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity);
+        ->getStorage('comment')
+        ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity);
     $query = $page_number ? array('page' => $page_number) : NULL;
 
     // Attach metadata.
@@ -368,7 +407,7 @@ public static function attachNewCommentsLinkMetadata(array $element, array $cont
       $context['entity_type'] => [
         $context['field_name'] => [
           $context['entity_id'] => [
-            'new_comment_count' => (int)$new,
+            'new_comment_count' => (int) $new,
             'first_new_comment_link' => \Drupal::urlGenerator()->generateFromPath('node/' . $entity->id(), ['query' => $query, 'fragment' => 'new']),
           ],
         ],
diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
index 02c3107..eeb4b3f 100644
--- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
+++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
@@ -167,7 +167,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 bbbce01..f1bd05e 100644
--- a/core/modules/comment/src/Plugin/views/row/Rss.php
+++ b/core/modules/comment/src/Plugin/views/row/Rss.php
@@ -40,10 +40,6 @@ public function preRender($result) {
     }
 
     $this->comments = entity_load_multiple('comment', $cids);
-    foreach ($this->comments as $comment) {
-      $comment->depth = count(explode('.', $comment->getThread())) - 1;
-    }
-
   }
 
   /**
