Bug 1242042: Enabling buffering for file stream to write message for C-C TB (Enabling buffering first step.) draft
authorISHIKAWA, Chiaki <ishikawa@yk.rim.or.jp>
Sat, 09 Nov 2019 11:11:57 +0900
changeset 81103 9dd140559a2c2e692c787cbdba37e364b90034f2
parent 81102 a1fa995a885e034c98b434ec943077d204ce27d6
child 81104 230fa4b32682cc499256e04bf8fd930b7aa88383
push id9745
push userishikawa@yk.rim.or.jp
push dateSat, 09 Nov 2019 02:12:14 +0000
treeherdertry-comm-central@f62e5475decc [default view] [failures only]
bugs1242042
Bug 1242042: Enabling buffering for file stream to write message for C-C TB (Enabling buffering first step.)
mailnews/local/src/nsLocalMailFolder.cpp
--- a/mailnews/local/src/nsLocalMailFolder.cpp
+++ b/mailnews/local/src/nsLocalMailFolder.cpp
@@ -1978,17 +1978,44 @@ nsresult nsMsgLocalMailFolder::InitCopyM
   nsresult rv = GetMsgStore(getter_AddRefs(mCopyState->m_msgStore));
   NS_ENSURE_SUCCESS(rv, rv);
   rv = mCopyState->m_msgStore->GetNewMsgOutputStream(
       this, getter_AddRefs(mCopyState->m_newHdr),
       getter_AddRefs(mCopyState->m_fileStream));
   NS_ENSURE_SUCCESS(rv, rv);
   if (mCopyState->m_parseMsgState)
     mCopyState->m_parseMsgState->SetNewMsgHdr(mCopyState->m_newHdr);
-  return rv;
+
+  // sanity check
+  NS_ASSERTION(mCopyState->m_fileStream,
+               "mCopyState->m_fileStream should not be nullptr.");
+  // let us buffer the m_fileStream
+  // We Flush(), but this is for formality to
+  // get internal state right just in case.
+  // No error is expected and error is ignored.
+  mCopyState->m_fileStream->Flush();
+
+  nsCOMPtr<nsIOutputStream> tmp_in = mCopyState->m_fileStream;
+  nsCOMPtr<nsIOutputStream> tmp_out;
+
+  nsresult rv2 = NS_NewBufferedOutputStream(getter_AddRefs(tmp_out),
+                                            tmp_in.forget(), 64 * 1024);
+  mCopyState->m_fileStream = tmp_out;
+#ifdef DEBUG
+  fprintf(stderr,
+          "(debug) creating a buffered mCopyState->m_filestream in "
+          "nsMsgLocalMailFolder::InitCopyMsgHdrAndFileStream.\n");
+  NS_ASSERTION(NS_SUCCEEDED(rv2),
+               "Buffering of mCopyState->m_fileStream via "
+               "NS_NewBufferdOutputStream ought to succeed "
+               "for performance reason.\n");
+#endif
+  NS_ENSURE_SUCCESS(rv2, rv2);
+
+  return NS_OK;  // error will return early.
 }
 
 // nsICopyMessageListener
 NS_IMETHODIMP nsMsgLocalMailFolder::BeginCopy(nsIMsgDBHdr *message) {
   if (!mCopyState) return NS_ERROR_NULL_POINTER;
 
   nsresult rv;
   if (!mCopyState->m_copyingMultipleMessages) {
@@ -2048,16 +2075,41 @@ NS_IMETHODIMP nsMsgLocalMailFolder::Copy
     char *newBuffer = (char *)PR_REALLOC(mCopyState->m_dataBuffer,
                                          aLength + mCopyState->m_leftOver + 4);
     if (!newBuffer) return NS_ERROR_OUT_OF_MEMORY;
 
     mCopyState->m_dataBuffer = newBuffer;
     mCopyState->m_dataBufferSize = aLength + mCopyState->m_leftOver + 3;
   }
 
+#if 0
+  // This seems superfulous as long as |CopyData()| is always called
+  // after |InitCopyMsgHdrAndFileStream()|, where m_fileStream is
+  // buffered, has been invoked.
+
+  //
+  // Flush before seek.
+  // Enable buffering for Write.
+  //
+
+  // sanity check first.
+  NS_ASSERTION(mCopyState->m_fileStream,
+               "mCopyState->m_fileStream should not be null.");
+
+  // just in case. Ignore error here.
+  mCopyState->m_fileStream->Flush(); 
+  mCopyState->m_fileStream = NS_BufferOutputStream(mCopyState->m_fileStream,
+                                                   64 * 1024 );
+#  ifdef DEBUG
+  fprintf(stderr,
+          "(debug) creating a buffered mCopyState->m_filestream in "
+          "nsMsgLocalMailFolder::CopyData().\n");
+#  endif
+#endif
+
   nsCOMPtr<nsISeekableStream> seekableStream =
       do_QueryInterface(mCopyState->m_fileStream, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
   seekableStream->Seek(nsISeekableStream::NS_SEEK_END, 0);
 
   // BTW, under Linux, and for POP3 case, Read is handled by recvfrom in
   // lower-level code with 32KB buffer: verified from strace data