:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(dashboards): reuse tile serializer across dashboard tiles (#71022)
Thomas Obermüller624f1b6d735a
diff --git a/products/dashboards/backend/api/dashboard.py b/products/dashboards/backend/api/dashboard.pyindex 7ab274866123..42c058c46e40 100644--- a/products/dashboards/backend/api/dashboard.py+++ b/products/dashboards/backend/api/dashboard.py@@ -2025,8 +2025,24 @@ class DashboardSerializer(DashboardMetadataSerializer):if not sorted_tiles:return []+ # One serializer reused across tiles: constructing DashboardTileSerializer per tile+ # deep-copies every declared field of the tile + nested insight serializers, which+ # dominates CPU on large dashboards. Per-tile state is passed via the shared context.+ # (dashboard, insight) is unique per dashboard, so the per-instance insight_result+ # lru_cache never leaks a result from one tile to another.+ tile_context = self.context.copy()+ reused_serializer = DashboardTileSerializer(context=tile_context)for order, tile in enumerate(sorted_tiles):- order, tile_data = serialize_tile_with_context(tile, order, self.context)+ tile_context.update({"dashboard_tile": tile, "order": order})++ if isinstance(tile.layouts, str):+ tile.layouts = json.loads(tile.layouts)++ try:+ tile_data = reused_serializer.to_representation(tile)+ except pydantic_core.ValidationError:+ # Fall back to the fresh-serializer path, which handles the error shape+ order, tile_data = serialize_tile_with_context(tile, order, self.context)serialized_tiles.append(cast(ReturnDict, tile_data))return serialized_tiles