Kaydet (Commit) 9ff65cb5 authored tarafından Jan Holesovsky's avatar Jan Holesovsky

writerfilter: Kill TokenToIdsMap's, and use switches instead.

This seems to save another nearly 300k; and I guess the compiler will optimize
it better than using the unordered_map.

Change-Id: I5df5cd2afe3f4bedad69e1e3477211842c40a90c
üst b7325b90
...@@ -72,14 +72,6 @@ CreateElementMapPointer OOXMLFactory_ns::getCreateElementMap(Id nId) ...@@ -72,14 +72,6 @@ CreateElementMapPointer OOXMLFactory_ns::getCreateElementMap(Id nId)
return m_CreateElementsMap[nId]; return m_CreateElementsMap[nId];
} }
TokenToIdMapPointer OOXMLFactory_ns::getTokenToIdMap(Id nId)
{
if (m_TokenToIdsMap.find(nId) == m_TokenToIdsMap.end())
m_TokenToIdsMap[nId] = createTokenToIdMap(nId);
return m_TokenToIdsMap[nId];
}
#ifdef DEBUG_DOMAINMAPPER #ifdef DEBUG_DOMAINMAPPER
std::string OOXMLFactory_ns::getDefineName(Id /*nId*/) const std::string OOXMLFactory_ns::getDefineName(Id /*nId*/) const
{ {
...@@ -120,7 +112,6 @@ void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler, ...@@ -120,7 +112,6 @@ void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler,
if (pFactory.get() != NULL) if (pFactory.get() != NULL)
{ {
TokenToIdMapPointer pTokenToIdMap = pFactory->getTokenToIdMap(nDefine);
AttributeToResourceMapPointer pMap = pFactory->getAttributeToResourceMap(nDefine); AttributeToResourceMapPointer pMap = pFactory->getAttributeToResourceMap(nDefine);
AttributeToResourceMap::const_iterator aIt; AttributeToResourceMap::const_iterator aIt;
...@@ -135,7 +126,7 @@ void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler, ...@@ -135,7 +126,7 @@ void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler,
sal_Int32 nToken = aIt->first; sal_Int32 nToken = aIt->first;
if (pAttribs->hasAttribute(nToken)) if (pAttribs->hasAttribute(nToken))
{ {
Id nId = (*pTokenToIdMap)[nToken]; Id nId = pFactory->getResourceId(nDefine, nToken);
switch (aIt->second.m_nResource) switch (aIt->second.m_nResource)
{ {
......
...@@ -85,10 +85,6 @@ typedef boost::unordered_map<Id, CreateElementMapPointer> CreateElementsMap; ...@@ -85,10 +85,6 @@ typedef boost::unordered_map<Id, CreateElementMapPointer> CreateElementsMap;
typedef boost::unordered_map<Id, std::string> IdToStringMap; typedef boost::unordered_map<Id, std::string> IdToStringMap;
typedef boost::shared_ptr<IdToStringMap> IdToStringMapPointer; typedef boost::shared_ptr<IdToStringMap> IdToStringMapPointer;
typedef boost::unordered_map<Id, Token_t> TokenToIdMap;
typedef boost::shared_ptr<TokenToIdMap> TokenToIdMapPointer;
typedef boost::unordered_map<Id, TokenToIdMapPointer> TokenToIdsMap;
class OOXMLFactory_ns { class OOXMLFactory_ns {
public: public:
typedef boost::shared_ptr<OOXMLFactory_ns> Pointer_t; typedef boost::shared_ptr<OOXMLFactory_ns> Pointer_t;
...@@ -103,21 +99,19 @@ public: ...@@ -103,21 +99,19 @@ public:
AttributeToResourceMapPointer getAttributeToResourceMap(Id nId); AttributeToResourceMapPointer getAttributeToResourceMap(Id nId);
CreateElementMapPointer getCreateElementMap(Id nId); CreateElementMapPointer getCreateElementMap(Id nId);
TokenToIdMapPointer getTokenToIdMap(Id nId);
protected: protected:
virtual ~OOXMLFactory_ns(); virtual ~OOXMLFactory_ns();
AttributesMap m_AttributesMap; AttributesMap m_AttributesMap;
CreateElementsMap m_CreateElementsMap; CreateElementsMap m_CreateElementsMap;
TokenToIdsMap m_TokenToIdsMap;
virtual AttributeToResourceMapPointer createAttributeToResourceMap(Id nId) = 0; virtual AttributeToResourceMapPointer createAttributeToResourceMap(Id nId) = 0;
virtual CreateElementMapPointer createCreateElementMap(Id nId) = 0; virtual CreateElementMapPointer createCreateElementMap(Id nId) = 0;
virtual TokenToIdMapPointer createTokenToIdMap(Id nId) = 0;
public: public:
virtual bool getListValue(Id nId, const OUString& rValue, sal_uInt32& rOutValue) = 0; virtual bool getListValue(Id nId, const OUString& rValue, sal_uInt32& rOutValue) = 0;
virtual Id getResourceId(Id nDefine, sal_Int32 nToken) = 0;
}; };
class OOXMLFactory class OOXMLFactory
......
...@@ -36,9 +36,9 @@ public: ...@@ -36,9 +36,9 @@ public:
static Pointer_t getInstance(); static Pointer_t getInstance();
virtual AttributeToResourceMapPointer createAttributeToResourceMap(Id nId); virtual AttributeToResourceMapPointer createAttributeToResourceMap(Id nId);
virtual bool getListValue(Id nId, const OUString& rValue, sal_uInt32& rOutValue);
virtual CreateElementMapPointer createCreateElementMap(Id nId); virtual CreateElementMapPointer createCreateElementMap(Id nId);
virtual TokenToIdMapPointer createTokenToIdMap(Id nId); virtual bool getListValue(Id nId, const OUString& rValue, sal_uInt32& rOutValue);
virtual Id getResourceId(Id nDefine, sal_Int32 nToken);
#ifdef DEBUG_DOMAINMAPPER #ifdef DEBUG_DOMAINMAPPER
virtual string getDefineName(Id nId) const; virtual string getDefineName(Id nId) const;
#endif #endif
......
...@@ -30,11 +30,10 @@ def createFastChildContextFromFactory(model): ...@@ -30,11 +30,10 @@ def createFastChildContextFromFactory(model):
if (pFactory.get() != NULL) if (pFactory.get() != NULL)
{ {
CreateElementMapPointer pMap = pFactory->getCreateElementMap(nDefine); CreateElementMapPointer pMap = pFactory->getCreateElementMap(nDefine);
TokenToIdMapPointer pTokenMap = pFactory->getTokenToIdMap(nDefine);
if (pMap.get() != NULL) if (pMap.get() != NULL)
{ {
Id nId = (*pTokenMap)[Element]; Id nId = pFactory->getResourceId(nDefine, Element);
CreateElement aCreateElement = (*pMap)[Element]; CreateElement aCreateElement = (*pMap)[Element];
switch (aCreateElement.m_nResource) switch (aCreateElement.m_nResource)
......
...@@ -532,64 +532,72 @@ string OOXMLFactory_%s::getDefineName(Id nId) const ...@@ -532,64 +532,72 @@ string OOXMLFactory_%s::getDefineName(Id nId) const
""") """)
# factoryTokenToIdMap # factoryGetResourceId
def factoryTokenToIdMapInner(nsNode, defineNode): def collectTokenToId(nsNode, defineNode):
ret = [] ret = {}
for refNode in defineNode.getElementsByTagName("ref"): for refNode in defineNode.getElementsByTagName("ref"):
refName = refNode.getAttribute("name") refName = refNode.getAttribute("name")
parent = refNode.parentNode parent = refNode.parentNode
if parent.localName in ("element", "attribute"): if parent.localName in ("element", "attribute"):
continue continue
refblock1 = [] refblock1 = {}
for define in [i for i in getChildrenByName(getChildByName(nsNode, "grammar"), "define") if i.getAttribute("name") == refName]: for define in [i for i in getChildrenByName(getChildByName(nsNode, "grammar"), "define") if i.getAttribute("name") == refName]:
refblock1.extend(factoryTokenToIdMapInner(nsNode, define)) refblock1.update(collectTokenToId(nsNode, define))
if not len(refblock1): if not len(refblock1):
for namespaceNode in getChildrenByName(nsNode.parentNode, "namespace"): for namespaceNode in getChildrenByName(nsNode.parentNode, "namespace"):
for define in [i for i in getChildrenByName(getChildByName(namespaceNode, "grammar"), "define") if i.getAttribute("name") == refName]: for define in [i for i in getChildrenByName(getChildByName(namespaceNode, "grammar"), "define") if i.getAttribute("name") == refName]:
ret.extend(factoryTokenToIdMapInner(namespaceNode, define)) ret.update(collectTokenToId(namespaceNode, define))
else: else:
ret.extend(refblock1) ret.update(refblock1)
body = []
defineName = defineNode.getAttribute("name") defineName = defineNode.getAttribute("name")
for resourceNode in [i for i in getChildrenByName(nsNode, "resource") if i.getAttribute("name") == defineName]: for resourceNode in [i for i in getChildrenByName(nsNode, "resource") if i.getAttribute("name") == defineName]:
for node in [i for i in resourceNode.childNodes if i.localName in ("element", "attribute")]: for node in [i for i in resourceNode.childNodes if i.localName in ("element", "attribute")]:
if node.hasAttribute("tokenid"): if node.hasAttribute("tokenid"):
body.append("(*pMap)[%s] = %s;" % (fastToken(node), idToLabel(node.getAttribute("tokenid")))) ret[fastToken(node)] = idToLabel(node.getAttribute("tokenid"))
if len(body):
ret.append("// %s" % defineName)
ret.extend(body)
return ret return ret
def factoryTokenToIdMapInner(nsNode, defineNode):
ids = collectTokenToId(nsNode, defineNode)
ret = []
for i in ids.keys():
ret.append(" case %s: return %s;" % (i, ids[i]))
return ret
def factoryTokenToIdMap(nsNode): def factoryGetResourceId(nsNode):
print("""TokenToIdMapPointer OOXMLFactory_%s::createTokenToIdMap(Id nId) print("""Id OOXMLFactory_%s::getResourceId(Id nDefine, sal_Int32 nToken)
{ {
TokenToIdMapPointer pMap(new TokenToIdMap()); (void) nDefine;
(void) nToken;
switch (nId) switch (nDefine)
{""" % nsToLabel(nsNode)) {""" % nsToLabel(nsNode))
for defineNode in getChildrenByName(getChildByName(nsNode, "grammar"), "define"): for defineNode in getChildrenByName(getChildByName(nsNode, "grammar"), "define"):
inner = "\n".join(factoryTokenToIdMapInner(nsNode, defineNode)) inner = "\n".join(factoryTokenToIdMapInner(nsNode, defineNode))
if len(inner): if len(inner):
print(" case %s:" % idForDefine(nsNode, defineNode)) print(" case %s:" % idForDefine(nsNode, defineNode))
print(" switch (nToken)")
print(" {")
print(inner) print(inner)
print(" }")
print(" break;") print(" break;")
print(" default:") print(" default:")
print(" switch (nToken)")
print(" {")
for startNode in getChildrenByName(nsNode, "start"): for startNode in getChildrenByName(nsNode, "start"):
startName = startNode.getAttribute("name") startName = startNode.getAttribute("name")
print(" // %s" % startName)
for defineNode in [i for i in getChildrenByName(getChildByName(nsNode, "grammar"), "define") if i.getAttribute("name") == startName]: for defineNode in [i for i in getChildrenByName(getChildByName(nsNode, "grammar"), "define") if i.getAttribute("name") == startName]:
inner = factoryTokenToIdMapInner(nsNode, defineNode) inner = factoryTokenToIdMapInner(nsNode, defineNode)
if len(inner): if len(inner):
print("\n".join(inner)) print("\n".join(inner))
print(""" break; print(""" }
break;
} }
return 0;
return pMap;
} }
""") """)
...@@ -697,7 +705,7 @@ namespace ooxml { ...@@ -697,7 +705,7 @@ namespace ooxml {
factoryCreateElementMap(files, nsNode) factoryCreateElementMap(files, nsNode)
factoryActions(nsNode) factoryActions(nsNode)
factoryGetDefineName(nsNode) factoryGetDefineName(nsNode)
factoryTokenToIdMap(nsNode) factoryGetResourceId(nsNode)
factoryAttributeAction(nsNode) factoryAttributeAction(nsNode)
print("""/// @endcond print("""/// @endcond
......
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