Kaydet (Commit) fc25afaa authored tarafından Michael Meeks's avatar Michael Meeks

fastparser: accelerate value tokenisation as well.

Change-Id: I99a39e91c684adb1fc92cdb466477cfa90104961
üst 7e77efe8
...@@ -49,6 +49,18 @@ struct UnknownAttribute ...@@ -49,6 +49,18 @@ struct UnknownAttribute
typedef std::vector< UnknownAttribute > UnknownAttributeList; typedef std::vector< UnknownAttribute > UnknownAttributeList;
/// avoid constantly allocating and freeing sequences.
class SAX_DLLPUBLIC FastTokenLookup
{
static const int mnUtf8BufferSize = 128;
::css::uno::Sequence< sal_Int8 > maUtf8Buffer;
public:
FastTokenLookup();
sal_Int32 getTokenFromChars(
const ::css::uno::Reference< ::css::xml::sax::XFastTokenHandler > &mxTokenHandler,
const char *pStr, size_t nLength = 0 );
};
class SAX_DLLPUBLIC FastAttributeList : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XFastAttributeList > class SAX_DLLPUBLIC FastAttributeList : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XFastAttributeList >
{ {
public: public:
...@@ -83,6 +95,7 @@ private: ...@@ -83,6 +95,7 @@ private:
std::vector< sal_Int32 > maAttributeTokens; std::vector< sal_Int32 > maAttributeTokens;
UnknownAttributeList maUnknownAttributes; UnknownAttributeList maUnknownAttributes;
::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxTokenHandler; ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxTokenHandler;
FastTokenLookup maTokenLookup;
}; };
} }
......
...@@ -348,7 +348,6 @@ Event& Entity::getEvent( CallbackType aType ) ...@@ -348,7 +348,6 @@ Event& Entity::getEvent( CallbackType aType )
FastSaxParser::FastSaxParser() FastSaxParser::FastSaxParser()
{ {
mxDocumentLocator.set( new FastLocatorImpl( this ) ); mxDocumentLocator.set( new FastLocatorImpl( this ) );
maUtf8Buffer.realloc( mnUtf8BufferSize );
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
...@@ -381,31 +380,7 @@ void FastSaxParser::DefineNamespace( const OString& rPrefix, const sal_Char* pNa ...@@ -381,31 +380,7 @@ void FastSaxParser::DefineNamespace( const OString& rPrefix, const sal_Char* pNa
sal_Int32 FastSaxParser::GetToken( const sal_Char* pToken, sal_Int32 nLen /* = 0 */ ) sal_Int32 FastSaxParser::GetToken( const sal_Char* pToken, sal_Int32 nLen /* = 0 */ )
{ {
sal_Int32 nRet; return maTokenLookup.getTokenFromChars( getEntity().mxTokenHandler, pToken, nLen );
if( !nLen )
nLen = strlen( pToken );
if ( nLen < mnUtf8BufferSize )
{
// Get intimiate with the underlying sequence cf. sal/types.h
sal_Sequence *pSeq = maUtf8Buffer.get();
sal_Int32 nPreRefCount = pSeq->nRefCount;
pSeq->nElements = nLen;
memcpy( pSeq->elements, pToken, nLen );
nRet = getEntity().mxTokenHandler->getTokenFromUTF8( maUtf8Buffer );
(void)nPreRefCount; // for non-debug mode.
assert( pSeq->nRefCount == nPreRefCount ); // callee must not take ref.
}
else
{
Sequence< sal_Int8 > aSeq( (sal_Int8*)pToken, nLen ); // heap allocate & free
nRet = getEntity().mxTokenHandler->getTokenFromUTF8( aSeq );
}
return nRet;
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
......
...@@ -222,9 +222,7 @@ private: ...@@ -222,9 +222,7 @@ private:
ParserData maData; /// Cached parser configuration for next call of parseStream(). ParserData maData; /// Cached parser configuration for next call of parseStream().
::std::stack< Entity > maEntities; /// Entity stack for each call of parseStream(). ::std::stack< Entity > maEntities; /// Entity stack for each call of parseStream().
FastTokenLookup maTokenLookup;
static const int mnUtf8BufferSize = 128;
::css::uno::Sequence< sal_Int8 > maUtf8Buffer; /// avoid constantly re-allocating this
}; };
} }
......
...@@ -114,10 +114,9 @@ sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXExcept ...@@ -114,10 +114,9 @@ sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXExcept
{ {
for (size_t i = 0; i < maAttributeTokens.size(); ++i) for (size_t i = 0; i < maAttributeTokens.size(); ++i)
if (maAttributeTokens[i] == Token) if (maAttributeTokens[i] == Token)
{ return maTokenLookup.getTokenFromChars( mxTokenHandler,
Sequence< sal_Int8 > aSeq( (sal_Int8*) mpChunk + maAttributeValues[i], AttributeValueLength(i) ); mpChunk + maAttributeValues[ i ],
return mxTokenHandler->getTokenFromUTF8( aSeq ); AttributeValueLength( i ) );
}
throw SAXException(); throw SAXException();
} }
...@@ -126,10 +125,9 @@ sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int ...@@ -126,10 +125,9 @@ sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int
{ {
for (size_t i = 0; i < maAttributeTokens.size(); ++i) for (size_t i = 0; i < maAttributeTokens.size(); ++i)
if (maAttributeTokens[i] == Token) if (maAttributeTokens[i] == Token)
{ return maTokenLookup.getTokenFromChars( mxTokenHandler,
Sequence< sal_Int8 > aSeq( (sal_Int8*) mpChunk + maAttributeValues[i], AttributeValueLength(i) ); mpChunk + maAttributeValues[ i ],
return mxTokenHandler->getTokenFromUTF8( aSeq ); AttributeValueLength( i ) );
}
return Default; return Default;
} }
...@@ -178,6 +176,47 @@ sal_Int32 FastAttributeList::AttributeValueLength(sal_Int32 i) ...@@ -178,6 +176,47 @@ sal_Int32 FastAttributeList::AttributeValueLength(sal_Int32 i)
return maAttributeValues[i + 1] - maAttributeValues[i] - 1; return maAttributeValues[i + 1] - maAttributeValues[i] - 1;
} }
FastTokenLookup::FastTokenLookup()
{
maUtf8Buffer.realloc( mnUtf8BufferSize );
}
/**
* Avoid doing any memory allocation if we can, instead keep a
* pet sequence around and do some heavy petting on it.
*/
sal_Int32 FastTokenLookup::getTokenFromChars(
const ::css::uno::Reference< ::css::xml::sax::XFastTokenHandler > &xTokenHandler,
const char *pToken, size_t nLen /* = 0 */ )
{
sal_Int32 nRet;
if( !nLen )
nLen = strlen( pToken );
if ( nLen < mnUtf8BufferSize )
{
// Get intimiate with the underlying sequence cf. sal/types.h
sal_Sequence *pSeq = maUtf8Buffer.get();
sal_Int32 nPreRefCount = pSeq->nRefCount;
pSeq->nElements = nLen;
memcpy( pSeq->elements, pToken, nLen );
nRet = xTokenHandler->getTokenFromUTF8( maUtf8Buffer );
(void)nPreRefCount; // for non-debug mode.
assert( pSeq->nRefCount == nPreRefCount ); // callee must not take ref
}
else
{
Sequence< sal_Int8 > aSeq( (sal_Int8*)pToken, nLen ); // heap allocate & free
nRet = xTokenHandler->getTokenFromUTF8( aSeq );
}
return nRet;
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment