changeset 3825:b0690df037ca

lazy deserialization of css declarations
author David Grieve<david.grieve@oracle.com>
date Tue, 14 May 2013 15:44:19 -0700
parents 2141df51b9c5
children e6ec5624c645
files javafx-ui-common/src/com/sun/javafx/css/Rule.java javafx-ui-common/src/com/sun/javafx/css/StyleManager.java javafx-ui-common/test/unit/com/sun/javafx/css/Node_cssStyleMap_Test.java javafx-ui-common/test/unit/com/sun/javafx/css/SelectorPartitioningTest.java
diffstat 4 files changed, 59 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-common/src/com/sun/javafx/css/Rule.java	Tue May 14 15:42:38 2013 -0700
+++ b/javafx-ui-common/src/com/sun/javafx/css/Rule.java	Tue May 14 15:44:19 2013 -0700
@@ -26,6 +26,8 @@
 package com.sun.javafx.css;
 
 import com.sun.javafx.collections.TrackableObservableList;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
@@ -46,7 +48,7 @@
  */
 final public class Rule {
 
-    final ObservableList<Selector> selectors =
+    private final ObservableList<Selector> selectors =
             new TrackableObservableList<Selector>() {
 
                 @Override
@@ -75,7 +77,7 @@
         return selectors;
     }
 
-    final ObservableList<Declaration> declarations = 
+    private final ObservableList<Declaration> declarations =
         new TrackableObservableList<Declaration>() {
 
         @Override
@@ -109,6 +111,27 @@
     };
     
     public List<Declaration> getDeclarations() {
+        if (serializedDecls != null && Rule.strings != null) {
+
+            try {
+                ByteArrayInputStream bis = new ByteArrayInputStream(serializedDecls);
+                DataInputStream dis = new DataInputStream(bis);
+                short nDeclarations = dis.readShort();
+                List<Declaration> decls = new ArrayList<Declaration>(nDeclarations);
+                for (int i = 0; i < nDeclarations; i++) {
+                    Declaration d = Declaration.readBinary(dis, Rule.strings);
+                    decls.add(d);
+                }
+
+                this.declarations.addAll(decls);
+
+            } catch (IOException ioe) {
+               assert false; ioe.getMessage();
+            } finally {
+                serializedDecls = null;
+            }
+
+        }
         return declarations;
     }
 
@@ -117,6 +140,7 @@
     public Stylesheet getStylesheet() {
         return stylesheet;
     }
+
     /* package */
     void setStylesheet(Stylesheet stylesheet) {
         this.stylesheet = stylesheet;
@@ -139,6 +163,16 @@
     public Rule(List<Selector> selectors, List<Declaration> declarations) {
         this.selectors.setAll(selectors);
         this.declarations.setAll(declarations);
+        serializedDecls = null;
+    }
+
+    private static String[] strings = null;  // TBD: blech!
+    private byte[] serializedDecls;
+
+    private Rule(List<Selector> selectors, byte[] buf, String[] stringStoreStrings) {
+        this.selectors.setAll(selectors);
+        this.serializedDecls = buf;
+        if (Rule.strings == null) Rule.strings = stringStoreStrings;
     }
 
     /**
@@ -204,11 +238,19 @@
             Selector sel = selectors.get(i);
             sel.writeBinary(os, stringStore);
         }
-        os.writeShort(declarations.size());
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream(5192);
+        DataOutputStream dos = new DataOutputStream(bos);
+
+        dos.writeShort(declarations.size());
+
         for (int i = 0; i < declarations.size(); i++) {
             Declaration decl = declarations.get(i);
-            decl.writeBinary(os, stringStore);
+            decl.writeBinary(dos, stringStore);
         }
+
+        os.writeInt(bos.size());
+        os.write(bos.toByteArray());
     }
 
     static Rule readBinary(DataInputStream is, String[] strings)
@@ -221,13 +263,13 @@
             selectors.add(s);
         }
 
-        short nDeclarations = is.readShort();
-        List<Declaration> declarations = new ArrayList<Declaration>(nDeclarations);
-        for (int i = 0; i < nDeclarations; i++) {
-            Declaration d = Declaration.readBinary(is, strings);
-            declarations.add(d);
-        }
 
-        return new Rule(selectors, declarations);
+        // de-serialize decls into byte array
+        int nBytes = is.readInt();
+        byte[] buf = new byte[nBytes];
+
+        is.readFully(buf);
+
+        return new Rule(selectors, buf, strings);
     }
 }
--- a/javafx-ui-common/src/com/sun/javafx/css/StyleManager.java	Tue May 14 15:42:38 2013 -0700
+++ b/javafx-ui-common/src/com/sun/javafx/css/StyleManager.java	Tue May 14 15:44:19 2013 -0700
@@ -1725,8 +1725,8 @@
                             continue;
                         }
 
-                        for (int d = 0, dmax = rule.declarations.size(); d < dmax; d++) {
-                            final Declaration decl = rule.declarations.get(d);
+                        for (int d = 0, dmax = rule.getDeclarations().size(); d < dmax; d++) {
+                            final Declaration decl = rule.getDeclarations().get(d);
 
                             final CascadingStyle s = new CascadingStyle(
                                     new Style(match.selector, decl),
--- a/javafx-ui-common/test/unit/com/sun/javafx/css/Node_cssStyleMap_Test.java	Tue May 14 15:42:38 2013 -0700
+++ b/javafx-ui-common/test/unit/com/sun/javafx/css/Node_cssStyleMap_Test.java	Tue May 14 15:44:19 2013 -0700
@@ -69,7 +69,7 @@
         for (Declaration decl : decls) {
             styles.add(
                 new CascadingStyle(
-                    new Style(decl.rule.selectors.get(0), decl), 
+                    new Style(decl.rule.getSelectors().get(0), decl),
                     new PseudoClassState(),
                     0, 
                     0
--- a/javafx-ui-common/test/unit/com/sun/javafx/css/SelectorPartitioningTest.java	Tue May 14 15:42:38 2013 -0700
+++ b/javafx-ui-common/test/unit/com/sun/javafx/css/SelectorPartitioningTest.java	Tue May 14 15:44:19 2013 -0700
@@ -186,7 +186,7 @@
                 CSSParser.getInstance().parse(data.stylesheetText);
                 
         for (Rule rule : stylesheet.getRules()) {
-            for (Selector selector : rule.selectors) {
+            for (Selector selector : rule.getSelectors()) {
                 instance.partition(selector);
             }
         }
@@ -209,8 +209,8 @@
 
         Rule rule = selectorDatum.selector.getRule();
 
-        assertEquals(1,rule.declarations.size());
-        Declaration decl = rule.declarations.get(0);
+        assertEquals(1,rule.getDeclarations().size());
+        Declaration decl = rule.getDeclarations().get(0);
         
         assertEquals("-fx-fill", decl.property);