Bug 479625 - 'Avoid unnecessary work and long-lived strings in nsDOMWorkerMessageEvent'. r+sr=jst, a=jst.

This commit is contained in:
Ben Turner 2009-03-19 12:37:43 -07:00
parent 1f7d39182f
commit f0a75fbf31
2 changed files with 30 additions and 11 deletions

View File

@ -283,11 +283,18 @@ nsDOMWorkerMessageEvent::GetData(nsAString& aData)
rv = cc->GetRetValPtr(&retval);
NS_ENSURE_SUCCESS(rv, rv);
if (mCachedJSVal) {
if (mHaveCachedJSVal) {
cc->SetReturnValueWasSet(PR_TRUE);
*retval = mCachedJSVal;
return cc->SetReturnValueWasSet(PR_TRUE);
return NS_OK;
}
if (mHaveAttemptedConversion) {
// Don't try to convert again if the first time around we saw an error.
return NS_ERROR_FAILURE;
}
mHaveAttemptedConversion = PR_TRUE;
JSContext* cx;
rv = cc->GetJSContext(&cx);
NS_ENSURE_SUCCESS(rv, rv);
@ -328,8 +335,16 @@ nsDOMWorkerMessageEvent::GetData(nsAString& aData)
mCachedJSVal = primitive;
}
// We no longer need to hold this copy of the data around.
mData.Truncate();
// Now that everything has succeeded we'll set this flag so that we return the
// cached jsval in the future.
mHaveCachedJSVal = PR_TRUE;
*retval = mCachedJSVal;
return cc->SetReturnValueWasSet(PR_TRUE);
cc->SetReturnValueWasSet(PR_TRUE);
return NS_OK;
}
NS_IMETHODIMP

View File

@ -211,21 +211,25 @@ public:
NS_DECL_NSICLASSINFO_GETINTERFACES
nsDOMWorkerMessageEvent()
: mIsJSON(PR_FALSE), mIsPrimitive(PR_FALSE) { }
: mIsJSON(PR_FALSE), mIsPrimitive(PR_FALSE), mHaveCachedJSVal(PR_FALSE),
mHaveAttemptedConversion(PR_FALSE) { }
void SetJSONData(PRBool aIsJSON, PRBool aIsPrimitive) {
mIsJSON = aIsJSON;
mIsPrimitive = aIsPrimitive;
mIsJSON = aIsJSON ? PR_TRUE : PR_FALSE;
mIsPrimitive = aIsPrimitive ? PR_TRUE : PR_FALSE;
}
protected:
nsString mData;
PRBool mIsJSON;
PRBool mIsPrimitive;
nsAutoJSValHolder mCachedJSVal;
nsString mOrigin;
nsCOMPtr<nsISupports> mSource;
nsString mData;
nsAutoJSValHolder mCachedJSVal;
PRPackedBool mIsJSON;
PRPackedBool mIsPrimitive;
PRPackedBool mHaveCachedJSVal;
PRPackedBool mHaveAttemptedConversion;
};
class nsDOMWorkerProgressEvent : public nsDOMWorkerEvent,