Bug 1163916 (part 2) - Avoid a static constructor for kCompressedBufferLength. r=froydnj.
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 13 May 2015 16:48:32 -0700
changeset 243772 4d742ed5c563787a8b2301427390d8f596d11f6d
parent 243771 eee8a7f02b1a5ce65ef3ac1c16338d4e3ade96b1
child 243773 ae5df55492a3590d5f75e48e18160050c812dda8
push id28753
push userkwierso@gmail.com
push dateThu, 14 May 2015 22:33:43 +0000
treeherdermozilla-central@07e2e15703cb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1163916
milestone41.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 1163916 (part 2) - Avoid a static constructor for kCompressedBufferLength. r=froydnj.
xpcom/io/SnappyUncompressInputStream.cpp
--- a/xpcom/io/SnappyUncompressInputStream.cpp
+++ b/xpcom/io/SnappyUncompressInputStream.cpp
@@ -11,18 +11,26 @@
 #include "nsStreamUtils.h"
 #include "snappy/snappy.h"
 
 namespace mozilla {
 
 NS_IMPL_ISUPPORTS(SnappyUncompressInputStream,
                   nsIInputStream);
 
-static size_t kCompressedBufferLength =
-  detail::SnappyFrameUtils::MaxCompressedBufferLength(snappy::kBlockSize);
+// Putting kCompressedBufferLength inside a function avoids a static
+// constructor.
+static size_t CompressedBufferLength()
+{
+  static size_t kCompressedBufferLength =
+      detail::SnappyFrameUtils::MaxCompressedBufferLength(snappy::kBlockSize);
+
+  MOZ_ASSERT(kCompressedBufferLength > 0);
+  return kCompressedBufferLength;
+}
 
 SnappyUncompressInputStream::SnappyUncompressInputStream(nsIInputStream* aBaseStream)
   : mBaseStream(aBaseStream)
   , mUncompressedBytes(0)
   , mNextByte(0)
   , mNextChunkType(Unknown)
   , mNextChunkDataLength(0)
   , mNeedFirstStreamIdentifier(true)
@@ -190,30 +198,30 @@ SnappyUncompressInputStream::ParseNextCh
   if (!mUncompressedBuffer) {
     mUncompressedBuffer.reset(new (fallible) char[snappy::kBlockSize]);
     if (NS_WARN_IF(!mUncompressedBuffer)) {
       return NS_ERROR_OUT_OF_MEMORY;
     }
   }
 
   if (!mCompressedBuffer) {
-    mCompressedBuffer.reset(new (fallible) char[kCompressedBufferLength]);
+    mCompressedBuffer.reset(new (fallible) char[CompressedBufferLength()]);
     if (NS_WARN_IF(!mCompressedBuffer)) {
       return NS_ERROR_OUT_OF_MEMORY;
     }
   }
 
   // We have no decompressed data and we also have not seen the start of stream
   // yet. Read and validate the StreamIdentifier chunk.  Also read the next
   // header to determine the size of the first real data chunk.
   if (mNeedFirstStreamIdentifier) {
     const uint32_t firstReadLength = kHeaderLength +
                                      kStreamIdentifierDataLength +
                                      kHeaderLength;
-    MOZ_ASSERT(firstReadLength <= kCompressedBufferLength);
+    MOZ_ASSERT(firstReadLength <= CompressedBufferLength());
 
     rv = ReadAll(mCompressedBuffer.get(), firstReadLength, firstReadLength,
                  aBytesReadOut);
     if (NS_WARN_IF(NS_FAILED(rv)) || *aBytesReadOut == 0) { return rv; }
 
     rv = ParseHeader(mCompressedBuffer.get(), kHeaderLength,
                      &mNextChunkType, &mNextChunkDataLength);
     if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
@@ -256,26 +264,26 @@ SnappyUncompressInputStream::ParseNextCh
     if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
 
     return NS_OK;
   }
 
   // We have no decompressed data, but we do know the size of the next chunk.
   // Read at least that much from the base stream.
   uint32_t readLength = mNextChunkDataLength;
-  MOZ_ASSERT(readLength <= kCompressedBufferLength);
+  MOZ_ASSERT(readLength <= CompressedBufferLength());
 
   // However, if there is enough data in the base stream, also read the next
   // chunk header.  This helps optimize the stream by avoiding many small reads.
   uint64_t avail;
   rv = mBaseStream->Available(&avail);
   if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
   if (avail >= (readLength + kHeaderLength)) {
     readLength += kHeaderLength;
-    MOZ_ASSERT(readLength <= kCompressedBufferLength);
+    MOZ_ASSERT(readLength <= CompressedBufferLength());
   }
 
   rv = ReadAll(mCompressedBuffer.get(), readLength, mNextChunkDataLength,
                aBytesReadOut);
   if (NS_WARN_IF(NS_FAILED(rv)) || *aBytesReadOut == 0) { return rv; }
 
   size_t numRead;
   size_t numWritten;