From 1dc1b7511fcde2b0a1ce53e5c0d1c344d08e1e36 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sun, 5 Apr 2026 23:01:21 +0800 Subject: [PATCH] Attempt to fix the perf issue in `selectattr | first` filter (#209) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace O(n²) Jinja2 filter searches with an O(1) dictionary lookup. --- Whatsapp_Chat_Exporter/utility.py | 6 +++++- Whatsapp_Chat_Exporter/whatsapp.html | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py index 6ccbee6..c974dbf 100644 --- a/Whatsapp_Chat_Exporter/utility.py +++ b/Whatsapp_Chat_Exporter/utility.py @@ -213,6 +213,9 @@ def rendering( if "??" not in headline: raise ValueError("Headline must contain '??' to replace with name") headline = headline.replace("??", name) + # Create a temporary lookup map only at render-time; + # media preview in reply is a UI-specific concern and + # is ignored by the core database processing with open(output_file_name, "w", encoding="utf-8") as f: f.write( template.render( @@ -226,7 +229,8 @@ def rendering( previous=previous, status=chat.status, media_base=chat.media_base, - headline=headline + headline=headline, + msg_map={m.key_id: m for m in msgs}.get ) ) diff --git a/Whatsapp_Chat_Exporter/whatsapp.html b/Whatsapp_Chat_Exporter/whatsapp.html index 3d1115a..e8a40f6 100644 --- a/Whatsapp_Chat_Exporter/whatsapp.html +++ b/Whatsapp_Chat_Exporter/whatsapp.html @@ -355,7 +355,7 @@ {% endif %}

- {% set replied_msg = msgs | selectattr('key_id', 'equalto', msg.reply) | first %} + {% set replied_msg = msg_map(msg.reply) %} {% if replied_msg and replied_msg.media == true %}
{% if "image/" in replied_msg.mime %} @@ -461,7 +461,7 @@ {% endif %}

- {% set replied_msg = msgs | selectattr('key_id', 'equalto', msg.reply) | first %} + {% set replied_msg = msg_map(msg.reply) %} {% if replied_msg and replied_msg.media == true %}
{% if "image/" in replied_msg.mime %}