Cache Document.get for DevMode. Kills thousands of JSNI calls.

Review at http://gwt-code-reviews.appspot.com/1338806


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9746 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/dom/client/Document.java b/user/src/com/google/gwt/dom/client/Document.java
index 8981878..b0778fa 100644
--- a/user/src/com/google/gwt/dom/client/Document.java
+++ b/user/src/com/google/gwt/dom/client/Document.java
@@ -15,6 +15,8 @@
  */
 package com.google.gwt.dom.client;
 
+import com.google.gwt.core.client.GWT;
+
 /**
  * A Document is the root of the HTML hierarchy and holds the entire content.
  * Besides providing access to the hierarchy, it also provides some convenience
@@ -23,12 +25,30 @@
 public class Document extends Node {
 
   /**
+   * We cache Document.nativeGet() in DevMode, because crossing the JSNI
+   * boundary thousands of times just to read a constant value is slow. 
+   */
+  private static Document doc;
+  
+  /**
    * Gets the default document. This is the document in which the module is
    * running.
    * 
    * @return the default document
    */
-  public static native Document get() /*-{
+  public static Document get() {
+    if (GWT.isScript()) {
+      return nativeGet();
+    }
+    
+    // No need to be MT-safe. Single-threaded JS code.
+    if (doc == null) {
+      doc = nativeGet();
+    }
+    return doc;
+  }
+
+  private static native Document nativeGet() /*-{
     return $doc;
   }-*/;