Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add different code-paths to {CMap, ToUnicodeMap}.charCodeOf depending on length, since Array.prototype.indexOf can be extremely inefficient for very large arrays (issue 8372) #8442

Merged
merged 1 commit into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/core/cmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,24 +283,34 @@ var CMap = (function CMapClosure() {
// indices in the *billions*. For such tables we use for..in, which isn't
// ideal because it stringifies the indices for all present elements, but
// it does avoid iterating over every undefined entry.
var map = this._map;
var length = map.length;
var i;
let map = this._map;
let length = map.length;
if (length <= 0x10000) {
for (i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
if (map[i] !== undefined) {
callback(i, map[i]);
}
}
} else {
for (i in this._map) {
for (let i in map) {
callback(i, map[i]);
}
}
},

charCodeOf(value) {
return this._map.indexOf(value);
// `Array.prototype.indexOf` is *extremely* inefficient for arrays which
// are both very sparse and very large (see issue8372.pdf).
let map = this._map;
if (map.length <= 0x10000) {
return map.indexOf(value);
}
for (let charCode in map) {
if (map[charCode] === value) {
return (charCode | 0);
}
}
return -1;
},

getMap() {
Expand Down
15 changes: 13 additions & 2 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,19 @@ var ToUnicodeMap = (function ToUnicodeMapClosure() {
return this._map[i];
},

charCodeOf(v) {
return this._map.indexOf(v);
charCodeOf(value) {
// `Array.prototype.indexOf` is *extremely* inefficient for arrays which
// are both very sparse and very large (see issue8372.pdf).
let map = this._map;
if (map.length <= 0x10000) {
return map.indexOf(value);
}
for (let charCode in map) {
if (map[charCode] === value) {
return (charCode | 0);
}
}
return -1;
},

amend(map) {
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
!issue8061.pdf
!issue8088.pdf
!issue8125.pdf
!issue8372.pdf
!issue8424.pdf
!bad-PageLabels.pdf
!filled-background.pdf
Expand Down
Binary file added test/pdfs/issue8372.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,13 @@
"link": false,
"type": "text"
},
{ "id": "issue8372-text",
"file": "pdfs/issue8372.pdf",
"md5": "b02fb07364dd00ad5044bd259860da97",
"rounds": 1,
"link": false,
"type": "text"
},
{ "id": "bug894572",
"file": "pdfs/bug894572.pdf",
"md5": "e54a6b0451939f685ed37e3d46e16158",
Expand Down