Many of our clients don’t like the paged comments feature of WordPress as they like to have all the keywords occurring in the comments present on our article page and not spread across multiple pages. Keeping thousands of fresh comments on the page with every possible combination of keywords helps them rank at the top of Google for very competitive product categories.
Our front-end comment moderation plugin Thoughtful Comments includes comment caching feature, this article describes why that is important even if you are caching your site HTML: Improve your WordPress speed by Caching Comments.
However the downside is that it doesn’t work for administrators and that the cache has to be purged and rebuilt when comments are added or removed. It’s also usually slows down logged in users as we move more and more of our clients over to a membership or at least logged in user model.
Quest of speeding up the comments display
We sought alternative ways of speeding up the comments by simplifying the PHP code required to display each comment. Here are all the filters running on comment text and how much they contribute to the total PHP processing time for comments.
|wptexturize||Converts regular quotes to fancy quotes etc.||No, it’s eyecandy|
|convert_chars||Escapes HTML special chars.||Yes|
|make_clickable||Makes the plain text links clickable.||Yes|
|force_balance_tags||Makes sure HTML in comment text won’t break the page layout.||Yes|
|convert_smilies||Converts text smileys to Emojis||No!|
|wpautop||Puts in paragraph breaks.||Yes|
In our test enviromnent 781 comments take ~1.52 seconds to display. When all of the above filters are disabled, it’s ~1.35 seconds (12% gain) and when the absolutely needed filters are kept (without them the comment wouldn’t appear properly – no links or line breaks) it’s ~1.43 seconds (6% gain). So that’s not much.
Comment author, date, etc.
Big part of comment HTML are the meta information. We checked how much could be saved on each by removing that part of markup and found the issues:
|Information||Description of the issue||Estimate of savings|
|Comment Date||It’s called 3 times for each comment with a lot of formatting going on||13%|
|Comment Link||The link is mostly just the ID of the element||12%|
|Edit Link||Comment editing permissions are checked for each comment||7%|
With all of these improvements and reduced comment text filtering 781 comments on our test post load in ~ 0.986 seconds (35% speed increase).
These improvements could be implemented in a form of a simplified comment display callback function – Documentation on WordPress.org.
The speed benefits of the above are clear, and they do help. We would still like to make the comment load not 35% faster, but lightning fast.
We tried to cache each comment individually rather than caching the whole list of comments. Advantages of this approach would be:
- cache doesn’t have to be cleared when
- posting a new comment
- removing a comment
- approving a comment
- and so on
We would like this new function to work as seamless as possible, so we checked where it could be stored – here is are the average speed values for comparison of different comment caching options.
|Caching mode||First load [s]||Repeated loads [ms]|
|custom database table||2.8||28|
|individual file for each comment||2.014||42|
- Single file – this is that’s currently done in FV Thoughtful Comments
- Comment meta – much slower than caching into a file, unfortunately. We confirmed this on two different servers with different Apache + MySQL configs
- Custom database table – faster than using comment meta, but all the database inserts still take its toll. Also, not sure why the display here would be faster than the other caching models, but we were able to replicate the behavior or different servers
- Individual file for each comment – unfortunately still slower than using a single cache file. We are not sure about all the implications of this for multiple webhead website serving, but this is probably the solution for us.
Other interesting options might be using Memcache or Redis. However this kind of cached data should have a very long TTL. So the most resilient yet very high performance model is to cache each comment separately. Which is what we are going to do in the latest Thoughtful Comments
Martin graduated as an engineer in Computer Science from Slovak Technical University in Bratislava. He grew up in Liptovský Mikuláš in northern Slovakia next to the beautiful Tatra mountains. He is the developer behind our FV Player.