diff options
author | Thomas Nguyen <tnguyen@mozilla.com> | 2017-05-19 13:17:07 +0800 |
---|---|---|
committer | wolfbeast <mcwerewolf@gmail.com> | 2018-04-19 11:33:20 +0200 |
commit | 823ab285b6cb2f73102b6c68ba5ee095a4d43439 (patch) | |
tree | 25990a4691873435231e0b731ae9543d47663404 | |
parent | 2e66d7415f8d49a190d9f92234b4c316b00a2c96 (diff) | |
download | uxp-823ab285b6cb2f73102b6c68ba5ee095a4d43439.tar.gz |
Bug 1361699 - Add buffer when writing hashstore to file. r=gcp, r=mcmanus, a=RyanVM
We write a lot of 4-bytes prefixes to file which call many system calls.
We should use a buffer and only write to file if the buffer is full or
finish writing. nsIBufferedOutputStream is a good candidate to do that
MozReview-Commit-ID: CzGOd7iXVTv
--HG--
extra : source : 8f2b4efc5f0d3191a7e80d9324933621e111b44a
4 files changed, 26 insertions, 26 deletions
diff --git a/netwerk/base/nsBufferedStreams.h b/netwerk/base/nsBufferedStreams.h index 93a770bebf..fee55695aa 100644 --- a/netwerk/base/nsBufferedStreams.h +++ b/netwerk/base/nsBufferedStreams.h @@ -88,10 +88,10 @@ protected: //////////////////////////////////////////////////////////////////////////////// -class nsBufferedOutputStream final : public nsBufferedStream, - public nsISafeOutputStream, - public nsIBufferedOutputStream, - public nsIStreamBufferAccess +class nsBufferedOutputStream : public nsBufferedStream, + public nsISafeOutputStream, + public nsIBufferedOutputStream, + public nsIStreamBufferAccess { public: NS_DECL_ISUPPORTS_INHERITED diff --git a/toolkit/components/url-classifier/HashStore.cpp b/toolkit/components/url-classifier/HashStore.cpp index c298612aa4..77bf3cbd4d 100644 --- a/toolkit/components/url-classifier/HashStore.cpp +++ b/toolkit/components/url-classifier/HashStore.cpp @@ -964,8 +964,7 @@ HashStore::WriteFile() NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIOutputStream> out; - rv = NS_NewCheckSummedOutputStream(getter_AddRefs(out), storeFile, - PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE); + rv = NS_NewCheckSummedOutputStream(getter_AddRefs(out), storeFile); NS_ENSURE_SUCCESS(rv, rv); uint32_t written; diff --git a/toolkit/components/url-classifier/nsCheckSummedOutputStream.cpp b/toolkit/components/url-classifier/nsCheckSummedOutputStream.cpp index 68f9f1f6f4..0e89fd20c5 100644 --- a/toolkit/components/url-classifier/nsCheckSummedOutputStream.cpp +++ b/toolkit/components/url-classifier/nsCheckSummedOutputStream.cpp @@ -13,14 +13,11 @@ // nsCheckSummedOutputStream NS_IMPL_ISUPPORTS_INHERITED(nsCheckSummedOutputStream, - nsSafeFileOutputStream, - nsISafeOutputStream, - nsIOutputStream, - nsIFileOutputStream) + nsBufferedOutputStream, + nsISafeOutputStream) NS_IMETHODIMP -nsCheckSummedOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm, - int32_t behaviorFlags) +nsCheckSummedOutputStream::Init(nsIOutputStream* stream, uint32_t bufferSize) { nsresult rv; mHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv); @@ -29,7 +26,7 @@ nsCheckSummedOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm, rv = mHash->Init(nsICryptoHash::MD5); NS_ENSURE_SUCCESS(rv, rv); - return nsSafeFileOutputStream::Init(file, ioFlags, perm, behaviorFlags); + return nsBufferedOutputStream::Init(stream, bufferSize); } NS_IMETHODIMP @@ -39,12 +36,12 @@ nsCheckSummedOutputStream::Finish() NS_ENSURE_SUCCESS(rv, rv); uint32_t written; - rv = nsSafeFileOutputStream::Write(reinterpret_cast<const char*>(mCheckSum.BeginReading()), + rv = nsBufferedOutputStream::Write(reinterpret_cast<const char*>(mCheckSum.BeginReading()), mCheckSum.Length(), &written); NS_ASSERTION(written == mCheckSum.Length(), "Error writing stream checksum"); NS_ENSURE_SUCCESS(rv, rv); - return nsSafeFileOutputStream::Finish(); + return nsBufferedOutputStream::Finish(); } NS_IMETHODIMP @@ -53,7 +50,7 @@ nsCheckSummedOutputStream::Write(const char *buf, uint32_t count, uint32_t *resu nsresult rv = mHash->Update(reinterpret_cast<const uint8_t*>(buf), count); NS_ENSURE_SUCCESS(rv, rv); - return nsSafeFileOutputStream::Write(buf, count, result); + return nsBufferedOutputStream::Write(buf, count, result); } //////////////////////////////////////////////////////////////////////////////// diff --git a/toolkit/components/url-classifier/nsCheckSummedOutputStream.h b/toolkit/components/url-classifier/nsCheckSummedOutputStream.h index c2fe26b5f5..b72c7da869 100644 --- a/toolkit/components/url-classifier/nsCheckSummedOutputStream.h +++ b/toolkit/components/url-classifier/nsCheckSummedOutputStream.h @@ -12,25 +12,27 @@ #include "nsICryptoHash.h" #include "nsNetCID.h" #include "nsString.h" -#include "../../../netwerk/base/nsFileStreams.h" #include "nsToolkitCompsCID.h" +#include "../../../netwerk/base/nsBufferedStreams.h" +#include "prio.h" -class nsCheckSummedOutputStream : public nsSafeFileOutputStream +class nsCheckSummedOutputStream : public nsBufferedOutputStream { public: NS_DECL_ISUPPORTS_INHERITED // Size of MD5 hash in bytes static const uint32_t CHECKSUM_SIZE = 16; + static const uint32_t MAX_BUFFER_SIZE = 64 * 1024; nsCheckSummedOutputStream() {} NS_IMETHOD Finish() override; NS_IMETHOD Write(const char *buf, uint32_t count, uint32_t *result) override; - NS_IMETHOD Init(nsIFile* file, int32_t ioFlags, int32_t perm, int32_t behaviorFlags) override; + NS_IMETHOD Init(nsIOutputStream* stream, uint32_t bufferSize) override; protected: - virtual ~nsCheckSummedOutputStream() { nsSafeFileOutputStream::Close(); } + virtual ~nsCheckSummedOutputStream() { nsBufferedOutputStream::Close(); } nsCOMPtr<nsICryptoHash> mHash; nsCString mCheckSum; @@ -39,13 +41,15 @@ protected: // returns a file output stream which can be QI'ed to nsIFileOutputStream. inline nsresult NS_NewCheckSummedOutputStream(nsIOutputStream **result, - nsIFile *file, - int32_t ioFlags = -1, - int32_t perm = -1, - int32_t behaviorFlags = 0) + nsIFile *file) { - nsCOMPtr<nsIFileOutputStream> out = new nsCheckSummedOutputStream(); - nsresult rv = out->Init(file, ioFlags, perm, behaviorFlags); + nsCOMPtr<nsIOutputStream> localOutFile; + nsresult rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(localOutFile), file, + PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr<nsIBufferedOutputStream> out = new nsCheckSummedOutputStream(); + rv = out->Init(localOutFile, nsCheckSummedOutputStream::CHECKSUM_SIZE); if (NS_SUCCEEDED(rv)) { out.forget(result); } |