FastAttributeList: avoid OStrings in attribute list; just use char buffer

Change-Id: I4879563fae3b85c68bbd1c4b260f9833848f4bda
This commit is contained in:
Matúš Kukan
2013-10-16 11:15:31 +02:00
parent 9ae701509a
commit c293e73e8f
3 changed files with 60 additions and 25 deletions

View File

@@ -40,9 +40,9 @@ struct UnknownAttribute
OString maName; OString maName;
OString maValue; OString maValue;
UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& rValue ); UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue );
UnknownAttribute( const OString& rName, const OString& rValue ); UnknownAttribute( const OString& rName, const sal_Char* pValue );
void FillAttribute( ::com::sun::star::xml::Attribute* pAttrib ) const; void FillAttribute( ::com::sun::star::xml::Attribute* pAttrib ) const;
}; };
@@ -56,9 +56,10 @@ public:
virtual ~FastAttributeList(); virtual ~FastAttributeList();
void clear(); void clear();
void add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength = 0 );
void add( sal_Int32 nToken, const OString& rValue ); void add( sal_Int32 nToken, const OString& rValue );
void addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& rValue ); void addUnknown( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue );
void addUnknown( const OString& rName, const OString& rValue ); void addUnknown( const OString& rName, const sal_Char* pValue );
// XFastAttributeList // XFastAttributeList
virtual ::sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) throw (::com::sun::star::uno::RuntimeException); virtual ::sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) throw (::com::sun::star::uno::RuntimeException);
@@ -70,11 +71,18 @@ public:
virtual ::com::sun::star::uno::Sequence< ::com::sun::star::xml::FastAttribute > SAL_CALL getFastAttributes() throw (::com::sun::star::uno::RuntimeException); virtual ::com::sun::star::uno::Sequence< ::com::sun::star::xml::FastAttribute > SAL_CALL getFastAttributes() throw (::com::sun::star::uno::RuntimeException);
private: private:
inline sal_Int32 AttributeValueLength(sal_Int32 i);
private:
sal_Char *mpChunk; ///< buffer to store all attribute values - null terminated strings
sal_Int32 mnChunkLength; ///< size of allocated memory for mpChunk
// maAttributeValues stores pointers, relative to mpChunk, for each attribute value string
// length of the string is maAttributeValues[n+1] - maAttributeValues[n] - 1
// maAttributeValues[0] == 0
std::vector< sal_Int32 > maAttributeValues;
std::vector< sal_Int32 > maAttributeTokens; std::vector< sal_Int32 > maAttributeTokens;
std::vector< OString > maAttributeValues;
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;
}; };
} }

View File

@@ -793,10 +793,10 @@ void FastSaxParser::callbackStartElement( const XML_Char* pwName, const XML_Char
{ {
sal_Int32 nAttributeToken = GetTokenWithPrefix( pPrefix, nPrefixLen, pName, nNameLen ); sal_Int32 nAttributeToken = GetTokenWithPrefix( pPrefix, nPrefixLen, pName, nNameLen );
if( nAttributeToken != FastToken::DONTKNOW ) if( nAttributeToken != FastToken::DONTKNOW )
rEntity.mxAttributes->add( nAttributeToken, OString(awAttributes[i+1]) ); rEntity.mxAttributes->add( nAttributeToken, awAttributes[i+1] );
else else
rEntity.mxAttributes->addUnknown( GetNamespaceURL( pPrefix, nPrefixLen ), rEntity.mxAttributes->addUnknown( GetNamespaceURL( pPrefix, nPrefixLen ),
OString(pName, nNameLen), OString(awAttributes[i+1]) ); OString(pName, nNameLen), awAttributes[i+1] );
} }
} }
else else
@@ -805,9 +805,9 @@ void FastSaxParser::callbackStartElement( const XML_Char* pwName, const XML_Char
{ {
sal_Int32 nAttributeToken = GetToken( pName, nNameLen ); sal_Int32 nAttributeToken = GetToken( pName, nNameLen );
if( nAttributeToken != FastToken::DONTKNOW ) if( nAttributeToken != FastToken::DONTKNOW )
rEntity.mxAttributes->add( nAttributeToken, OString(awAttributes[i+1]) ); rEntity.mxAttributes->add( nAttributeToken, awAttributes[i+1] );
else else
rEntity.mxAttributes->addUnknown( OString(pName, nNameLen), OString(awAttributes[i+1]) ); rEntity.mxAttributes->addUnknown( OString(pName, nNameLen), awAttributes[i+1] );
} }
} }
} }

View File

