From dbbbea666251175c2a4f9b0387d53ff097c65872 Mon Sep 17 00:00:00 2001 From: Matúš Kukan Date: Tue, 30 Sep 2014 13:53:26 +0200 Subject: FastSerializer: Simplify cache to be fixed sized buffer This makes writeBytes(), which is called a lot, simpler and thus faster. E.g. for ~15m calls, this saves ~110m pcycles. Change-Id: I29d01a1a8651f668aff574e0f015cd2f018eb1cd --- sax/source/tools/CachedOutputStream.hxx | 46 ++++++++++----------------------- 1 file changed, 13 insertions(+), 33 deletions(-) (limited to 'sax') diff --git a/sax/source/tools/CachedOutputStream.hxx b/sax/source/tools/CachedOutputStream.hxx index 56b17fb92c85..82c2b6624840 100644 --- a/sax/source/tools/CachedOutputStream.hxx +++ b/sax/source/tools/CachedOutputStream.hxx @@ -22,28 +22,17 @@ namespace sax_fastparser { class CachedOutputStream { - /// realloc aligns to this value - static const sal_Int32 mnMinimumResize = 0x1000; /// When buffer hits this size, it's written to mxOutputStream static const sal_Int32 mnMaximumSize = 0x10000; /// Output stream, usually writing data into files. css::uno::Reference< css::io::XOutputStream > mxOutputStream; - sal_Int32 mnCacheAllocatedSize; sal_Int32 mnCacheWrittenSize; - sal_Int8* mpCache; + sal_Int8 mpCache[ mnMaximumSize ]; public: - CachedOutputStream() : mnCacheAllocatedSize(mnMinimumResize) - , mnCacheWrittenSize(0) - { - mpCache = static_cast(malloc(mnCacheAllocatedSize)); - } - - ~CachedOutputStream() - { - free(mpCache); - } + CachedOutputStream() : mnCacheWrittenSize(0) {} + ~CachedOutputStream() {} css::uno::Reference< css::io::XOutputStream > getOutputStream() const { @@ -58,30 +47,21 @@ public: /// cache string and if limit is hit, flush void writeBytes( const sal_Int8* pStr, sal_Int32 nLen ) { - // Writer does some elements sorting, so it can accumulate - // pretty big strings in FastSaxSerializer::ForMerge. - // In that case, just flush data and write immediately. - if (nLen > mnMaximumSize) - { - flush(); - mxOutputStream->writeBytes( css::uno::Sequence(pStr, nLen) ); - return; - } - // Write when the buffer gets big enough if (mnCacheWrittenSize + nLen > mnMaximumSize) + { flush(); - sal_Int32 nMissingBytes = mnCacheWrittenSize + nLen - mnCacheAllocatedSize; - // Ensure the buffer has enough space left - if (nMissingBytes > 0) - { - // Round off to the next multiple of mnMinimumResize - mnCacheAllocatedSize = mnCacheAllocatedSize + - ((nMissingBytes + mnMinimumResize - 1) / mnMinimumResize) * mnMinimumResize; - mpCache = static_cast(realloc(mpCache, mnCacheAllocatedSize)); + // Writer does some elements sorting, so it can accumulate + // pretty big strings in FastSaxSerializer::ForMerge. + // In that case, just flush data and write immediately. + if (nLen > mnMaximumSize) + { + mxOutputStream->writeBytes( css::uno::Sequence(pStr, nLen) ); + return; + } } - assert(mnCacheWrittenSize + nLen <= mnCacheAllocatedSize); + memcpy(mpCache + mnCacheWrittenSize, pStr, nLen); mnCacheWrittenSize += nLen; } -- cgit