For regular users, every refresh of the notification badge and every inbox page compared their chat notifications against every chat they could see, and opened the saved copy of every shared chat on the instance to find the ones set to "Allow replies". That cost grew with the size of the instance, to about 70 ms per badge refresh on SQLite and 50 ms on Postgres at 50,000 chats. Now the user's own chats, the chats in their folders and the chats shared with them are looked up separately, and only the saved copies of chats shared with that user get opened. Who sees which notification is unchanged, comment notifications on chats included.
<table>
<tr><th>Regular user, median ms per request</th><th>SQLite before</th><th>SQLite after</th><th>Postgres before</th><th>Postgres after</th></tr>
<tr><td>Badge refresh, 5,000 chats</td><td>15.0</td><td>11.7</td><td>20.8</td><td>17.5</td></tr>
<tr><td>Inbox first page, 5,000 chats</td><td>19.0</td><td>15.1</td><td>27.8</td><td>23.9</td></tr>
<tr><td>Badge refresh, 50,000 chats</td><td>72.1</td><td>23.9</td><td>51.0</td><td>26.2</td></tr>
<tr><td>Inbox first page, 50,000 chats</td><td>78.3</td><td>26.9</td><td>57.3</td><td>31.9</td></tr>
<tr><td>Chat comment mentioning 5 people, 50,000 chats</td><td>312.5</td><td>43.6</td><td>159.3</td><td>88.2</td></tr>
</table>
Fixes#35066