Bug 1385525 - Part 2: Make SetTextTransaction lighter weight by making it a normal C++ class allocated on the stack; r=masayuki
authorEhsan Akhgari <ehsan@mozilla.com>
Sat, 29 Jul 2017 02:13:59 -0400
changeset 420843 dfb4439aa194de33421b2ebfc0b71238b975c2d5
parent 420842 df5e7bd8b6735c863c9f34c04be49089c478535f
child 420844 30a16b6553d79eb6ecc3a91bfdc6fb4ffc3f4f54
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmasayuki
bugs1385525
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1385525 - Part 2: Make SetTextTransaction lighter weight by making it a normal C++ class allocated on the stack; r=masayuki This avoids the cost of allocation on the heap and cycle collection, and removes some dead code.
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorBase.h
editor/libeditor/SetTextTransaction.cpp
editor/libeditor/SetTextTransaction.h
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -2718,21 +2718,17 @@ EditorBase::NotifyDocumentListeners(
 
   return rv;
 }
 
 nsresult
 EditorBase::SetTextImpl(Selection& aSelection, const nsAString& aString,
                         Text& aCharData)
 {
-  RefPtr<SetTextTransaction> transaction =
-    CreateTxnForSetText(aString, aCharData);
-  if (NS_WARN_IF(!transaction)) {
-    return NS_ERROR_FAILURE;
-  }
+  SetTextTransaction transaction(aCharData, aString, *this, &mRangeUpdater);
 
   uint32_t length = aCharData.Length();
 
   AutoRules beginRulesSniffing(this, EditAction::setText,
                                nsIEditor::eNext);
 
   // Let listeners know what's up
   {
@@ -2749,17 +2745,17 @@ EditorBase::SetTextImpl(Selection& aSele
           aString);
       }
     }
   }
 
   // We don't support undo here, so we don't really need all of the transaction
   // machinery, therefore we can run our transaction directly, breaking all of
   // the rules!
