Skip to content

Commit c8d8f9f

Browse files
authored
Merge pull request #20352 from calixteman/improve_struct_tree_parser
Improve performance of the struct tree build (bug 1987914)
2 parents 0fedfc9 + 9797dc0 commit c8d8f9f

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

src/core/struct_tree.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@ class StructTreeRoot {
3535
this.ref = rootRef instanceof Ref ? rootRef : null;
3636
this.roleMap = new Map();
3737
this.structParentIds = null;
38+
this.kidRefToPosition = undefined;
39+
}
40+
41+
getKidPosition(kidRef) {
42+
if (this.kidRefToPosition === undefined) {
43+
const obj = this.dict.get("K");
44+
if (Array.isArray(obj)) {
45+
const map = (this.kidRefToPosition = new Map());
46+
for (let i = 0, ii = obj.length; i < ii; i++) {
47+
const ref = obj[i];
48+
if (ref) {
49+
map.set(ref.toString(), i);
50+
}
51+
}
52+
} else if (obj instanceof Dict) {
53+
this.kidRefToPosition = new Map([[obj.objId, 0]]);
54+
} else if (!obj) {
55+
this.kidRefToPosition = new Map();
56+
} else {
57+
this.kidRefToPosition = null;
58+
}
59+
}
60+
return this.kidRefToPosition
61+
? (this.kidRefToPosition.get(kidRef) ?? NaN)
62+
: -1;
3863
}
3964

4065
init() {
@@ -785,31 +810,14 @@ class StructTreePage {
785810
}
786811

787812
addTopLevelNode(dict, element) {
788-
const obj = this.rootDict.get("K");
789-
if (!obj) {
813+
const index = this.root.getKidPosition(dict.objId);
814+
if (isNaN(index)) {
790815
return false;
791816
}
792-
793-
if (obj instanceof Dict) {
794-
if (obj.objId !== dict.objId) {
795-
return false;
796-
}
797-
this.nodes[0] = element;
798-
return true;
799-
}
800-
801-
if (!Array.isArray(obj)) {
802-
return true;
803-
}
804-
let save = false;
805-
for (let i = 0; i < obj.length; i++) {
806-
const kidRef = obj[i];
807-
if (kidRef?.toString() === dict.objId) {
808-
this.nodes[i] = element;
809-
save = true;
810-
}
817+
if (index !== -1) {
818+
this.nodes[index] = element;
811819
}
812-
return save;
820+
return true;
813821
}
814822

815823
/**

0 commit comments

Comments
 (0)