From 5fe3002e76994812ec79e39933b40853176ed493 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Mon, 3 Aug 2020 14:48:51 +0100 Subject: [PATCH] Cache the SVG icon HTML fragment The `{% icons %}` tag runs on every admin page render, and without caching, this will perform a template fetch for every .svg file - which is exceptionally slow on environments with shared filesystems (e.g. Vagrant). Adding this cache reduces the runtime of `wagtail.admin.tests.pages.test_create_page` from 89 to 57 seconds. --- wagtail/admin/templatetags/wagtailadmin_tags.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/wagtail/admin/templatetags/wagtailadmin_tags.py b/wagtail/admin/templatetags/wagtailadmin_tags.py index 72657623c3..7b31f11e48 100644 --- a/wagtail/admin/templatetags/wagtailadmin_tags.py +++ b/wagtail/admin/templatetags/wagtailadmin_tags.py @@ -547,11 +547,18 @@ def icon(name=None, class_name='icon', title=None, wrapped=False): } -@register.inclusion_tag("wagtailadmin/shared/icons.html") +_icons_html = None + + +@register.simple_tag def icons(): - icon_hooks = hooks.get_hooks('register_icons') - icons = sorted(itertools.chain.from_iterable(hook([]) for hook in icon_hooks)) - return {'icons': icons} + global _icons_html + if _icons_html is None: + icon_hooks = hooks.get_hooks('register_icons') + icons = sorted(itertools.chain.from_iterable(hook([]) for hook in icon_hooks)) + _icons_html = render_to_string("wagtailadmin/shared/icons.html", {'icons': icons}) + + return _icons_html @register.filter()