Skip to content

Commit 076b2f9

Browse files
committed
perf: Memoize translations in page tree rendering
Cache repeated translation lookups in a frozen hash at the class level. Translations like "Your user role does not allow you to edit this page" were being looked up 6 times per page (6000 times for 1000 pages). Combined with the URL optimization, render time for 1000 pages improves from ~725ms to ~330ms (approximately 54% faster).
1 parent 3516ca1 commit 076b2f9

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

app/components/alchemy/admin/page_node.html.erb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</span>
3737
<% else %>
3838
<sl-tooltip
39-
content="<%= Alchemy.t("Your user role does not allow you to edit this page") %>"
39+
content="<%= t(:cannot_edit_page) %>"
4040
class="like-hint-tooltip"
4141
placement="bottom-start"
4242
>
@@ -51,7 +51,7 @@
5151
<%= link_to(
5252
@page.name,
5353
url(:edit_page),
54-
title: Alchemy.t(:edit_page),
54+
title: t(:edit_page),
5555
class: "sitemap_pagename_link"
5656
) %>
5757
<% else %>
@@ -88,12 +88,12 @@
8888

8989
<div class="sitemap_right_tools">
9090
<% if can?(:info) %>
91-
<sl-tooltip content="<%= Alchemy.t(:page_infos) %>">
91+
<sl-tooltip content="<%= t(:page_infos) %>">
9292
<%= link_to_dialog(
9393
render_icon("information"),
9494
url(:info_page),
9595
{
96-
title: Alchemy.t(:page_infos),
96+
title: t(:page_infos),
9797
size: "520x290"
9898
},
9999
class: "icon_button"
@@ -102,7 +102,7 @@
102102
<% else %>
103103
<div class="sitemap_tool disabled">
104104
<sl-tooltip
105-
content="<%= Alchemy.t("Your user role does not allow you to edit this page") %>"
105+
content="<%= t(:cannot_edit_page) %>"
106106
class="like-hint-tooltip"
107107
placement="bottom-start"
108108
>
@@ -112,12 +112,12 @@
112112
<% end %>
113113

114114
<% if can?(:configure) %>
115-
<sl-tooltip content="<%= Alchemy.t(:edit_page_properties) %>">
115+
<sl-tooltip content="<%= t(:edit_page_properties) %>">
116116
<%= link_to_dialog(
117117
render_icon("settings-3"),
118118
url(:configure_page),
119119
{
120-
title: Alchemy.t(:edit_page_properties),
120+
title: t(:edit_page_properties),
121121
size: "500x680"
122122
},
123123
class: "icon_button"
@@ -126,7 +126,7 @@
126126
<% else %>
127127
<div class="sitemap_tool disabled">
128128
<sl-tooltip
129-
content="<%= Alchemy.t("Your user role does not allow you to edit this page") %>"
129+
content="<%= t(:cannot_edit_page) %>"
130130
class="like-hint-tooltip"
131131
placement="bottom-start"
132132
>
@@ -136,7 +136,7 @@
136136
<% end %>
137137

138138
<% if can?(:copy) %>
139-
<sl-tooltip content="<%= Alchemy.t(:copy_page) %>">
139+
<sl-tooltip content="<%= t(:copy_page) %>">
140140
<%= button_to(
141141
render_icon("file-copy"),
142142
url(:clipboard_insert),
@@ -147,7 +147,7 @@
147147
<% else %>
148148
<div class="sitemap_tool disabled">
149149
<sl-tooltip
150-
content="<%= Alchemy.t("Your user role does not allow you to edit this page") %>"
150+
content="<%= t(:cannot_edit_page) %>"
151151
class="like-hint-tooltip"
152152
placement="bottom-start"
153153
>
@@ -157,18 +157,18 @@
157157
<% end %>
158158

159159
<% if can?(:destroy) %>
160-
<sl-tooltip content="<%= Alchemy.t(:delete_page) %>">
160+
<sl-tooltip content="<%= t(:delete_page) %>">
161161
<%= link_to_confirm_dialog(
162162
render_icon("delete-bin-2"),
163-
Alchemy.t(:confirm_to_delete_page),
163+
t(:confirm_to_delete_page),
164164
url(:page),
165165
class: "icon_button"
166166
) %>
167167
</sl-tooltip>
168168
<% else %>
169169
<div class="sitemap_tool disabled">
170170
<sl-tooltip
171-
content="<%= Alchemy.t("Your user role does not allow you to edit this page") %>"
171+
content="<%= t(:cannot_edit_page) %>"
172172
class="like-hint-tooltip"
173173
placement="bottom-start"
174174
>
@@ -178,12 +178,12 @@
178178
<% end %>
179179

180180
<% if can?(:create) %>
181-
<sl-tooltip content="<%= Alchemy.t(:create_page) %>">
181+
<sl-tooltip content="<%= t(:create_page) %>">
182182
<%= link_to_dialog(
183183
render_icon(:add),
184184
url(:new_child_page),
185185
{
186-
title: Alchemy.t(:create_page),
186+
title: t(:create_page),
187187
size: "340x165",
188188
overflow: true
189189
},
@@ -193,7 +193,7 @@
193193
<% else %>
194194
<div class="sitemap_tool disabled">
195195
<sl-tooltip
196-
content="<%= Alchemy.t("Your user role does not allow you to edit this page") %>"
196+
content="<%= t(:cannot_edit_page) %>"
197197
class="like-hint-tooltip"
198198
placement="bottom-start"
199199
>

app/components/alchemy/admin/page_node.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def url(key)
2525
self.class.routes[key].sub(PAGE_ID, page.id.to_s)
2626
end
2727

28+
# Memoized translation lookup
29+
def t(key)
30+
self.class.translations[key]
31+
end
32+
2833
PAGE_ID = "__ID__"
2934

3035
class << self
@@ -45,6 +50,20 @@ def routes
4550
}.freeze
4651
end
4752
end
53+
54+
# Translations - computed once, reused for all pages
55+
def translations
56+
@_translations ||= {
57+
cannot_edit_page: Alchemy.t("Your user role does not allow you to edit this page"),
58+
edit_page: Alchemy.t(:edit_page),
59+
page_infos: Alchemy.t(:page_infos),
60+
edit_page_properties: Alchemy.t(:edit_page_properties),
61+
copy_page: Alchemy.t(:copy_page),
62+
delete_page: Alchemy.t(:delete_page),
63+
confirm_to_delete_page: Alchemy.t(:confirm_to_delete_page),
64+
create_page: Alchemy.t(:create_page)
65+
}.freeze
66+
end
4867
end
4968
end
5069
end

0 commit comments

Comments
 (0)