PERF: Admin plugin preload settings routes (#31215)

Followup 503f9b6f02ac5c4918d41611848c886b8755e5a0

This previous commit introduced an autogenerated
settings route for every plugin with more than one
setting defined. Plugins with only one setting
only have enabled_site_settings defined, which are
handled using the toggle in the admin plugin list,
so we don't need a dedicated setting page for them.

However in production this introduced a performance
issue, since we were looking through SiteSetting.all_settings
for every plugin, which could be quite slow in some
cases especially on our hosting.

Instead, we already have all the plugin settings cached
inside `SiteSetting.plugins`. We can instead use this to
count how many settings the plugin has, then if there is > 1
for a plugin we use the settings route. This is a much faster lookup
than
searching through SiteSetting.all_settings.
This commit is contained in:
Martin Brennan
2025-02-07 11:23:43 +10:00
committed by GitHub
parent 284e708e67
commit 52a50f1028
3 changed files with 88 additions and 9 deletions

View File

@ -123,8 +123,7 @@ class Plugin::Instance
route = self.admin_route
if route.blank?
return if !any_settings?
return if !any_settings? || has_only_enabled_setting?
route = default_admin_route
end
@ -137,11 +136,15 @@ class Plugin::Instance
end
def any_settings?
return false if !configurable?
configurable? && plugin_settings.values.length.positive?
end
SiteSetting
.all_settings(filter_plugin: self.name)
.any? { |s| s[:setting] != @enabled_site_setting }
def has_only_enabled_setting?
any_settings? && plugin_settings.values.one?
end
def plugin_settings
@plugin_settings ||= SiteSetting.plugins.select { |_, plugin_name| plugin_name == self.name }
end
def configurable?