discourse/lib/guardian
Alan Guo Xiang Tan 24db6fbb73
PERF: Memoize topic level checks in PostGuardian (#19647)
When loading posts in a topic, the topic level guardian
checks are run multiple times even though all the posts belong to the
same topic. Profiling in production revealed that this accounted for a
significant amount of request time for a user that is not staff or anon.
Therefore, we're optimizing this by adding memoizing the topic level
calls in `PostGuardian`. Speficifally, the result of
`TopicGuardian#can_see_topic?` and `PostGuardian#can_create_post?`
method calls are memoized per topic.

Locally profiling shows a significant improvement for normal users
loading a topic with 100 posts.

Benchmark script command: `ruby script/bench.rb --unicorn --skip-bundle-assets --iterations 100`

Before:

```
topic user:
  50: 114
  75: 117
  90: 122
  99: 209
topic.json user:
  50: 67
  75: 69
  90: 72
  99: 162
```

After:

```
topic user:
  50: 101
  75: 104
  90: 107
  99: 184
topic.json user:
  50: 53
  75: 53
  90: 56
  99: 138
```
2023-01-03 09:00:42 +08:00
..
bookmark_guardian.rb FEATURE: Promote polymorphic bookmarks to default and migrate (#16729) 2022-05-23 10:07:15 +10:00
category_guardian.rb FIX: access to category chat only when user can create post (#19488) 2022-12-19 11:35:28 +11:00
ensure_magic.rb DEV: enable frozen string literal on all files 2019-05-13 09:31:32 +08:00
group_guardian.rb DEV: Remove usages of enable_personal_messages (#18437) 2022-10-05 10:50:20 +10:00
post_guardian.rb PERF: Memoize topic level checks in PostGuardian (#19647) 2023-01-03 09:00:42 +08:00
post_revision_guardian.rb DEV: enable frozen string literal on all files 2019-05-13 09:31:32 +08:00
tag_guardian.rb DEV: TODO followups (#18936) 2022-11-09 07:48:05 +10:00
topic_guardian.rb SECURITY: Restrict unlisted topic creation (#19259) 2022-12-01 10:26:35 +00:00
user_guardian.rb FEATURE: users with no posts shouldn't able to edit username after the allowed period. (#17583) 2022-07-21 04:46:15 +05:30