diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2023-04-09 20:43:29 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-04-10 21:51:49 +0200 |
commit | 9ff063eeabc38d9c1fd1a4beb0d0e4112559b0c1 (patch) | |
tree | 0af5e56b4f35fa564f0bb51453278de4337dd8b5 /toolkit | |
parent | f6fa9c455502e9a00e4a4cff6e19f3e17c5479c3 (diff) |
use std::unordered_map for static properties in toolkit
which is faster than binary lookup
Change-Id: Ia810313af36c75fd9b5b241704f0cbd66afcb6b0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150189
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'toolkit')
-rw-r--r-- | toolkit/source/helper/property.cxx | 102 | ||||
-rw-r--r-- | toolkit/source/helper/unopropertyarrayhelper.cxx | 8 |
2 files changed, 31 insertions, 79 deletions
diff --git a/toolkit/source/helper/property.cxx b/toolkit/source/helper/property.cxx index f05fee0aea7e..1775ea9cdfcb 100644 --- a/toolkit/source/helper/property.cxx +++ b/toolkit/source/helper/property.cxx @@ -36,6 +36,7 @@ #include <algorithm> #include <string_view> #include <utility> +#include <unordered_map> using ::com::sun::star::uno::Any; using ::com::sun::star::uno::Sequence; @@ -51,16 +52,14 @@ namespace { struct ImplPropertyInfo { - OUString aName; sal_uInt16 nPropId; css::uno::Type aType; sal_Int16 nAttribs; bool bDependsOnOthers; // eg. VALUE depends on MIN/MAX and must be set after MIN/MAX. - ImplPropertyInfo( OUString theName, sal_uInt16 nId, const css::uno::Type& rType, + ImplPropertyInfo( sal_uInt16 nId, const css::uno::Type& rType, sal_Int16 nAttrs, bool bDepends = false ) - : aName(std::move(theName)) - , nPropId(nId) + : nPropId(nId) , aType(rType) , nAttribs(nAttrs) , bDependsOnOthers(bDepends) @@ -72,20 +71,21 @@ struct ImplPropertyInfo } #define DECL_PROP_1( asciiname, id, type, attrib1 ) \ - ImplPropertyInfo( asciiname, BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 ) + { asciiname, ImplPropertyInfo( BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 ) } #define DECL_PROP_2( asciiname, id, type, attrib1, attrib2 ) \ - ImplPropertyInfo( asciiname, BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 | css::beans::PropertyAttribute::attrib2 ) + { asciiname, ImplPropertyInfo( BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 | css::beans::PropertyAttribute::attrib2 ) } #define DECL_PROP_3( asciiname, id, type, attrib1, attrib2, attrib3 ) \ - ImplPropertyInfo( asciiname, BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 | css::beans::PropertyAttribute::attrib2 | css::beans::PropertyAttribute::attrib3 ) + { asciiname, ImplPropertyInfo( BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 | css::beans::PropertyAttribute::attrib2 | css::beans::PropertyAttribute::attrib3 ) } #define DECL_DEP_PROP_2( asciiname, id, type, attrib1, attrib2 ) \ - ImplPropertyInfo( asciiname, BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 | css::beans::PropertyAttribute::attrib2, true ) + { asciiname, ImplPropertyInfo( BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 | css::beans::PropertyAttribute::attrib2, true ) } #define DECL_DEP_PROP_3( asciiname, id, type, attrib1, attrib2, attrib3 ) \ - ImplPropertyInfo( asciiname, BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 | css::beans::PropertyAttribute::attrib2 | css::beans::PropertyAttribute::attrib3, true ) + { asciiname, ImplPropertyInfo( BASEPROPERTY_##id, cppu::UnoType<type>::get(), css::beans::PropertyAttribute::attrib1 | css::beans::PropertyAttribute::attrib2 | css::beans::PropertyAttribute::attrib3, true ) } -static ImplPropertyInfo* ImplGetPropertyInfos( sal_uInt16& rElementCount ) +typedef std::unordered_map<OUString, ImplPropertyInfo> ImpPropertyInfoMap; +static const ImpPropertyInfoMap & ImplGetPropertyInfos() { - static ImplPropertyInfo aImplPropertyInfos [] = { + static const ImpPropertyInfoMap aImplPropertyInfos { DECL_PROP_2 ( "AccessibleName", ACCESSIBLENAME, OUString, BOUND, MAYBEDEFAULT ), DECL_PROP_3 ( "Align", ALIGN, sal_Int16, BOUND, MAYBEDEFAULT, MAYBEVOID ), DECL_PROP_2 ( "Autocomplete", AUTOCOMPLETE, bool, BOUND, MAYBEDEFAULT ), @@ -272,85 +272,37 @@ static ImplPropertyInfo* ImplGetPropertyInfos( sal_uInt16& rElementCount ) DECL_PROP_3 ( "ActiveSelectionTextColor", ACTIVE_SEL_TEXT_COLOR, sal_Int32, BOUND, MAYBEDEFAULT, MAYBEVOID ), DECL_PROP_3 ( "InactiveSelectionTextColor", INACTIVE_SEL_TEXT_COLOR, sal_Int32, BOUND, MAYBEDEFAULT, MAYBEVOID ), }; - rElementCount = SAL_N_ELEMENTS(aImplPropertyInfos); return aImplPropertyInfos; } -namespace { - -struct ImplPropertyInfoCompareFunctor -{ - bool operator()(const ImplPropertyInfo& lhs,const ImplPropertyInfo& rhs) const - { - return lhs.aName.compareTo(rhs.aName) < 0; - } - bool operator()(const ImplPropertyInfo& lhs,std::u16string_view rhs) const - { - return lhs.aName.compareTo(rhs) < 0; - } -}; - -} - -static void ImplAssertValidPropertyArray() -{ - static bool bSorted = false; - if( !bSorted ) - { - sal_uInt16 nElements; - ImplPropertyInfo* pInfo = ImplGetPropertyInfos( nElements ); - ::std::sort(pInfo, pInfo+nElements,ImplPropertyInfoCompareFunctor()); - bSorted = true; - } -} - sal_uInt16 GetPropertyId( const OUString& rPropertyName ) { - ImplAssertValidPropertyArray(); - - sal_uInt16 nElements; - ImplPropertyInfo* pInfo = ImplGetPropertyInfos( nElements ); - ImplPropertyInfo* pInf = ::std::lower_bound(pInfo,pInfo+nElements,rPropertyName,ImplPropertyInfoCompareFunctor()); -/* - (ImplPropertyInfo*) - bsearch( &aSearch, pInfo, nElements, sizeof( ImplPropertyInfo ), ImplPropertyInfoCompare ); -*/ - - return ( pInf && pInf != (pInfo+nElements) && pInf->aName == rPropertyName) ? pInf->nPropId: 0; + const ImpPropertyInfoMap & rMap = ImplGetPropertyInfos(); + auto it = rMap.find(rPropertyName); + return it != rMap.end() ? it->second.nPropId : 0; } static const ImplPropertyInfo* ImplGetImplPropertyInfo( sal_uInt16 nPropertyId ) { - ImplAssertValidPropertyArray(); + const ImpPropertyInfoMap & rMap = ImplGetPropertyInfos(); - sal_uInt16 nElements; - ImplPropertyInfo* pInfo = ImplGetPropertyInfos( nElements ); - sal_uInt16 n; - for ( n = 0; n < nElements && pInfo[n].nPropId != nPropertyId; ++n) - ; - - return (n < nElements) ? &pInfo[n] : nullptr; + for (auto const & rPair : rMap) + if (rPair.second.nPropId == nPropertyId) + return &rPair.second; + return nullptr; } -sal_uInt16 GetPropertyOrderNr( sal_uInt16 nPropertyId ) +const OUString& GetPropertyName( sal_uInt16 nPropertyId ) { - ImplAssertValidPropertyArray(); + const ImpPropertyInfoMap & rMap = ImplGetPropertyInfos(); - sal_uInt16 nElements; - ImplPropertyInfo* pInfo = ImplGetPropertyInfos( nElements ); - for ( sal_uInt16 n = nElements; n; ) - { - if ( pInfo[--n].nPropId == nPropertyId ) - return n; - } - return 0xFFFF; -} + for (auto const & rPair : rMap) + if (rPair.second.nPropId == nPropertyId) + return rPair.first; -const OUString& GetPropertyName( sal_uInt16 nPropertyId ) -{ - const ImplPropertyInfo* pImplPropertyInfo = ImplGetImplPropertyInfo( nPropertyId ); - assert(pImplPropertyInfo && "Invalid PropertyId!"); - return pImplPropertyInfo->aName; + assert(false && "Invalid PropertyId!"); + static const OUString EMPTY; + return EMPTY; } const css::uno::Type* GetPropertyType( sal_uInt16 nPropertyId ) diff --git a/toolkit/source/helper/unopropertyarrayhelper.cxx b/toolkit/source/helper/unopropertyarrayhelper.cxx index 0a36f4bf31a7..0b4b8055c226 100644 --- a/toolkit/source/helper/unopropertyarrayhelper.cxx +++ b/toolkit/source/helper/unopropertyarrayhelper.cxx @@ -64,17 +64,17 @@ css::uno::Sequence< css::beans::Property > UnoPropertyArrayHelper::getProperties { // Sort by names ... - std::map<sal_Int32, sal_uInt16> aSortedPropsIds; + std::map<OUString, sal_uInt16> aSortedPropsIds; for (const auto& rId : maIDs) { sal_uInt16 nId = sal::static_int_cast< sal_uInt16 >(rId); - aSortedPropsIds[ 1+GetPropertyOrderNr( nId ) ] = nId; + aSortedPropsIds.emplace(GetPropertyName( nId ), nId); if ( nId == BASEPROPERTY_FONTDESCRIPTOR ) { // single properties ... for ( sal_uInt16 i = BASEPROPERTY_FONTDESCRIPTORPART_START; i <= BASEPROPERTY_FONTDESCRIPTORPART_END; i++ ) - aSortedPropsIds[ 1+GetPropertyOrderNr( i ) ] = i; + aSortedPropsIds.emplace(GetPropertyName( i ), i); } } @@ -86,7 +86,7 @@ css::uno::Sequence< css::beans::Property > UnoPropertyArrayHelper::getProperties for ( const auto& rPropIds : aSortedPropsIds ) { sal_uInt16 nId = rPropIds.second; - pProps[n].Name = GetPropertyName( nId ); + pProps[n].Name = rPropIds.first; pProps[n].Handle = nId; pProps[n].Type = *GetPropertyType( nId ); pProps[n].Attributes = GetPropertyAttribs( nId ); |