Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions eerepr/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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'})"
Expand Down