From ba9043cd013ac86be1d82be443a7e9b34f35fab3 Mon Sep 17 00:00:00 2001 From: Aaron Zuspan Date: Sun, 23 Feb 2025 07:37:10 -0800 Subject: [PATCH 1/2] Calculate max list length once 3 * (n - 1) + 3 reduces to 3 * n, and we can save a few CPU cycles by calculating the max list length once instead of calculating the minimum string length for each list. --- eerepr/html.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/eerepr/html.py b/eerepr/html.py index 0b41b92..11fd65a 100644 --- a/eerepr/html.py +++ b/eerepr/html.py @@ -7,6 +7,10 @@ # Max characters to display for a list before truncating to "List (n elements)" MAX_INLINE_LENGTH = 50 +# Max elements in a list without definitely truncating, i.e. a list where each element +# takes 1 character plus brackets and delimiters. +MAX_LIST_LENGTH = MAX_INLINE_LENGTH // 3 + # Sorting priority for Earth Engine properties PROPERTY_PRIORITY = [ "type", @@ -57,11 +61,9 @@ def list_to_html(obj: list, key: Hashable | None = None) -> str: n = len(obj) header = f"{key}: " if key is not None else "" - # Skip the expensive stringification for lists that are definitely too long to - # include inline (counting whitespace and delimiters). This is a substantial - # performance improvement for large collections. - min_length = 3 * (n - 1) + 3 - if min_length < MAX_INLINE_LENGTH and len(contents := str(obj)) < MAX_INLINE_LENGTH: + # Only stringify the list if it has few enough elements that it might not be + # truncated. + if n < MAX_LIST_LENGTH and len(contents := str(obj)) < MAX_INLINE_LENGTH: header += contents else: header += f"List ({n} {'element' if n == 1 else 'elements'})" From d7c39eab091b9d3409770a3ae2c6b736870c32f0 Mon Sep 17 00:00:00 2001 From: Aaron Zuspan Date: Tue, 25 Feb 2025 09:09:14 -0800 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf40dde..41a0dcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. ### Performance - Non-deterministic objects are no longer cached, rather than getting a unique attribute to force a cache miss. Negligible reduction in memory, unless you're caching huge shuffled lists. +- The optimization of long truncated lists was modified to check list lengths against a pre-computed max length instead of calculating the minimum string length of each list. Minor speedup when working with a lot of lists. ## [0.1.1] - 2025-02-19