From 39c7d9f68a3723feeebf3b05646aaef2fe3ce070 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Sat, 25 Jul 2015 12:14:30 +0300 Subject: Omit empty hierarchy data from the response (#18510) Reduces the payload size for the "Update all labels" action with 40 layouts in BasicPerformanceTest by 16% (from 11087 to 9270 bytes). The reduction is improved to 22% (1855 to 1455) if the response is gzipped since the omited data doesn't compress very well. Change-Id: I1d2837c93222fffa59b14836f162e3e87349e086 --- .../communication/ConnectorHierarchyWriter.java | 25 +++++++++++++++------- .../server/communication/SharedStateWriter.java | 15 +++++++++---- .../vaadin/server/communication/UidlWriter.java | 6 ++++-- 3 files changed, 32 insertions(+), 14 deletions(-) (limited to 'server') diff --git a/server/src/com/vaadin/server/communication/ConnectorHierarchyWriter.java b/server/src/com/vaadin/server/communication/ConnectorHierarchyWriter.java index 1c1a220b5d..503bf8c0ae 100644 --- a/server/src/com/vaadin/server/communication/ConnectorHierarchyWriter.java +++ b/server/src/com/vaadin/server/communication/ConnectorHierarchyWriter.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.Serializable; import java.io.Writer; import java.util.Collection; +import java.util.Set; import com.vaadin.server.AbstractClientConnector; import com.vaadin.server.ClientConnector; @@ -49,10 +50,13 @@ public class ConnectorHierarchyWriter implements Serializable { * The {@link UI} whose hierarchy to write. * @param writer * The {@link Writer} used to write the JSON. + * @param stateUpdateConnectors + * connector ids with state changes * @throws IOException * If the serialization fails. */ - public void write(UI ui, Writer writer) throws IOException { + public void write(UI ui, Writer writer, Set stateUpdateConnectors) + throws IOException { Collection dirtyVisibleConnectors = ui .getConnectorTracker().getDirtyVisibleConnectors(); @@ -69,13 +73,18 @@ public class ConnectorHierarchyWriter implements Serializable { children.set(children.length(), child.getConnectorId()); } } - try { - hierarchyInfo.put(connectorId, children); - } catch (JsonException e) { - throw new PaintException( - "Failed to send hierarchy information about " - + connectorId + " to the client: " - + e.getMessage(), e); + + // Omit for leaf nodes with state changes + if (children.length() > 0 + || !stateUpdateConnectors.contains(connectorId)) { + try { + hierarchyInfo.put(connectorId, children); + } catch (JsonException e) { + throw new PaintException( + "Failed to send hierarchy information about " + + connectorId + " to the client: " + + e.getMessage(), e); + } } } writer.write(JsonUtil.stringify(hierarchyInfo)); diff --git a/server/src/com/vaadin/server/communication/SharedStateWriter.java b/server/src/com/vaadin/server/communication/SharedStateWriter.java index 6ef02955f7..06b59ad4cc 100644 --- a/server/src/com/vaadin/server/communication/SharedStateWriter.java +++ b/server/src/com/vaadin/server/communication/SharedStateWriter.java @@ -20,6 +20,8 @@ import java.io.IOException; import java.io.Serializable; import java.io.Writer; import java.util.Collection; +import java.util.HashSet; +import java.util.Set; import com.vaadin.server.ClientConnector; import com.vaadin.server.PaintException; @@ -47,31 +49,36 @@ public class SharedStateWriter implements Serializable { * The UI whose state changes should be written. * @param writer * The writer to use. + * @return a set of connector ids with state changes * @throws IOException * If the serialization fails. */ - public void write(UI ui, Writer writer) throws IOException { + public Set write(UI ui, Writer writer) throws IOException { Collection dirtyVisibleConnectors = ui .getConnectorTracker().getDirtyVisibleConnectors(); + Set writtenConnectors = new HashSet(); JsonObject sharedStates = Json.createObject(); for (ClientConnector connector : dirtyVisibleConnectors) { // encode and send shared state + String connectorId = connector.getConnectorId(); try { JsonObject stateJson = connector.encodeState(); if (stateJson != null && stateJson.keys().length != 0) { - sharedStates.put(connector.getConnectorId(), stateJson); + sharedStates.put(connectorId, stateJson); + writtenConnectors.add(connectorId); } } catch (JsonException e) { throw new PaintException( "Failed to serialize shared state for connector " + connector.getClass().getName() + " (" - + connector.getConnectorId() + "): " - + e.getMessage(), e); + + connectorId + "): " + e.getMessage(), e); } } writer.write(JsonUtil.stringify(sharedStates)); + + return writtenConnectors; } } diff --git a/server/src/com/vaadin/server/communication/UidlWriter.java b/server/src/com/vaadin/server/communication/UidlWriter.java index 25b1bdaaf9..b117cb4b4d 100644 --- a/server/src/com/vaadin/server/communication/UidlWriter.java +++ b/server/src/com/vaadin/server/communication/UidlWriter.java @@ -159,7 +159,8 @@ public class UidlWriter implements Serializable { // processing. writer.write("\"state\":"); - new SharedStateWriter().write(ui, writer); + Set stateUpdateConnectors = new SharedStateWriter().write( + ui, writer); writer.write(", "); // close states // TODO This should be optimized. The type only needs to be @@ -179,7 +180,8 @@ public class UidlWriter implements Serializable { // child to 0 children) writer.write("\"hierarchy\":"); - new ConnectorHierarchyWriter().write(ui, writer); + new ConnectorHierarchyWriter().write(ui, writer, + stateUpdateConnectors); writer.write(", "); // close hierarchy // send server to client RPC calls for components in the UI, in call -- cgit v1.2.3