Use map from key to count instead of multiset (#3885)

* Test that enumerates lots of elements with the same name

* Use map from key to count instead of multiset

Co-authored-by: Alexander Gololobov <{ID}+{username}@users.noreply.github.com>
This commit is contained in:
Alexander Gololobov 2023-01-24 07:52:23 +01:00 committed by GitHub
parent 079b50e0c1
commit 0fd1749b81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 5 deletions

View File

@ -266,7 +266,7 @@ void XMLConfiguration::enumerate(const std::string& key, Keys& range) const
{
using Poco::NumberFormatter;
std::multiset<std::string> keys;
std::map<std::string, size_t> keys;
const Poco::XML::Node* pNode = findNode(key);
if (pNode)
{
@ -276,12 +276,12 @@ void XMLConfiguration::enumerate(const std::string& key, Keys& range) const
if (pChild->nodeType() == Poco::XML::Node::ELEMENT_NODE)
{
const std::string& nodeName = pChild->nodeName();
int n = (int) keys.count(nodeName);
if (n)
range.push_back(nodeName + "[" + NumberFormatter::format(n) + "]");
size_t& count = keys[nodeName];
if (count)
range.push_back(nodeName + "[" + NumberFormatter::format(count) + "]");
else
range.push_back(nodeName);
keys.insert(nodeName);
++count;
}
pChild = pChild->nextSibling();
}

View File

@ -300,6 +300,27 @@ void XMLConfigurationTest::testLoadEmpty()
}
void XMLConfigurationTest::testManyKeys()
{
std::ostringstream ostr;
ostr << "<config>\n";
const size_t count = 200000;
for (size_t i = 0; i < count; ++i)
{
ostr << "<element>" << i << "</element>\n";
}
ostr << "</config>\n";
std::istringstream istr(ostr.str());
AutoPtr<XMLConfiguration> pConf = new XMLConfiguration(istr);
AbstractConfiguration::Keys all_elements;
pConf->keys("", all_elements);
assertTrue(all_elements.size() == count);
}
void XMLConfigurationTest::setUp()
{
}
@ -322,6 +343,7 @@ CppUnit::Test* XMLConfigurationTest::suite()
CppUnit_addTest(pSuite, XMLConfigurationTest, testSaveEmpty);
CppUnit_addTest(pSuite, XMLConfigurationTest, testFromScratch);
CppUnit_addTest(pSuite, XMLConfigurationTest, testLoadEmpty);
CppUnit_addTest(pSuite, XMLConfigurationTest, testManyKeys);
return pSuite;
}

View File

@ -31,6 +31,7 @@ public:
void testSaveEmpty();
void testFromScratch();
void testLoadEmpty();
void testManyKeys();
void setUp();
void tearDown();