Bug 458440 - Turn speculative parsing back on and clean up the code a little. r+sr=jst
authorBlake Kaplan <mrbkap@gmail.com>
Tue, 11 Nov 2008 10:52:40 -0800
changeset 21569 db2dd52d47c1
parent 21568 d11e76e6fab9
child 21570 37b3fdbb0f07
child 21666 729761b2f486
push id3586
push usermrbkap@mozilla.com
push dateTue, 11 Nov 2008 18:52:59 +0000
treeherdermozilla-central@db2dd52d47c1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs458440
milestone1.9.1b2pre
Bug 458440 - Turn speculative parsing back on and clean up the code a little. r+sr=jst
parser/htmlparser/src/nsHTMLTokenizer.cpp
parser/htmlparser/src/nsHTMLTokens.cpp
parser/htmlparser/src/nsParser.cpp
parser/htmlparser/src/nsParser.h
--- a/parser/htmlparser/src/nsHTMLTokenizer.cpp
+++ b/parser/htmlparser/src/nsHTMLTokenizer.cpp
@@ -46,17 +46,16 @@
  * older parsers and the SGML specification. Note that most of the real
  * "tokenization" takes place in nsHTMLTokens.cpp.
  */
 
 #include "nsIAtom.h"
 #include "nsHTMLTokenizer.h"
 #include "nsScanner.h"
 #include "nsElementTable.h"
-#include "CParserContext.h"
 #include "nsReadableUtils.h"
 #include "nsUnicharUtils.h"
 
 /************************************************************************
   And now for the main class -- nsHTMLTokenizer...
  ************************************************************************/
 
 /**
--- a/parser/htmlparser/src/nsHTMLTokens.cpp
+++ b/parser/htmlparser/src/nsHTMLTokens.cpp
@@ -37,17 +37,16 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include <ctype.h>
 #include <time.h>
 #include <stdio.h>
 #include "nsScanner.h"
 #include "nsToken.h"
-#include "nsIAtom.h"
 #include "nsHTMLTokens.h"
 #include "prtypes.h"
 #include "nsDebug.h"
 #include "nsHTMLTags.h"
 #include "nsHTMLEntities.h"
 #include "nsCRT.h"
 #include "nsReadableUtils.h"
 #include "nsUnicharUtils.h"
--- a/parser/htmlparser/src/nsParser.cpp
+++ b/parser/htmlparser/src/nsParser.cpp
@@ -195,17 +195,17 @@ public:
 private:
   Type *mHoldee;
   Reaper mReaper;
 };
 
 class nsSpeculativeScriptThread : public nsIRunnable {
 public:
   nsSpeculativeScriptThread()
-    : mLock(PR_DestroyLock),
+    : mLock(nsAutoLock::DestroyLock),
       mCVar(PR_DestroyCondVar),
       mKeepParsing(0),
       mCurrentlyParsing(0),
       mNumURIs(0),
       mNumConsumed(0),
       mContext(nsnull),
       mTerminated(PR_FALSE) {
   }
@@ -252,33 +252,33 @@ public:
     StopParsing(PR_FALSE);
   }
   PRBool Terminated() {
     return mTerminated;
   }
 
 private:
 
-  nsresult ProcessToken(CToken *aToken);
+  void ProcessToken(CToken *aToken);
 
   void AddToPrefetchList(const nsAString &src,
                          const nsAString &charset,
                          const nsAString &elementType,
                          PrefetchType type);
 
   // These members are only accessed on the speculatively parsing thread.
   nsTokenAllocator mTokenAllocator;
 
   // The following members are shared across the main thread and the
   // speculatively parsing thread.
   Holder<PRLock> mLock;
   Holder<PRCondVar> mCVar;
 
-  PRUint32 mKeepParsing;
-  PRUint32 mCurrentlyParsing;
+  volatile PRUint32 mKeepParsing;
+  volatile PRUint32 mCurrentlyParsing;
   nsRefPtr<nsHTMLTokenizer> mTokenizer;
   nsAutoPtr<nsScanner> mScanner;
 
   enum { kBatchPrefetchURIs = 5 };
   nsAutoTArray<PrefetchEntry, kBatchPrefetchURIs> mURIs;
   PRUint16 mNumURIs;
 
   // Number of characters consumed by the last speculative parse.
@@ -376,34 +376,36 @@ nsSpeculativeScriptThread::Run()
   NS_ASSERTION(!NS_IsMainThread(), "Speculative parsing on the main thread?");
 
   mNumConsumed = 0;
 
   mTokenizer->WillTokenize(PR_FALSE, &mTokenAllocator);
   while (mKeepParsing) {
     PRBool flushTokens = PR_FALSE;
     nsresult rv = mTokenizer->ConsumeToken(*mScanner, flushTokens);
-    if (rv == kEOF) {
+    if (NS_FAILED(rv)) {
       break;
     }
 
     mNumConsumed += mScanner->Mark();
 
     // TODO Don't pop the tokens.
     CToken *token;
-    while (mKeepParsing && NS_SUCCEEDED(rv) && (token = mTokenizer->PopToken())) {
-      rv = ProcessToken(token);
+    while (mKeepParsing && (token = mTokenizer->PopToken())) {
+      ProcessToken(token);
     }
   }
   mTokenizer->DidTokenize(PR_FALSE);
 
-  nsAutoLock al(mLock.get());
-
-  mCurrentlyParsing = 0;
-  PR_NotifyCondVar(mCVar.get());
+  {
+    nsAutoLock al(mLock.get());
+
+    mCurrentlyParsing = 0;
+    PR_NotifyCondVar(mCVar.get());
+  }
   return NS_OK;
 }
 
 nsresult
 nsSpeculativeScriptThread::StartParsing(nsParser *aParser)
 {
   NS_ASSERTION(NS_IsMainThread(), "Called on the wrong thread");
   NS_ASSERTION(!mCurrentlyParsing, "Bad race happening");
@@ -420,17 +422,17 @@ nsSpeculativeScriptThread::StartParsing(
   nsCOMPtr<nsIDocument> doc = do_QueryInterface(sink->GetTarget());
   if (!doc) {
     return NS_OK;
   }
 
   nsAutoString toScan;
   CParserContext *context = aParser->PeekContext();
   if (!mLock.get()) {
-    mLock = PR_NewLock();
+    mLock = nsAutoLock::NewLock("nsSpeculativeScriptThread::mLock");
     if (!mLock.get()) {
       return NS_ERROR_OUT_OF_MEMORY;
     }
 
     mCVar = PR_NewCondVar(mLock.get());
     if (!mCVar.get()) {
       return NS_ERROR_OUT_OF_MEMORY;
     }
@@ -491,17 +493,17 @@ nsSpeculativeScriptThread::StartParsing(
   mDocument.swap(doc);
   mKeepParsing = 1;
   mCurrentlyParsing = 1;
   mContext = context;
   return aParser->ThreadPool()->Dispatch(this, NS_DISPATCH_NORMAL);
 }
 
 void
-nsSpeculativeScriptThread::StopParsing(PRBool aFromDocWrite)
+nsSpeculativeScriptThread::StopParsing(PRBool /*aFromDocWrite*/)
 {
   NS_ASSERTION(NS_IsMainThread(), "Can't stop parsing from another thread");
 
   if (!mLock.get()) {
     // If we bailed early out of StartParsing, don't do anything.
     return;
   }
 
@@ -528,20 +530,19 @@ nsSpeculativeScriptThread::StopParsing(P
     nsPreloadURIs::PreloadURIs(mURIs, this);
     mNumURIs = 0;
     mURIs.Clear();
   }
 
   // Note: Currently, we pop the tokens off (see the comment in Run) so this
   // isn't a problem. If and when we actually use the tokens created
   // off-thread, we'll need to use aFromDocWrite for real.
-  (void)aFromDocWrite;
 }
 
