Skip to content

Commit 8690882

Browse files
authored
perf: Avoid generating str repr of table during rendering (#1015)
What's happening here is that [`_repr_mimebundle_`](https://github.com/manzt/anywidget/blob/a49a872a9e2cd491e7597bc1e550d14352efc7b6/anywidget/widget.py#L76-L82) creates **both** the rich HTML content **and** a plain text repr to show in environments that don't support HTML rendering. So it was taking 11.6 seconds to print the string repr of the Arrow table, just to generate the `_repr_mimebundle_`. I'm not clear if this is a regression or just an unhandled bug on the arro3 side (kylebarron/arro3#432). I think arro3 handles _long_ tables by taking the first and last `n` rows to print. But it clearly doesn't handle very _wide_ rows at all. ### Change list - This overrides [`_repr_keys`](https://github.com/jupyter-widgets/ipywidgets/blob/df7836eb4509283a0bf14d7b4ac92bc30c11fc76/python/ipywidgets/ipywidgets/widgets/widget.py#L827-L843) to ensure that the str repr of the `table` attribute is never generated. I think this is the only key that should be very slow. ### Benchmark 99.9% faster rendering 😅 On this branch: ```py from lonboard import viz from geodatasets import get_path import geopandas as gpd from IPython.display import display gdf = gpd.read_file(get_path("nybb")) %%time m = viz(gdf) # CPU times: user 112 ms, sys: 28.3 ms, total: 141 ms # Wall time: 148 ms %%time display(m) # CPU times: user 5.01 ms, sys: 1.41 ms, total: 6.43 ms # Wall time: 5.64 ms ``` On main: ```py from lonboard import viz from geodatasets import get_path import geopandas as gpd from IPython.display import display gdf = gpd.read_file(get_path("nybb")) %%time m = viz(gdf) # CPU times: user 112 ms, sys: 28.3 ms, total: 141 ms # Wall time: 148 ms %%time display(m) # CPU times: user 11.6 s, sys: 262 ms, total: 11.9 s # Wall time: 12 s ``` Closes #1014
1 parent 7f14643 commit 8690882

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lonboard/layer/_base.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
if TYPE_CHECKING:
3333
import sys
34-
from collections.abc import Sequence
34+
from collections.abc import Generator, Sequence
3535

3636
import duckdb
3737
import geopandas as gpd
@@ -313,6 +313,17 @@ class BaseArrowLayer(BaseLayer):
313313

314314
table: ArrowTableTrait
315315

316+
def _repr_keys(self) -> Generator[str, Any, None]:
317+
# Avoid rendering `table` in the string repr
318+
#
319+
# By default, `_repr_mimebundle_` creates the rich HTML content **and** a plain
320+
# text repr to show in environments that don't support rendering HTML. We want
321+
# to avoid generating a str repr of any large values.
322+
# https://github.com/developmentseed/lonboard/issues/1014
323+
for key in super()._repr_keys():
324+
if key != "table":
325+
yield key
326+
316327
def __init__(
317328
self,
318329
table: ArrowStreamExportable,

0 commit comments

Comments
 (0)