@@ -27,13 +27,13 @@ using namespace ::com::sun::star::xml::sax;
namespace sax_fastparser namespace sax_fastparser
{ {
UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& rValue ) UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue )
: maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( rValue ) : maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( pValue )
{ {
} }
UnknownAttribute::UnknownAttribute( const OString& rName, const OString& rValue ) UnknownAttribute::UnknownAttribute( const OString& rName, const sal_Char* pValue )
: maName( rName ), maValue( rValue ) : maName( rName ), maValue( pValue )
{ {
} }
@@ -50,33 +50,54 @@ void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler ) FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler )
: mxTokenHandler( xTokenHandler ) : mxTokenHandler( xTokenHandler )
{ {
// random initial size of buffer to store attribute values
mnChunkLength = 58;
mpChunk = (sal_Char *) malloc( mnChunkLength );
maAttributeValues.push_back( 0 );
} }
FastAttributeList::~FastAttributeList() FastAttributeList::~FastAttributeList()
{ {
free( mpChunk );
} }
void FastAttributeList::clear() void FastAttributeList::clear()
{ {
maAttributeTokens.clear(); maAttributeTokens.clear();
maAttributeValues.clear(); maAttributeValues.clear();
maAttributeValues.push_back( 0 );
maUnknownAttributes.clear(); maUnknownAttributes.clear();
} }
void FastAttributeList::add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength )
{
maAttributeTokens.push_back( nToken );
if (nValueLength == 0)
nValueLength = strlen(pValue);
sal_Int32 nWritePosition = maAttributeValues.back();
maAttributeValues.push_back( maAttributeValues.back() + nValueLength + 1 );
if (maAttributeValues.back() > mnChunkLength)
{
mnChunkLength = maAttributeValues.back();
mpChunk = (sal_Char *) realloc( mpChunk, mnChunkLength );
}
strncpy(mpChunk + nWritePosition, pValue, nValueLength);
mpChunk[nWritePosition + nValueLength] = '\0';
}
void FastAttributeList::add( sal_Int32 nToken, const OString& rValue ) void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
{ {
maAttributeTokens.push_back( nToken ); add( nToken, rValue.getStr(), rValue.getLength() );
maAttributeValues.push_back( rValue );
} }
void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& rValue ) void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue )
{ {
maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, rValue ) ); maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, pValue ) );
} }
void FastAttributeList::addUnknown( const OString& rName, const OString& rValue ) void FastAttributeList::addUnknown( const OString& rName, const sal_Char* pValue )
{ {
maUnknownAttributes.push_back( UnknownAttribute( rName, rValue ) ); maUnknownAttributes.push_back( UnknownAttribute( rName, pValue ) );
} }
// XFastAttributeList // XFastAttributeList
@@ -94,7 +115,7 @@ 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)
{ {
Sequence< sal_Int8 > aSeq( (sal_Int8*) maAttributeValues[i].getStr(), maAttributeValues[i].getLength() ); Sequence< sal_Int8 > aSeq( (sal_Int8*) mpChunk + maAttributeValues[i], AttributeValueLength(i) );
return mxTokenHandler->getTokenFromUTF8( aSeq ); return mxTokenHandler->getTokenFromUTF8( aSeq );
} }
@@ -106,7 +127,7 @@ 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)
{ {
Sequence< sal_Int8 > aSeq( (sal_Int8*) maAttributeValues[i].getStr(), maAttributeValues[i].getLength() ); Sequence< sal_Int8 > aSeq( (sal_Int8*) mpChunk + maAttributeValues[i], AttributeValueLength(i) );
return mxTokenHandler->getTokenFromUTF8( aSeq ); return mxTokenHandler->getTokenFromUTF8( aSeq );
} }
@@ -117,7 +138,7 @@ OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, R
{ {
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 OStringToOUString( maAttributeValues[i], RTL_TEXTENCODING_UTF8 ); return OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
throw SAXException(); throw SAXException();
} }
@@ -126,7 +147,7 @@ OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (Runtime
{ {
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 OStringToOUString( maAttributeValues[i], RTL_TEXTENCODING_UTF8 ); return OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
return OUString(); return OUString();
} }
@@ -145,12 +166,18 @@ Sequence< FastAttribute > FastAttributeList::getFastAttributes( ) throw (Runtim
for (size_t i = 0; i < maAttributeTokens.size(); ++i) for (size_t i = 0; i < maAttributeTokens.size(); ++i)
{ {
pAttr->Token = maAttributeTokens[i]; pAttr->Token = maAttributeTokens[i];
pAttr->Value = OStringToOUString( maAttributeValues[i], RTL_TEXTENCODING_UTF8 ); pAttr->Value = OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
pAttr++; pAttr++;
} }
return aSeq; return aSeq;
} }
sal_Int32 FastAttributeList::AttributeValueLength(sal_Int32 i)
{
// Pointers to null terminated strings
return maAttributeValues[i + 1] - maAttributeValues[i] - 1;
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */