Fix 'hot' sort intermediate value out-of-bounds.

Fixes #321. Due to the extremely large quantity of comments on a
typical Motte post, 'hot' sorting logic underflowed an intermediate
value. Roughly:

  |(-1 000 000) * (103 [votes] + 1 + 2723 [comments] / 1)| > 2^31

We resolve this by reducing the coefficient from 1e6 to 1e5, which
reduces precision of intermediate calculations somewhat, and by
dividing #comments by 10 rather than 1, which better matches Motte
user behavior regardless: Users comment much more often than vote.
This buys us two orders of magnitude more headroom before out-of-
bounds. Shouldn't be an issue until the CW thread reaches ~200k
comments.
This commit is contained in:
TLSM 2022-09-12 08:06:24 -04:00 committed by Ben Rog-Wilhelm
parent ccabc8b881
commit 716f078e81

View file

@ -340,11 +340,13 @@ def frontlist(v=None, sort='new', page=1, t="all", ids_only=True, ccmode="false"
if not (v and v.shadowbanned): if not (v and v.shadowbanned):
posts = posts.join(User, User.id == Submission.author_id).filter(User.shadowbanned == None) posts = posts.join(User, User.id == Submission.author_id).filter(User.shadowbanned == None)
num = 1
if sort == "hot": if sort == "hot":
ti = int(time.time()) + 3600 ti = int(time.time()) + 3600
posts = posts.order_by(-1000000*(Submission.realupvotes + 1 + Submission.comment_count/num + (func.length(Submission.body_html)-func.length(func.replace(Submission.body_html,'</a>','')))/4)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc()) posts = posts.order_by(
-100000
* (Submission.realupvotes + 1 + Submission.comment_count / 10)
/ (func.power((ti - Submission.created_utc) / 1000, 1.23)),
Submission.created_utc.desc())
elif sort == "bump": elif sort == "bump":
posts = posts.filter(Submission.comment_count > 1).order_by(Submission.bump_utc.desc(), Submission.created_utc.desc()) posts = posts.filter(Submission.comment_count > 1).order_by(Submission.bump_utc.desc(), Submission.created_utc.desc())
elif sort == "new": elif sort == "new":