Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use hash of reactive value object to determine if it has changed #1428

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ install_requires =
# starlette. For more information, see:
# https://github.com/posit-dev/py-shiny/issues/1114#issuecomment-1942757757
python-multipart>=0.0.7;platform_system!="Emscripten"
xxhash>=3.4.1
tests_require =
pytest>=3
zip_safe = False
Expand Down
26 changes: 24 additions & 2 deletions shiny/reactive/_reactives.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import asyncio
import functools
import pickle
import traceback
import warnings
from typing import (
Expand All @@ -30,11 +31,13 @@
overload,
)

from xxhash import xxh32_hexdigest

from .. import _utils
from .._docstring import add_example
from .._utils import is_async_callable, run_coro_sync
from .._validation import req
from ..types import MISSING, MISSING_TYPE, ActionButtonValue, SilentException
from ..types import MISSING, MISSING_TYPE, ActionButtonValue, Any, SilentException
from ._core import Context, Dependents, ReactiveWarning, isolate

if TYPE_CHECKING:
Expand All @@ -43,6 +46,14 @@
T = TypeVar("T")


def is_immutable(x: Any) -> bool:
return isinstance(x, (int, float, str, tuple, frozenset, bool))


def hash_digest(x: Any) -> str:
return xxh32_hexdigest(pickle.dumps(x, protocol=pickle.HIGHEST_PROTOCOL))


# ==============================================================================
# Value
# ==============================================================================
Expand Down Expand Up @@ -116,6 +127,9 @@ def __init__(
self._read_only: bool = read_only
self._value_dependents: Dependents = Dependents()
self._is_set_dependents: Dependents = Dependents()
self._hash: str | None = None
if not (is_immutable(value) or isinstance(value, MISSING_TYPE)):
self._hash = hash_digest(value)

def __call__(self) -> T:
return self.get()
Expand Down Expand Up @@ -172,14 +186,22 @@ def set(self, value: T) -> bool:
# The ._set() method allows setting read-only Value objects. This is used when the
# Value is part of a session.Inputs object, and the session wants to set it.
def _set(self, value: T) -> bool:
if self._value is value:
value_hash = None
if is_immutable(value) and value == self._value:
return False
elif isinstance(self._value, MISSING_TYPE) and isinstance(value, MISSING_TYPE):
return False
else:
value_hash = hash_digest(value)
if value_hash == self._hash:
return False

if isinstance(self._value, MISSING_TYPE) != isinstance(value, MISSING_TYPE):
self._is_set_dependents.invalidate()

self._value = value
self._value_dependents.invalidate()
self._hash = value_hash
return True

def unset(self) -> None:
Expand Down
Loading