-  nsresult rv = transaction->DoTransaction();
+  nsresult rv = transaction.DoTransaction();
 
   // Let listeners know what happened
   {
     AutoActionListenerArray listeners(mActionListeners);
     for (auto& listener : listeners) {
       if (length) {
         listener->DidDeleteText(
           static_cast<nsIDOMCharacterData*>(aCharData.AsDOMNode()), 0,
@@ -2782,25 +2778,16 @@ EditorBase::CreateTxnForInsertText(const
                                    int32_t aOffset)
 {
   RefPtr<InsertTextTransaction> transaction =
     new InsertTextTransaction(aTextNode, aOffset, aStringToInsert, *this,
                               &mRangeUpdater);
   return transaction.forget();
 }
 
-already_AddRefed<SetTextTransaction>
-EditorBase::CreateTxnForSetText(const nsAString& aString,
-                                Text& aTextNode)
-{
-  RefPtr<SetTextTransaction> transaction =
-    new SetTextTransaction(aTextNode, aString, *this, &mRangeUpdater);
-  return transaction.forget();
-}
-
 nsresult
 EditorBase::DeleteText(nsGenericDOMDataNode& aCharData,
                        uint32_t aOffset,
                        uint32_t aLength)
 {
   RefPtr<DeleteTextTransaction> transaction =
     CreateTxnForDeleteText(aCharData, aOffset, aLength);
   NS_ENSURE_STATE(transaction);
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -428,19 +428,16 @@ protected:
   /**
    * Create a transaction for inserting aStringToInsert into aTextNode.  Never
    * returns null.
    */
   already_AddRefed<mozilla::InsertTextTransaction>
     CreateTxnForInsertText(const nsAString& aStringToInsert, Text& aTextNode,
                            int32_t aOffset);
 
-  already_AddRefed<SetTextTransaction>
-    CreateTxnForSetText(const nsAString& aString, Text& aTextNode);
-
   /**
    * Never returns null.
    */
   already_AddRefed<mozilla::CompositionTransaction>
     CreateTxnForComposition(const nsAString& aStringToInsert);
 
   /**
    * Create a transaction for adding a style sheet.
--- a/editor/libeditor/SetTextTransaction.cpp
+++ b/editor/libeditor/SetTextTransaction.cpp
@@ -9,51 +9,33 @@
 #include "mozilla/DebugOnly.h"          // DebugOnly
 #include "mozilla/EditorBase.h"         // mEditorBase
 #include "mozilla/SelectionState.h"     // RangeUpdater
 #include "mozilla/dom/Selection.h"      // Selection local var
 #include "mozilla/dom/Text.h"           // mTextNode
 #include "nsAString.h"                  // nsAString parameter
 #include "nsDebug.h"                    // for NS_ASSERTION, etc.
 #include "nsError.h"                    // for NS_OK, etc.
-#include "nsQueryObject.h"              // for do_QueryObject
 
 namespace mozilla {
 
 using namespace dom;
 
 SetTextTransaction::SetTextTransaction(Text& aTextNode,
                                        const nsAString& aStringToSet,
                                        EditorBase& aEditorBase,
                                        RangeUpdater* aRangeUpdater)
   : mTextNode(&aTextNode)
   , mStringToSet(aStringToSet)
   , mEditorBase(&aEditorBase)
   , mRangeUpdater(aRangeUpdater)
 {
 }
 
-SetTextTransaction::~SetTextTransaction()
-{
-}
-
-NS_IMPL_CYCLE_COLLECTION_INHERITED(SetTextTransaction, EditTransactionBase,
-                                   mEditorBase,
-                                   mTextNode)
-
-NS_IMPL_ADDREF_INHERITED(SetTextTransaction, EditTransactionBase)
-NS_IMPL_RELEASE_INHERITED(SetTextTransaction, EditTransactionBase)
-NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SetTextTransaction)
-  if (aIID.Equals(NS_GET_IID(SetTextTransaction))) {
-    foundInterface = static_cast<nsITransaction*>(this);
-  } else
-NS_INTERFACE_MAP_END_INHERITING(EditTransactionBase)
-
-
-NS_IMETHODIMP
+nsresult
 SetTextTransaction::DoTransaction()
 {
   if (NS_WARN_IF(!mEditorBase) || NS_WARN_IF(!mTextNode)) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   nsresult rv = mTextNode->GetData(mPreviousData);
   if (NS_WARN_IF(NS_FAILED(rv))) {
@@ -77,33 +59,9 @@ SetTextTransaction::DoTransaction()
                  "Selection could not be collapsed after insert");
   }
   mRangeUpdater->SelAdjDeleteText(mTextNode, 0, mPreviousData.Length());
   mRangeUpdater->SelAdjInsertText(*mTextNode, 0, mStringToSet);
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
-SetTextTransaction::UndoTransaction()
-{
-  return mTextNode->SetData(mPreviousData);
-}
-
-NS_IMETHODIMP
-SetTextTransaction::Merge(nsITransaction* aTransaction,
-                          bool* aDidMerge)
-{
-  // Set out param default value
-  *aDidMerge = false;
-
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-SetTextTransaction::GetTxnDescription(nsAString& aString)
-{
-  aString.AssignLiteral("SetTextTransaction: ");
-  aString += mStringToSet;
-  return NS_OK;
-}
-
 } // namespace mozilla
--- a/editor/libeditor/SetTextTransaction.h
+++ b/editor/libeditor/SetTextTransaction.h
@@ -2,79 +2,60 @@
 /* vim: set ts=2 sw=2 et tw=78: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_SetTextTransaction_h
 #define mozilla_SetTextTransaction_h
 
-#include "mozilla/EditTransactionBase.h"  // base class
-#include "nsCycleCollectionParticipant.h" // various macros
-#include "nsID.h"                       // NS_DECLARE_STATIC_IID_ACCESSOR
-#include "nsISupportsImpl.h"            // NS_DECL_ISUPPORTS_INHERITED
+#include "mozilla/Attributes.h"         // for MOZ_STACK_CLASS
 #include "nsString.h"                   // nsString members
 #include "nscore.h"                     // NS_IMETHOD, nsAString
 
-class nsITransaction;
-
-#define NS_SETTEXTTXN_IID \
-{ 0x568bac0b, 0xa42a, 0x4150, \
-  { 0xbd, 0x90, 0x34, 0xd0, 0x2f, 0x32, 0x74, 0x2e } }
-
 namespace mozilla {
 
 class EditorBase;
 class RangeUpdater;
 
 namespace dom {
 class Text;
 } // namespace dom
 
 /**
- * A transaction that inserts text into a content node.
+ * A fake transaction that inserts text into a content node.
+ *
+ * This class mimics a transaction class but it is not intended to be used as one.
  */
-class SetTextTransaction final : public EditTransactionBase
+class MOZ_STACK_CLASS SetTextTransaction final
 {
 public:
-  NS_DECLARE_STATIC_IID_ACCESSOR(NS_SETTEXTTXN_IID)
-
   /**
    * @param aTextNode       The text content node.
    * @param aString         The new text to insert.
    * @param aEditorBase     Used to get and set the selection.
    * @param aRangeUpdater   The range updater
    */
   SetTextTransaction(dom::Text& aTextNode,
                      const nsAString& aString, EditorBase& aEditorBase,
                      RangeUpdater* aRangeUpdater);
 
-  NS_DECL_ISUPPORTS_INHERITED
-  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SetTextTransaction,
-                                           EditTransactionBase)
-
-  NS_DECL_EDITTRANSACTIONBASE
-
-  NS_IMETHOD Merge(nsITransaction* aTransaction, bool* aDidMerge) override;
+  nsresult DoTransaction();
 
 private:
-  virtual ~SetTextTransaction();
-
   // The Text node to operate upon.
   RefPtr<dom::Text> mTextNode;
 
   // The text to insert into mTextNode at mOffset.
   nsString mStringToSet;
 
   // The previous text for undo
   nsString mPreviousData;
 
   // The editor, which we'll need to get the selection.
-  RefPtr<EditorBase> mEditorBase;
+  EditorBase* MOZ_NON_OWNING_REF mEditorBase;
 
   RangeUpdater* mRangeUpdater;
 };
 
-NS_DEFINE_STATIC_IID_ACCESSOR(SetTextTransaction, NS_SETTEXTTXN_IID)
-
 } // namespace mozilla
 
 #endif // #ifndef mozilla_SetTextTransaction_h