Bug 592422 - preallocate individual (non-block) cache files. r=jduell,taras a=b
☠☠ backed out by dcbc869144d4 ☠ ☠
authorMichal Novotny <michal.novotny@gmail.com>
Wed, 22 Sep 2010 11:47:46 +0200
changeset 56301 e72707266b4aabf7e9e894defd9828b9deb0196e
parent 56300 80a39b6421b574f71aadd4f1231427e5089eda68
child 56302 0cf0c2f57f292ec1d82084c80bf176fd0f5f82f0
child 56304 dcbc869144d4b5f0432473f94b171a868b3e804a
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjduell, taras, b
bugs592422
milestone2.0b8pre
Bug 592422 - preallocate individual (non-block) cache files. r=jduell,taras a=b
netwerk/cache/nsDiskCacheMap.h
netwerk/cache/nsDiskCacheStreams.cpp
xpcom/glue/FileUtils.cpp
--- a/netwerk/cache/nsDiskCacheMap.h
+++ b/netwerk/cache/nsDiskCacheMap.h
@@ -89,16 +89,19 @@ struct nsDiskCacheEntry;
 // Min and max values for the number of records in the DiskCachemap
 #define kMinRecordCount    512
 
 #define kSeparateFile      0
 // #define must always  be <= 65535KB, or overflow. See bug 443067 Comment 8
 #define kMaxDataFileSize   5 * 1024 * 1024  // 5 MB (in bytes) 
 #define kBuckets           (1 << 5)    // must be a power of 2!
 
+// preallocate up to 1MB of separate cache file
+#define kPreallocateLimit  1 * 1024 * 1024
+
 class nsDiskCacheRecord {
 
 private:
     PRUint32    mHashNumber;
     PRUint32    mEvictionRank;
     PRUint32    mDataLocation;
     PRUint32    mMetaLocation;
  
--- a/netwerk/cache/nsDiskCacheStreams.cpp
+++ b/netwerk/cache/nsDiskCacheStreams.cpp
@@ -38,16 +38,17 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 
 #include "nsDiskCache.h"
 #include "nsDiskCacheDevice.h"
 #include "nsDiskCacheStreams.h"
 #include "nsCacheService.h"
+#include "mozilla/FileUtils.h"
 
 
 
 // Assumptions:
 //      - cache descriptors live for life of streams
 //      - streams will only be used by FileTransport,
 //         they will not be directly accessible to clients
 //      - overlapped I/O is NOT supported
@@ -723,16 +724,20 @@ nsDiskCacheStreamIO::FlushBufferToFile()
             rv = cacheMap->DeleteStorage(record, nsDiskCache::kData);
             if (NS_FAILED(rv))  return rv;
         }
         record->SetDataFileGeneration(mBinding->mGeneration);
         
         // allocate file
         rv = OpenCacheFile(PR_RDWR | PR_CREATE_FILE, &mFD);
         if (NS_FAILED(rv))  return rv;
+
+        PRInt64 dataSize = mBinding->mCacheEntry->PredictedDataSize();
+        if (dataSize != -1)
+            mozilla::fallocate(mFD, PR_MIN(dataSize, kPreallocateLimit));
     }
     
     // write buffer
     PRInt32 bytesWritten = PR_Write(mFD, mBuffer, mBufEnd);
     if (PRUint32(bytesWritten) != mBufEnd) {
         NS_WARNING("failed to flush all data");
         return NS_ERROR_UNEXPECTED;     // NS_ErrorAccordingToNSPR()
     }
--- a/xpcom/glue/FileUtils.cpp
+++ b/xpcom/glue/FileUtils.cpp
@@ -42,20 +42,22 @@
 #include <sys/stat.h>
 #elif defined(XP_WIN)
 #include <windows.h>
 #endif
 
 #include "nscore.h"
 #include "private/pprio.h"
 #include "mozilla/FileUtils.h"
+#include "mozilla/FunctionTimer.h"
 
 bool 
 mozilla::fallocate(PRFileDesc *aFD, PRInt64 aLength) 
 {
+  NS_TIME_FUNCTION;
 #if defined(HAVE_POSIX_FALLOCATE)
   return posix_fallocate(PR_FileDesc2NativeHandle(aFD), 0, aLength) == 0;
 #elif defined(XP_WIN)
   return PR_Seek64(aFD, aLength, PR_SEEK_SET) == aLength
     && 0 != SetEndOfFile((HANDLE)PR_FileDesc2NativeHandle(aFD));
 #elif defined(XP_MACOSX)
   int fd = PR_FileDesc2NativeHandle(aFD);
   fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, aLength};