holoviz/panel · #9067
perf: Make ReactiveESM components cheap to render and update
panel/custom.py167 + / 46 −
@@ -11,7 +11,8 @@ from abc import abstractmethod from collections import defaultdict-from functools import partial+from functools import lru_cache, partial+from weakref import WeakKeyDictionary import param @@ -58,6 +59,55 @@ _IGNORED_ESM_PROPERTIES = ('js_event_callbacks', 'esm_constants', 'js_property_callbacks', 'subscribed_events', 'syncable') +# Properties that are applied to the ReactiveESM model itself rather than+# its data model when they change.+_ESM_MODEL_PROPERTIES = frozenset(Reactive.param) | {'esm', 'importmap'}++# Per-class caches of values derived only from class attributes, keyed by+# the class so dynamically created classes can be collected.+_ESM_SPECS: WeakKeyDictionary[type, _ESMSpec] = WeakKeyDictionary()+_BUNDLE_PATHS: WeakKeyDictionary[type, dict[bool, os.PathLike | None]] = WeakKeyDictionary()+_ESM_PATHS: WeakKeyDictionary[type, dict[tuple[bool, bool], os.PathLike | None]] = WeakKeyDictionary()+_RESOURCE_PATHS: WeakKeyDictionary[type, dict[tuple, str]] = WeakKeyDictionary()+_IMPORTMAPS: WeakKeyDictionary[type, dict[bool, dict]] = WeakKeyDictionary()+_LINKED_PROPERTIES: WeakKeyDictionary[type, tuple[str, ...]] = WeakKeyDictionary()++# Dedented ESM sources by path, together with the mtime they were read at.+_ESM_SOURCES: dict[str, tuple[int, str]] = {}+++class _ESMSpec(t.NamedTuple):+ """+ Classification of a ReactiveESM class' parameters, computed once per+ class since it only depends on class level metadata.+ """+ ignored: tuple[str, ...]+ popped: tuple[str, ...]+ data: tuple[str, ...]+ events: tuple[str, ...]+ children: tuple[str, ...]+++def _read_esm_source(path: pathlib.Path) -> str:+ key = os.fspath(path)+ mtime = path.stat().st_mtime_ns+ cached = _ESM_SOURCES.get(key)+ if cached is not None and cached[0] == mtime:+ return cached[1]+ source = textwrap.dedent(path.read_text(encoding='utf-8'))+ _ESM_SOURCES[key] = (mtime, source)+ return source+++@lru_cache(maxsize=256)+def _dedent(esm: str) -> str:+ return textwrap.dedent(esm)+++@lru_cache(maxsize=None)+def _bundle_hash(bundle_path: str) -> str:+ return hashlib.sha256(bundle_path.encode('utf-8')).hexdigest()+ class PyComponent(Viewable, Layoutable): '''@@ -278,6 +328,22 @@ def _module_path(cls): @classproperty def _bundle_path(cls) -> os.PathLike | None:+ # Resolving the bundle checks the filesystem and imports modules,+ # so a found bundle is cached for every instance of the class. A+ # missing one is not, since it may be compiled while the process runs.+ autoreload = bool(config.autoreload)+ # classproperty is called with the class, which mypy cannot infer+ owner = t.cast('type', cls)+ cached = _BUNDLE_PATHS.get(owner, {}).get(autoreload)+ if cached is not None and os.path.isfile(cached):+ return cached+ path = cls._resolve_bundle_path()+ if path is not None:+ _BUNDLE_PATHS.setdefault(owner, {})[autoreload] = path+ return path++ @classmethod+ def _resolve_bundle_path(cls) -> os.PathLike | None: if config.autoreload and cls._esm: return None mod_path = cls._module_path@@ -343,6 +409,20 @@ def _bundle_css(cls): @classmethod def _esm_path(cls, compiled: bool | t.Literal['compiling'] = True) -> os.PathLike | None:+ autoreload = bool(config.autoreload)+ key = (compiled is True, autoreload)+ cached = _ESM_PATHS.get(cls, {}).get(key)+ if cached is not None and os.path.isfile(cached):+ return cached+ path = cls._resolve_esm_path(compiled)+ # When compiled, the source of a component without a bundle is not+ # cached either, so a bundle compiled later takes over.+ if path is not None and (compiled is not True or path == _BUNDLE_PATHS.get(cls, {}).get(autoreload)):+ _ESM_PATHS.setdefault(cls, {})[key] = path+ return path++ @classmethod+ def _resolve_esm_path(cls, compiled: bool | t.Literal['compiling'] = True) -> os.PathLike | None: if compiled is True or not cls._esm: bundle_path = cls._bundle_path if bundle_path:@@ -366,13 +446,23 @@ def _esm_path(cls, compiled: bool | t.Literal['compiling'] = True) -> os.PathLik @classmethod def _component_resource_path(cls, esm_path, compiled):+ from bokeh.embed.bundle import extension_dirs++ # The resolved path depends on the registered extensions and the+ # prefix the app is served under, so both are part of the key.+ key = (os.fspath(esm_path), compiled is True, state.rel_path, len(extension_dirs))+ cached = _RESOURCE_PATHS.get(cls)+ if cached is not None and key in cached:+ return cached[key] base_cls = cls for scls in cls.__mro__[1:]: if not issubclass(scls, ReactiveESM): continue if esm_path == scls._esm_path(compiled=compiled is True): base_cls = scls- return component_resource_path(base_cls, '_bundle_path', esm_path)+ path = component_resource_path(base_cls, '_bundle_path', esm_path)+ _RESOURCE_PATHS.setdefault(cls, {})[key] = path+ return path @classmethod def _notebook_bundle_url(cls, esm_path) -> str | None:@@ -395,15 +485,14 @@ def _render_esm(cls, compiled: bool | t.Literal['compiling'] = True, server: boo modified = hashlib.sha256(str(esm_path.stat().st_mtime).encode('utf-8')).hexdigest() esm += f'?{modified}' else:- esm = esm_path.read_text(encoding='utf-8')+ return _read_esm_source(pathlib.Path(esm_path)) else: esm = cls._esm if esm is None: raise ValueError( f'{cls.__name__}._esm was found empty. Ensure you define an ESM module.' )- esm = textwrap.dedent(esm)- return esm+ return _dedent(esm) def _cleanup(self, root: Model | None = None) -> None: if root:@@ -434,40 +523,66 @@ def _update_esm(self): continue self._apply_update({}, {'esm': esm}, model, ref) - @property- def _linked_properties(self) -> tuple[str, ...]:- mapping = {v: k for k, v in self._property_mapping.items() if v is not None}- params = self.param.objects(instance=False)- return tuple(- p for p in self._data_model.properties()- if p not in _IGNORED_ESM_PROPERTIES and not isinstance(params[mapping.get(p, p)], (Child, Children))- )-- def _get_properties(self, doc: Document | None) -> dict[str, t.Any]:- props = super()._get_properties(doc)- cls = type(self)- data_props = {}+ @classmethod+ def _esm_spec(cls) -> _ESMSpec:+ spec = _ESM_SPECS.get(cls)+ if spec is not None:+ return spec+ params = cls.param.objects(instance=False)+ mapping = cls._property_mapping # Split data model properties from ESM model properties # Note that inherited parameters are generally treated # as ESM model properties unless their type has changed- ignored = [+ ignored = tuple( p for p in Reactive.param- if not issubclass(cls.param[p].owner, ReactiveESM) or+ if not issubclass(params[p].owner, ReactiveESM) or (p in Viewable.param and p not in ('name', 'use_shadow_dom')- and type(Reactive.param[p]) is type(cls.param[p]))- ]- events = []- for k in self.param:- p = self.param[k]- if is_viewable_param(p) or type(self)._property_mapping.get(k, "") is None:- props.pop(k, None)- continue- elif k not in props or (k in ignored and k != 'name') or ((p.precedence or 0) < 0):+ and type(Reactive.param[p]) is type(params[p]))+ )+ popped, data, events, children = [], [], [], []+ for k, p in params.items():+ viewable = is_viewable_param(p)+ if viewable or mapping.get(k, "") is None:+ popped.append(k)+ if viewable and mapping.get(k, "") is not None:+ children.append(k)+ elif k in ignored:+ # The model is named after the class, the name parameter+ # is neither an ESM nor a data model property.+ if k == 'name':+ popped.append(k)+ elif (p.precedence or 0) < 0: continue- v = props.pop(k)- if isinstance(p, param.Event):- events.append(k)- data_props[k] = v+ else:+ data.append(k)+ if isinstance(p, param.Event):+ events.append(k)+ _ESM_SPECS[cls] = spec = _ESMSpec(+ ignored, tuple(popped), tuple(data), tuple(events), tuple(children)+ )+ return spec++ @property+ def _linked_properties(self) -> tuple[str, ...]:+ cls = type(self)+ linked = _LINKED_PROPERTIES.get(cls)+ if linked is None:+ mapping = {v: k for k, v in cls._property_mapping.items() if v is not None}+ params = cls.param.objects(instance=False)+ _LINKED_PROPERTIES[cls] = linked = tuple(+ p for p in cls._data_model.properties()+ if p not in _IGNORED_ESM_PROPERTIES and not isinstance(params[mapping.get(p, p)], (Child, Children))+ )+ return linked++ def _get_properties(self, doc: Document | None) -> dict[str, t.Any]:+ props = super()._get_properties(doc)+ cls = type(self)+ spec = cls._esm_spec()+ for k in spec.popped:+ props.pop(k, None)+ data_props = {k: props.pop(k) for k in spec.data if k in props}+ events = [k for k in spec.events if k in data_props] bundle_path = self._bundle_path importmap = self._process_importmap() is_session = notebook_bundle = False@@ -488,7 +603,7 @@ def _get_properties(self, doc: Document | None) -> dict[str, t.Any]: if self._bundle_css: css_bundle = esm_url.removesuffix('.js') + '.css' else:- bundle_hash = hashlib.sha256(str(bundle_path).encode('utf-8')).hexdigest()+ bundle_hash = _bundle_hash(str(bundle_path)) else: bundle_hash = None esm = self._render_esm(not config.autoreload, server=is_session or notebook_bundle)@@ -499,7 +614,7 @@ def _get_properties(self, doc: Document | None) -> dict[str, t.Any]: if url := notebook_extension_url(esm.removeprefix('./')): esm = url else:- bundle_hash = hashlib.sha256(str(bundle_path).encode('utf-8')).hexdigest()+ bundle_hash = _bundle_hash(str(bundle_path)) css_bundle = None data_props['esm_constants'] = self._constants if cls._data_model__initialized:@@ -515,7 +630,7 @@ def _get_properties(self, doc: Document | None) -> dict[str, t.Any]: 'bundle': bundle_hash, 'css_bundle': css_bundle, 'class_name': cls.__name__,- 'data': self._data_model(**{p: v for p, v in data_props.items() if p not in ignored}),+ 'data': self._data_model(**data_props), 'dev': config.autoreload or getattr(self, '_debug', False), 'esm': esm, 'events': events,@@ -550,12 +665,8 @@ def _get_child_model( def _get_children(self, data_model, doc, root, parent, comm) -> tuple[dict[str, list[UIElement] | UIElement | None], list[UIElement]]: children = {} old_models = []- for k in self.param:- p = self.param[k]- if not is_viewable_param(p) or type(self)._property_mapping.get(k, "") is None:- continue- v = getattr(self, k)- children[k], old = self._get_child_model(v, doc, root, parent, comm)+ for k in type(self)._esm_spec().children:+ children[k], old = self._get_child_model(getattr(self, k), doc, root, parent, comm) old_models += old return children, old_models @@ -611,8 +722,9 @@ def _update_model( ) -> None: model_msg, data_msg, data_resets = {}, {}, {} curdoc_events = self._in_process__events.get(doc, {})+ spec = type(self)._esm_spec() for prop, value in list(msg.items()):- if prop in list(Reactive.param)+['esm', 'importmap']:+ if prop in _ESM_MODEL_PROPERTIES: model_msg[prop] = value continue elif prop in model.children:@@ -627,16 +739,22 @@ def _update_model( continue data_msg[prop] = value- if prop in self.param and isinstance(self.param[prop], param.Event) and value:+ if prop in spec.events and value: data_resets[prop] = False for name, event in events.items(): if name not in model.children: continue new = event.new old_objects = event.old if isinstance(event.old, list) else [event.old]+ if isinstance(new, list):+ # Membership by identity, a list lookup per old child would be+ # quadratic in the number of children.+ retained = {id(obj) for obj in new}+ else:+ retained = {id(new)} for old in old_objects:- if old is None or old is new or (isinstance(new, list) and old in new):+ if old is None or id(old) in retained: continue old._release_model(root, model) @@ -653,7 +771,10 @@ def _update_model( Panel._batch_update = True with hold(doc): changing = []- with freeze_doc(doc, model, msg, force=update_children):+ # The children live on the data model, so that is what is+ # frozen, with the resolved child models, for the document to+ # attach only the added models instead of recomputing.+ with freeze_doc(doc, model.data, data_msg, force=update_children): if model_msg: changing += self._set_on_model(model_msg, root, model) if data_msg:panel/reactive.py30 + / 5 −
@@ -29,6 +29,7 @@ from bokeh.core.property.descriptors import UnsetValueError from bokeh.model import DataModel+from bokeh.model.util import visit_value_and_its_immediate_references from bokeh.models import ImportedStyleSheet, InlineStyleSheet from packaging.version import Version from param.parameterized import (@@ -57,7 +58,9 @@ ) if t.TYPE_CHECKING:- from collections.abc import Callable, Mapping, Sequence+ from collections.abc import (+ Callable, Iterable, Mapping, Sequence,+ ) import pandas as pd @@ -1783,15 +1786,37 @@ def _process_param_change(self, params): return props @classmethod- def _patch_datamodel_ref(cls, props, ref):+ def _patch_datamodel_ref(cls, props, ref, attrs: Iterable[str] | None = None): """ Ensure all DataModels have reference to the root model to ensure that they can be cleaned up correctly.++ Only DataModels referenced from the DataModel's own property+ values are visited (optionally restricted to the given+ attributes): child components tag the DataModels they own+ themselves, so walking into their models, as a select over all+ references would, costs time proportional to the whole tree. """ ref_str = f"__ref:{ref}"- for m in props.select({'type': DataModel}):+ models = [props]+ seen = set()+ while models:+ m = models.pop()+ if id(m) in seen:+ continue+ seen.add(id(m)) if ref_str not in m.tags: m.tags.append(ref_str)+ if attrs is None:+ values = m.properties_with_values(include_defaults=False).values()+ else:+ values = [getattr(m, attr) for attr in attrs]+ attrs = None+ def visit(sub):+ if isinstance(sub, DataModel):+ models.append(sub)+ for value in values:+ visit_value_and_its_immediate_references(value, visit) def _set_on_model(self, msg: Mapping[str, t.Any], root: Model, model: Model) -> list[str]: if not msg:@@ -1824,8 +1849,8 @@ def _set_on_model(self, msg: Mapping[str, t.Any], root: Model, model: Model) -> self._changing[root.ref['id']] = prev_changing else: del self._changing[root.ref['id']]- if isinstance(model, DataModel):- self._patch_datamodel_ref(model, root.ref['id'])+ if transformed and isinstance(model, DataModel):+ self._patch_datamodel_ref(model, root.ref['id'], attrs=transformed) return changing panel/tests/test_custom.py81 + / 0 −
@@ -1,3 +1,5 @@+import os+ import numpy as np import pandas as pd import param@@ -369,3 +371,82 @@ def test_esm_event_reset_does_not_retrigger_watchers(document, comm): assert values == [True] assert model.data.value is False+++class Inner(param.Parameterized):++ text = param.String(default='a')+++class ESMWithNested(ReactiveESM):++ child = param.ClassSelector(class_=Inner)++ children = param.List(item_type=Inner)+++def test_reactive_esm_tags_nested_datamodels(document, comm):+ esm = ESMWithNested(child=Inner(), children=[Inner()])++ model = esm.get_root(document, comm)++ ref = f"__ref:{model.ref['id']}"+ assert ref in model.data.tags+ assert ref in model.data.child.tags+ assert ref in model.data.children[0].tags++ esm.child = Inner(text='b')+ esm.children = [Inner(text='c')]++ assert model.data.child.text == 'b'+ assert ref in model.data.child.tags+ assert model.data.children[0].text == 'c'+ assert ref in model.data.children[0].tags+++def test_reactive_esm_children_append_attaches_without_recompute(document, comm, monkeypatch):+ from bokeh.document.models import DocumentModelManager++ md1 = Markdown('foo')+ esm = ESMWithChildren(children=[md1])++ model = esm.get_root(document, comm)+ document.add_root(model)++ recomputes = []+ recompute = DocumentModelManager.recompute+ monkeypatch.setattr(+ DocumentModelManager, 'recompute', lambda self: recomputes.append(1) or recompute(self)+ )++ md2 = Markdown('bar')+ esm.children = [md1, md2]++ ref = model.ref['id']+ assert ref in md1._models and ref in md2._models+ md2_model, _ = md2._models[ref]+ assert model.data.children == [md1._models[ref][0], md2_model]+ assert md2_model.document is document+ assert md2_model.id in document.models._models+ assert not recomputes++ monkeypatch.undo()+ attached = set(document.models._models)+ document.models.recompute()+ assert set(document.models._models) == attached+++def test_render_esm_reflects_file_changes(tmp_path):+ esm_file = tmp_path / 'component.js'+ esm_file.write_text('export function render() { return "a" }')++ class ESMFromFile(ReactiveESM):+ _esm = esm_file++ assert 'return "a"' in ESMFromFile._render_esm()++ esm_file.write_text('export function render() { return "b" }')+ stat = esm_file.stat()+ os.utime(esm_file, ns=(stat.st_atime_ns, stat.st_mtime_ns + 10**9))++ assert 'return "b"' in ESMFromFile._render_esm()