From c4b541be3a40b7420a44d8be308ed6eafd38e0d8 Mon Sep 17 00:00:00 2001 From: Will Abbott Date: Tue, 9 Jul 2024 22:49:21 +0100 Subject: [PATCH] added caching in cotton component --- dev/example_project/render_load_test.py | 60 ++++++++++++------------ django_cotton/cotton_loader.py | 3 -- django_cotton/templatetags/_component.py | 15 +++++- django_cotton/tests/test_cotton.py | 24 +++++----- 4 files changed, 55 insertions(+), 47 deletions(-) diff --git a/dev/example_project/render_load_test.py b/dev/example_project/render_load_test.py index af238e9..8bef813 100644 --- a/dev/example_project/render_load_test.py +++ b/dev/example_project/render_load_test.py @@ -54,7 +54,7 @@ def benchmark_template_rendering(template_name, iterations=10000): return end_time - start_time, render_to_string(template_name) -def benchmark_template_rendering_alt(template_name, iterations=1000): +def benchmark_template_rendering_alt(template_name, iterations=500): data = list(range(1, iterations)) start_time = time.time() render_to_string(template_name, context={"data": data}) @@ -68,32 +68,32 @@ simple_cotton = benchmark_template_rendering_alt("simple_cotton.html") print(f"Native Django Template: {simple_native} seconds") print(f"Cotton Template: {simple_cotton} seconds") -# -# # Benchmarking each template -# time_native_include, output_native_include = benchmark_template_rendering( -# "benchmarks/native_include.html" -# ) -# time_cotton_include, output_cotton_include = benchmark_template_rendering( -# "cotton/benchmarks/cotton_include.html" -# ) -# -# -# time_native_extends, output_native_extends = benchmark_template_rendering( -# "benchmarks/native_extends.html" -# ) -# time_compiled_cotton, output_compiled_cotton = benchmark_template_rendering( -# "cotton/benchmarks/cotton_compiled.html" -# ) -# time_cotton, output_cotton = benchmark_template_rendering( -# "cotton/benchmarks/cotton.html" -# ) -# -# # Output results -# print("Include, native vs cotton:") -# print(f"Native {{% include %}}: {time_native_include} seconds") -# print(f"Cotton for include:: {time_cotton_include} seconds") -# print("-------") -# print("Block + Extends, native vs cotton:") -# print(f"Native {{% block %}} and {{% extends %}}: {time_native_extends} seconds") -# print(f"Uncompiled Cotton Template: {time_cotton} seconds") -# print(f"Compiled Cotton Template: {time_compiled_cotton} seconds") + +# Benchmarking each template +time_native_include, output_native_include = benchmark_template_rendering( + "benchmarks/native_include.html" +) +time_cotton_include, output_cotton_include = benchmark_template_rendering( + "cotton/benchmarks/cotton_include.html" +) + + +time_native_extends, output_native_extends = benchmark_template_rendering( + "benchmarks/native_extends.html" +) +time_compiled_cotton, output_compiled_cotton = benchmark_template_rendering( + "cotton/benchmarks/cotton_compiled.html" +) +time_cotton, output_cotton = benchmark_template_rendering( + "cotton/benchmarks/cotton.html" +) + +# Output results +print("Include, native vs cotton:") +print(f"Native {{% include %}}: {time_native_include} seconds") +print(f"Cotton for include:: {time_cotton_include} seconds") +print("-------") +print("Block + Extends, native vs cotton:") +print(f"Native {{% block %}} and {{% extends %}}: {time_native_extends} seconds") +print(f"Uncompiled Cotton Template: {time_cotton} seconds") +print(f"Compiled Cotton Template: {time_compiled_cotton} seconds") diff --git a/django_cotton/cotton_loader.py b/django_cotton/cotton_loader.py index 4b328b5..3cd24b2 100755 --- a/django_cotton/cotton_loader.py +++ b/django_cotton/cotton_loader.py @@ -38,11 +38,8 @@ class Loader(BaseLoader): cached_content = self.cache_handler.get_cached_template(cache_key) if cached_content is not None: - print("CACHE HIT", origin.name) return cached_content - print("CACHE MISS", origin.name) - template_string = self._get_template_string(origin.name) compiled_template = self.template_processor.process( template_string, origin.template_name diff --git a/django_cotton/templatetags/_component.py b/django_cotton/templatetags/_component.py index 28ca965..eb183bb 100644 --- a/django_cotton/templatetags/_component.py +++ b/django_cotton/templatetags/_component.py @@ -1,13 +1,24 @@ import ast +from functools import lru_cache from django import template from django.template import Node -from django.template.loader import render_to_string +from django.template.loader import get_template from django.utils.safestring import mark_safe from django_cotton.utils import ensure_quoted +@lru_cache(maxsize=128) +def get_cached_template(template_name): + return get_template(template_name) + + +def render_template(template_name, context): + template = get_cached_template(template_name) + return template.render(context) + + def cotton_component(parser, token): """ Template tag to render a cotton component with dynamic attributes. @@ -81,7 +92,7 @@ class CottonComponentNode(Node): # Reset the component's slots in context to prevent bleeding into sibling components all_slots[self.component_key] = {} - return render_to_string(self.template_path, local_context) + return render_template(self.template_path, local_context) def process_dynamic_attribute(self, value, context): """ diff --git a/django_cotton/tests/test_cotton.py b/django_cotton/tests/test_cotton.py index c484c11..40e1f0e 100644 --- a/django_cotton/tests/test_cotton.py +++ b/django_cotton/tests/test_cotton.py @@ -27,14 +27,14 @@ class InlineTestCase(CottonInlineTestCase): def test_new_lines_in_attributes_are_preserved(self): self.create_template( - "cotton/component.html", + "cotton/preserved.html", """
{{ slot }}
""", ) self.create_template( - "view.html", + "preserved_view.html", """ - """, ) self.create_template( - "view.html", + "hyphens_view.html", """ - + """, ) # Register Url - self.register_url("view/", self.make_view("view.html")) + self.register_url("view/", self.make_view("hyphens_view.html")) # Override URLconf with self.settings(ROOT_URLCONF=self.get_url_conf()): @@ -92,7 +92,7 @@ class InlineTestCase(CottonInlineTestCase): self, ): self.create_template( - "cotton/component.html", + "cotton/cvar_hyphens.html", """ @@ -101,14 +101,14 @@ class InlineTestCase(CottonInlineTestCase): ) self.create_template( - "view.html", + "cvar_hyphens_view.html", """ - + """, ) # Register Url - self.register_url("view/", self.make_view("view.html")) + self.register_url("view/", self.make_view("cvar_hyphens_view.html")) # Override URLconf with self.settings(ROOT_URLCONF=self.get_url_conf()):