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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

Nothing yet!
### 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.

## [0.1.1] - 2025-02-19

Expand Down
18 changes: 10 additions & 8 deletions eerepr/repr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import html
import uuid
from functools import _lru_cache_wrapper, lru_cache
from typing import Any, Literal, Union
from warnings import warn
Expand Down Expand Up @@ -57,16 +56,19 @@ def _repr_html_(obj: EEObject) -> str:
)


def _uncached_repr_html_(obj: EEObject) -> str:
"""Generate an HTML representation of an EE object without caching."""
if isinstance(_repr_html_, _lru_cache_wrapper):
return _repr_html_.__wrapped__(obj)
return _repr_html_(obj)


def _ee_repr(obj: EEObject) -> str:
"""Wrapper around _repr_html_ to prevent cache hits on nondeterministic objects."""
if _is_nondeterministic(obj):
# We don't want to cache nondeterministic objects, so we'll add add a unique
# attribute that causes ee.ComputedObject.__eq__ to return False, preventing a
# cache hit.
obj._eerepr_id = uuid.uuid4()
"""Handle errors and conditional caching for _repr_html_."""
repr_func = _uncached_repr_html_ if _is_nondeterministic(obj) else _repr_html_

try:
rep = _repr_html_(obj)
rep = repr_func(obj)
except ee.EEException as e:
if options.on_error == "raise":
raise e from None
Expand Down
8 changes: 3 additions & 5 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ def test_caching(obj):
assert cache.cache_info().hits == 1


def test_nondeterministic_caching():
def test_nondeterministic_uncached():
"""
ee.List.shuffle(seed=False) is nondeterministic. Make sure it misses the cache.
ee.List.shuffle(seed=False) is nondeterministic. Make sure it isn't cached.
"""
eerepr.initialize()
cache = eerepr.repr._repr_html_

assert cache.cache_info().misses == 0
x = ee.List([0, 1, 2]).shuffle(seed=False)
x._repr_html_()
x._repr_html_()
assert cache.cache_info().misses == 2
assert cache.cache_info().currsize == 0