diff options
author | Matúš Kukan <matus.kukan@collabora.com> | 2014-09-30 13:53:26 +0200 |
---|---|---|
committer | Matúš Kukan <matus.kukan@collabora.com> | 2014-10-23 13:24:25 +0200 |
commit | dbbbea666251175c2a4f9b0387d53ff097c65872 (patch) | |
tree | e0fafe223dfe89cb8f39968c7d156d5e9b3f7f64 /sax | |
parent | 659064475d78b19f06a21985c4cc1feb50c9ca03 (diff) |
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
Diffstat (limited to 'sax')
-rw-r--r-- | sax/source/tools/CachedOutputStream.hxx | 46 |
1 files changed, 13 insertions, 33 deletions
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<sal_Int8 *>(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<sal_Int8>(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<sal_Int8 *>(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<sal_Int8>(pStr, nLen) ); + return; + } } - assert(mnCacheWrittenSize + nLen <= mnCacheAllocatedSize); + memcpy(mpCache + mnCacheWrittenSize, pStr, nLen); mnCacheWrittenSize += nLen; } |