Stephen Haberman has uploaded a new change for review.

  https://gwt-review.googlesource.com/3440


Change subject: Optimize initializing fields at the top scope.
......................................................................

Optimize initializing fields at the top scope.

Change-Id: I97a06eb36396a8b8659ce9a025b21a9cf93d0500
---
M dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
1 file changed, 33 insertions(+), 3 deletions(-)



diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
index 240f6a0..3606478 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
@@ -15,10 +15,10 @@
  */
 package com.google.gwt.dev.jjs.impl;

+import com.google.gwt.core.ext.PropertyOracle;
 import com.google.gwt.core.ext.linker.CastableTypeMap;
 import com.google.gwt.core.ext.linker.impl.StandardCastableTypeMap;
 import com.google.gwt.core.ext.linker.impl.StandardSymbolData;
-import com.google.gwt.core.ext.PropertyOracle;
 import com.google.gwt.dev.jjs.HasSourceInfo;
 import com.google.gwt.dev.jjs.InternalCompilerException;
 import com.google.gwt.dev.jjs.JsOutputOption;
@@ -859,7 +859,7 @@
       JVariable target = x.getVariableRef().getTarget();
       if (target instanceof JField) {
         JField field = (JField) target;
- if (field.getLiteralInitializer() != null && (field.isStatic() || field.isFinal())) {
+        if (initializeAtTopScope(field)) {
           // Will initialize at top scope; no need to double-initialize.
           push(null);
           return;
@@ -893,7 +893,7 @@
     @Override
     public void endVisit(JField x, Context ctx) {
       // if we need an initial value, create an assignment
- if (x.getLiteralInitializer() != null && (x.isFinal() || x.isStatic())) {
+      if (initializeAtTopScope(x)) {
         // setup the constant value
         accept(x.getLiteralInitializer());
       } else if (x.getEnclosingType() == program.getTypeJavaLangObject()) {
@@ -2207,6 +2207,36 @@
jsInvocation.setQualifier(names.get(clinitMethod).makeRef(sourceInfo));
       return jsInvocation;
     }
+
+    /**
+ * If a field is a literal, we can potentially treat it as immutable and assign it once on the + * prototype, to be reused by all instances of the class, instead of re-assigning the same
+     * literal in each constructor.
+     *
+ * Technically, to match JVM semantics, we should only do this for final or static fields. For + * non-final/non-static fields, a super class's cstr should actually see default values (not the
+     * literal initializer) before the subclass's cstr runs.
+     *
+ * However, this is slow for an admittedly uncommon case (a super class using a virtual method + * to read a subclass's field), so we apply some hueristics to see if we can do the fast thing
+     * (initialize at the top/prototype scope).
+     */
+    private boolean initializeAtTopScope(JField x) {
+      if (x.getLiteralInitializer() != null) {
+        if (x.isFinal() || x.isStatic()) {
+ // we can definitely initialize at top-scope, as that matches the JVM
+          return true;
+ } else if (x.getEnclosingType().getSuperClass() == program.getTypeJavaLangObject()) { + // the superclass is j.l.Object, which we know won't access any of a subclass's fields
+          return true;
+        } else {
+ // TODO Look at our superclass constructors to see if any of them call a virtual
+          // method. For now, assume that they might.
+          return false;
+        }
+      }
+      return false;
+    }
   }

   private static class JavaToJsOperatorMap {

--
To view, visit https://gwt-review.googlesource.com/3440
To unsubscribe, visit https://gwt-review.googlesource.com/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I97a06eb36396a8b8659ce9a025b21a9cf93d0500
Gerrit-PatchSet: 1
Gerrit-Project: gwt
Gerrit-Branch: master
Gerrit-Owner: Stephen Haberman <[email protected]>

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
--- You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to