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..2f37c3b 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;
 
@@ -106,6 +107,8 @@ 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);
 
@@ -301,6 +304,46 @@ 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}
    */
@@ -310,13 +353,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']['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 +368,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 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;
-    }
-
   }
 
   /**