-nsresult
+void
 nsSpeculativeScriptThread::ProcessToken(CToken *aToken)
 {
   // Only called on the speculative script thread.
 
   CHTMLToken *token = static_cast<CHTMLToken *>(aToken);
   switch (static_cast<eHTMLTokenTypes>(token->GetTokenType())) {
     case eToken_start: {
         CStartToken *start = static_cast<CStartToken *>(aToken);
@@ -630,17 +631,16 @@ nsSpeculativeScriptThread::ProcessToken(
         break;
       }
 
     default:
       break;
   }
 
   IF_FREE(aToken, &mTokenAllocator);
-  return NS_OK;
 }
 
 void
 nsSpeculativeScriptThread::AddToPrefetchList(const nsAString &src,
                                       const nsAString &charset,
                                       const nsAString &elementType,
                                       PrefetchType type)
 {
@@ -1533,34 +1533,32 @@ nsParser::DidBuildModel(nsresult anError
   }
 
   return result;
 }
 
 void
 nsParser::SpeculativelyParse()
 {
-#if 0 // Disable temporarily to see if this is the cause of the bustage.
   if (mParserContext->mParserCommand == eViewNormal &&
       !mParserContext->mMimeType.EqualsLiteral("text/html")) {
     return;
   }
 
   if (!mSpeculativeScriptThread) {
     mSpeculativeScriptThread = new nsSpeculativeScriptThread();
     if (!mSpeculativeScriptThread) {
       return;
     }
   }
 
   nsresult rv = mSpeculativeScriptThread->StartParsing(this);
   if (NS_FAILED(rv)) {
     mSpeculativeScriptThread = nsnull;
   }
-#endif
 }
 
 /**
  * This method adds a new parser context to the list,
  * pushing the current one to the next position.
  *
  * @param   ptr to new context
  */
--- a/parser/htmlparser/src/nsParser.h
+++ b/parser/htmlparser/src/nsParser.h
@@ -89,31 +89,28 @@
 #include "nsCOMArray.h"
 #include "nsIUnicharStreamListener.h"
 #include "nsCycleCollectionParticipant.h"
 
 class nsICharsetConverterManager;
 class nsICharsetAlias;
 class nsIDTD;
 class nsScanner;
-class nsIProgressEventSink;
 class nsSpeculativeScriptThread;
 class nsIThreadPool;
 
 #ifdef _MSC_VER
 #pragma warning( disable : 4275 )
 #endif
 
 
 class nsParser : public nsIParser,
-                 public nsIStreamListener{
-
-  
+                 public nsIStreamListener
+{
   public:
-    friend class CTokenHandler;
     /**
      * Called on module init
      */
     static nsresult Init();
 
     /**
      * Called on module shutdown
      */