Avoid duplicate set look-up and ordering overhead when tracking visited fonts and objects in FontScanner.
diff --git a/poppler/FontInfo.cc b/poppler/FontInfo.cc
index b7e970d..e6ccfb2 100644
--- a/poppler/FontInfo.cc
+++ b/poppler/FontInfo.cc
@@ -115,9 +115,8 @@
Ref fontRef = *font->getID();
// add this font to the list if not already found
- if (fonts.find(fontRef.num) == fonts.end()) {
+ if (fonts.insert(fontRef.num).second) {
fontsList->push_back(new FontInfo(font, xrefA));
- fonts.insert(fontRef.num);
}
}
}
@@ -135,11 +134,9 @@
const Object obj2 = objDict.getDict()->getVal(i, &obj2Ref);
if (obj2Ref != Ref::INVALID()) {
// check for an already-seen object
- if (visitedObjects.find(obj2Ref.num) != visitedObjects.end()) {
+ if (!visitedObjects.insert(obj2Ref.num).second) {
continue;
- }
-
- visitedObjects.insert(obj2Ref.num);
+ }
}
if (obj2.isStream()) {
@@ -147,11 +144,9 @@
const Object resObj = obj2.streamGetDict()->lookup("Resources", &resourcesRef);
if (resourcesRef != Ref::INVALID()) {
- if (visitedObjects.find(resourcesRef.num) != visitedObjects.end()) {
+ if (!visitedObjects.insert(resourcesRef.num).second) {
continue;
- }
-
- visitedObjects.insert(resourcesRef.num);
+ }
}
if (resObj.isDict() && resObj.getDict() != resDict) {
diff --git a/poppler/FontInfo.h b/poppler/FontInfo.h
index 6394c4a..2a40bcd 100644
--- a/poppler/FontInfo.h
+++ b/poppler/FontInfo.h
@@ -28,6 +28,8 @@
#include "Object.h"
+#include <unordered_set>
+
class GfxFont;
class PDFDoc;
@@ -95,8 +97,8 @@
PDFDoc *doc;
int currentPage;
- std::set<int> fonts;
- std::set<int> visitedObjects;
+ std::unordered_set<int> fonts;
+ std::unordered_set<int> visitedObjects;
void scanFonts(XRef *xrefA, Dict *resDict, std::vector<FontInfo*> *fontsList);
};