Make WordPress Core

Opened 11 years ago

Last modified 3 weeks ago

#35432 new enhancement

`wp_list_comments()` should perform a more modest fallback comment query

Reported by: boonebgorges Owned by:
Priority: normal Milestone:
Component: Comments Version: 4.4
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description

Previously: #35175, #35356, #8071.

In 4.4, comments_template() was updated so that it no longer queries for every single comment belonging to a post; instead, it only fetches the comments appropriate for the current comment-page. See #8071. This change introduced a bug in wp_list_comments(): since all of the post's comments were no longer available in $wp_query->comments, it was no longer possible to pass custom (read: differing from $wp_query) pagination params to wp_list_comments() and get the proper comments back. We addressed this problem in 4.4.1 by adding a clause to wp_list_comments() that falls back on the old behavior - querying for all of the post's comments - if non-default pagination params are provided. See #35175 [36157].

This fallback solution is obviously not ideal. Querying for all of a post's comments is what #8071 was supposed to fix. wp_list_comments(), like comments_template(), should be able to perform a fallback query that fetches only the required comments.

The necessary logic already exists in comments_template(), so this may be a relatively simple matter of porting the functionality over, or abstracting it for use in both functions.

cc @smerriman

Change History (1)

This ticket was mentioned in ​PR #14754 on ​WordPress/wordpress-develop by ​@whopiyush.


3 weeks ago
#1

  • Keywords has-patch has-unit-tests added; needs-patch removed

Summary

wp_list_comments() accepts page and per_page arguments, and when they differ from the values in $wp_query it runs its own comment query. That fallback has no number limit: it loads every approved comment on the post, including those of the current user awaiting moderation, and only then lets Walker_Comment paginate the array in memory.

This is the behaviour [36157] introduced as a stopgap for #35175, and it reinstates exactly the problem #8071 set out to solve. A post with 500 comments runs the full 500-row query to render 20 of them, and the cost grows without bound because the remaining pages are just as expensive.

The modest query this ticket asks for already exists in comments_template(), so this ports the same approach rather than inventing a new one.

What this changes

Resolve the page before the query, not after it. wp_list_comments() calculates the effective page later in the function. The fallback now performs that same resolution up front so the query and the walker agree on which comments belong on the page.

Fetch one page. When paging is in effect the fallback passes number and offset, and the walker is told not to paginate a second time, which is the mechanism comments_template() already relies on (it zeroes page and per_page for the same reason).

Keep threading intact. A threaded list paginates by top level comment, and the walker also needs each parent's descendants to render. Requesting hierarchical from WP_Comment_Query produces precisely that: it restricts the limited query to comments whose comment_parent is 0 and then fills in their children. Page sizes therefore stay counted in top level comments, as before.

Paginate over the type being displayed. When type is something other than all, it is passed to the query. Previously pagination always counted the whole set and the type was separated afterwards, so a type of comment on a page of pingbacks could come back empty. The query never carries more than one page of the requested type, and for all the query is unchanged.

The empty page case still needs a total, so it counts instead of fetching. When no page is given and default_comments_page is newest, the last page is the one shown, and its offset cannot be known without a total. That path issues a count query (count, unnumbered, unoffset) and derives the page from it, rather than loading every comment. It is the only path that needs a second query, and it is bounded work.

Behavioural notes

The rendered output is unchanged. A characterisation harness comparing trunk against this patch over 210 combinations of page, per_page, type, threading, reverse_top_level and default_comments_page produces byte-identical comment lists in every case; the only difference is that the fallback query is bounded. reverse_top_level needs no special handling because its window selects the same set as the natural one, so disabling the walker's paging yields the same comments in the same order.

Callers that pass their own $comments array bypass all of this, as before, and the branch that reads $wp_query->comments is untouched.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Sonnet
Used for: test cases, code review

---

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

Note: See TracTickets for help on using tickets.