cargilld 2004/12/13 08:36:43 Modified: c/src/xercesc/framework XMLBuffer.cpp XMLBuffer.hpp Log: Performance improvement from Christian Will and bug fix from David Bertoni. Revision Changes Path 1.8 +19 -24 xml-xerces/c/src/xercesc/framework/XMLBuffer.cpp Index: XMLBuffer.cpp =================================================================== RCS file: /home/cvs/xml-xerces/c/src/xercesc/framework/XMLBuffer.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- XMLBuffer.cpp 29 Sep 2004 20:33:51 -0000 1.7 +++ XMLBuffer.cpp 13 Dec 2004 16:36:43 -0000 1.8 @@ -16,6 +16,9 @@ /** * $Log$ + * Revision 1.8 2004/12/13 16:36:43 cargilld + * Performance improvement from Christian Will and bug fix from David Bertoni. + * * Revision 1.7 2004/09/29 20:33:51 peiyongz * resize the internal buffer even the handler can successfully flush the buffer * @@ -69,22 +72,9 @@ // --------------------------------------------------------------------------- // XMLBuffer: Buffer management // --------------------------------------------------------------------------- -void XMLBuffer::append(const XMLCh* const chars, const unsigned int count) -{ - unsigned int actualCount = count; - if (!count) - actualCount = XMLString::stringLen(chars); - insureCapacity(actualCount); - memcpy(&fBuffer[fIndex], chars, actualCount * sizeof(XMLCh)); - fIndex += actualCount; -} void XMLBuffer::insureCapacity(const unsigned int extraNeeded) -{ - // If we can handle it, do nothing yet - if (fIndex + extraNeeded < fCapacity) - return; - +{ // If we can't handle it, try doubling the buffer size. unsigned int newCap = (fIndex + extraNeeded) * 2; @@ -115,16 +105,21 @@ ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Array_BadNewSize, fMemoryManager); } - // Allocate new buffer - XMLCh* newBuf = (XMLCh*) fMemoryManager->allocate((newCap+1) * sizeof(XMLCh)); //new XMLCh[newCap+1]; - - // Copy over the old stuff - memcpy(newBuf, fBuffer, fCapacity * sizeof(XMLCh)); - - // Clean up old buffer and store new stuff - fMemoryManager->deallocate(fBuffer); //delete [] fBuffer; - fBuffer = newBuf; - fCapacity = newCap; + // Note the previous if block can modify newCap, so we may not need to allocate + // at all. + if (newCap > fCapacity) + { + // Allocate new buffer + XMLCh* newBuf = (XMLCh*) fMemoryManager->allocate((newCap+1) * sizeof(XMLCh)); //new XMLCh[newCap+1]; + + // Copy over the old stuff + memcpy(newBuf, fBuffer, fIndex * sizeof(XMLCh)); + + // Clean up old buffer and store new stuff + fMemoryManager->deallocate(fBuffer); //delete [] fBuffer; + fBuffer = newBuf; + fCapacity = newCap; + } } XERCES_CPP_NAMESPACE_END 1.9 +75 -18 xml-xerces/c/src/xercesc/framework/XMLBuffer.hpp Index: XMLBuffer.hpp =================================================================== RCS file: /home/cvs/xml-xerces/c/src/xercesc/framework/XMLBuffer.hpp,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- XMLBuffer.hpp 29 Sep 2004 18:59:18 -0000 1.8 +++ XMLBuffer.hpp 13 Dec 2004 16:36:43 -0000 1.9 @@ -16,6 +16,9 @@ /* * $Log$ + * Revision 1.9 2004/12/13 16:36:43 cargilld + * Performance improvement from Christian Will and bug fix from David Bertoni. + * * Revision 1.8 2004/09/29 18:59:18 peiyongz * [jira1207] --patch from Dan Rosen * @@ -72,6 +75,7 @@ #include <xercesc/util/XMemory.hpp> #include <xercesc/util/PlatformUtils.hpp> #include <xercesc/framework/MemoryManager.hpp> +#include <string.h> XERCES_CPP_NAMESPACE_BEGIN @@ -84,10 +88,10 @@ * that appends of characters and other buffers or strings be very fast, so * it always maintains the current buffer size. * - * The buffer is not nul terminated until some asks to see the raw buffer + * The buffer is not null terminated until some asks to see the raw buffer * contents. This also avoids overhead during append operations. */ - class XMLPARSER_EXPORT XMLBuffer : public XMemory +class XMLPARSER_EXPORT XMLBuffer : public XMemory { public : // ----------------------------------------------------------------------- @@ -98,13 +102,13 @@ //@{ XMLBuffer(const unsigned int capacity = 1023 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager) : - - fUsed(false) - , fIndex(0) + + fIndex(0) , fCapacity(capacity) - , fMemoryManager(manager) - , fFullHandler(0) , fFullSize(0) + , fUsed(false) + , fMemoryManager(manager) + , fFullHandler(0) , fBuffer(0) { // Buffer is one larger than capacity, to allow for zero term @@ -128,8 +132,28 @@ // ----------------------------------------------------------------------- void setFullHandler(XMLBufferFullHandler* handler, const unsigned int fullSize) { - fFullHandler = handler; - fFullSize = fullSize; + if (handler && fullSize) { + fFullHandler = handler; + fFullSize = fullSize; + + // Need to consider the case that the fullsize is less than the current capacity. + // For example, say fullSize = 100 and fCapacity is 1023 (the default). + // If the fIndex is less than the fullSize, then no problem. We can just carry + // on by resetting fCapacity to fullsize and proceed business as usual. + // If the fIndex is already bigger than the fullSize then we call insureCapacity + // to see if it can handle emptying the current buffer (it will throw an + // exception if it can't). + if (fullSize < fCapacity) { + fCapacity = fullSize; + if (fIndex >= fullSize) { + insureCapacity(0); + } + } + } + else { + // reset fFullHandler to zero because setFullHandler had bad input + fFullHandler = 0; + } } // ----------------------------------------------------------------------- @@ -143,14 +167,47 @@ fBuffer[fIndex++] = toAppend; } - void append (const XMLCh* const chars, const unsigned int count = 0); + void append (const XMLCh* const chars, const unsigned int count) + { + if (count) { + if (fIndex + count >= fCapacity) { + insureCapacity(count); + } + memcpy(&fBuffer[fIndex], chars, count * sizeof(XMLCh)); + fIndex += count; + } + else { + append(chars); + } + } + + void append (const XMLCh* const chars) + { + if (chars != 0 && *chars != 0) { + // get length of chars + unsigned int count = 0; + for (; *(chars+count); count++ ); + + if (fIndex + count >= fCapacity) { + insureCapacity(count); + } + memcpy(&fBuffer[fIndex], chars, count * sizeof(XMLCh)); + fIndex += count; + } + } - void set (const XMLCh* const chars, const unsigned int count = 0) + void set (const XMLCh* const chars, const unsigned int count) { fIndex = 0; append(chars, count); } + void set (const XMLCh* const chars) + { + fIndex = 0; + append(chars); + } + const XMLCh* getRawBuffer() const { fBuffer[fIndex] = 0; @@ -236,14 +293,14 @@ // indicated by fFullSize. If writing to the buffer would exceed the // buffer's maximum size, fFullHandler's bufferFull callback is // invoked, to empty the buffer. - // ----------------------------------------------------------------------- - bool fUsed; - unsigned int fIndex; - unsigned int fCapacity; - MemoryManager* const fMemoryManager; - XMLBufferFullHandler* fFullHandler; + // ----------------------------------------------------------------------- + unsigned int fIndex; + unsigned int fCapacity; unsigned int fFullSize; - XMLCh* fBuffer; + bool fUsed; + MemoryManager* const fMemoryManager; + XMLBufferFullHandler* fFullHandler; + XMLCh* fBuffer; }; /**
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]