Bug 927407 - Optimizing NS_{Input, Output}StreamIsBuffered to avoid unnecessary main thread I/O in trivial cases

This commit is contained in:
David Rajchenbach-Teller 2013-10-16 07:42:00 +01:00
parent 15fba679b5
commit 87f3ec791b
2 changed files with 26 additions and 4 deletions

View File

@ -15,6 +15,7 @@
#include "nsString.h"
#include "nsIAsyncInputStream.h"
#include "nsIAsyncOutputStream.h"
#include "nsIBufferedStreams.h"
using namespace mozilla;
@ -668,6 +669,11 @@ TestInputStream(nsIInputStream *inStr,
bool
NS_InputStreamIsBuffered(nsIInputStream *stream)
{
nsCOMPtr<nsIBufferedInputStream> bufferedIn = do_QueryInterface(stream);
if (bufferedIn) {
return true;
}
bool result = false;
uint32_t n;
nsresult rv = stream->ReadSegments(TestInputStream,
@ -691,6 +697,11 @@ TestOutputStream(nsIOutputStream *outStr,
bool
NS_OutputStreamIsBuffered(nsIOutputStream *stream)
{
nsCOMPtr<nsIBufferedOutputStream> bufferedOut = do_QueryInterface(stream);
if (bufferedOut) {
return true;
}
bool result = false;
uint32_t n;
stream->WriteSegments(TestOutputStream, &result, 1, &n);

View File

@ -131,15 +131,20 @@ NS_ConsumeStream(nsIInputStream *aSource, uint32_t aMaxCount,
nsACString &aBuffer);
/**
* This function tests whether or not the input stream is buffered. A buffered
* This function tests whether or not the input stream is buffered. A buffered
* input stream is one that implements readSegments. The test for this is to
* simply call readSegments, without actually consuming any data from the
* 1/ check whether the input stream implements nsIBufferedInputStream;
* 2/ if not, call readSegments, without actually consuming any data from the
* stream, to verify that it functions.
*
* NOTE: If the stream is non-blocking and has no data available yet, then this
* test will fail. In that case, we return false even though the test is not
* test will fail. In that case, we return false even though the test is not
* really conclusive.
*
* PERFORMANCE NOTE: If the stream does not implement nsIBufferedInputStream,
* calling readSegments may cause I/O. Therefore, you should avoid calling
* this function from the main thread.
*
* @param aInputStream
* The input stream to test.
*/
@ -149,13 +154,19 @@ NS_InputStreamIsBuffered(nsIInputStream *aInputStream);
/**
* This function tests whether or not the output stream is buffered. A
* buffered output stream is one that implements writeSegments. The test for
* this is to simply call writeSegments, without actually writing any data into
* this is to:
* 1/ check whether the output stream implements nsIBufferedOutputStream;
* 2/ if not, call writeSegments, without actually writing any data into
* the stream, to verify that it functions.
*
* NOTE: If the stream is non-blocking and has no available space yet, then
* this test will fail. In that case, we return false even though the test is
* not really conclusive.
*
* PERFORMANCE NOTE: If the stream does not implement nsIBufferedOutputStream,
* calling writeSegments may cause I/O. Therefore, you should avoid calling
* this function from the main thread.
*
* @param aOutputStream
* The output stream to test.
*/