peformance: Cache user mentions for multiple PMs.

It's slightly annoying to plumb Optional[MentionBackend]
down the stack, but it's a one-time change.

I tried to make the cache code relatively unobtrusive
for the single-message use case.

We should be able to eliminate redundant stream queries
using similar techniques.

I considered caching at the level of rendering the message
itself, but this involves nearly as much plumbing, and
you have to account for the fact that several users on
your realm may have distinct default languages (French,
Spanish, Russian, etc.), so you would not eliminate as
many query hops. Also, if multiple streams were involved,
users would get slightly different messages based on
their prior subscriptions.
This commit is contained in:
Steve Howell
2021-12-29 16:54:08 +00:00
committed by Tim Abbott
parent c6448263c3
commit c4bd4496dd
4 changed files with 71 additions and 21 deletions

View File

@@ -50,7 +50,7 @@ from zerver.lib.exceptions import (
OrganizationOwnerRequired,
ResourceNotFoundError,
)
from zerver.lib.mention import silent_mention_syntax_for_user
from zerver.lib.mention import MentionBackend, silent_mention_syntax_for_user
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
from zerver.lib.retention import parse_message_retention_days
@@ -603,6 +603,9 @@ def send_messages_for_new_subscribers(
newly_created_stream_names = {s.name for s in created_streams}
realm = user_profile.realm
mention_backend = MentionBackend(realm.id)
# Inform the user if someone else subscribed them to stuff,
# or if a new stream was created with the "announce" option.
notifications = []
@@ -633,10 +636,11 @@ def send_messages_for_new_subscribers(
notifications.append(
internal_prep_private_message(
realm=user_profile.realm,
realm=realm,
sender=sender,
recipient_user=recipient_user,
content=msg,
mention_backend=mention_backend,
)
)