Ensure that the Cmd
/Name
/Ref
caches are cleared when running other cleanup
code
The purpose of these caches is to reduce peak memory usage, by only ever having *a single* instance of a particular object. However, as-is these caches are never cleared and they will thus remain until the worker is destroyed. This could very well have a negative effect on total memory usage, particularly for large/long documents, hence it seems to make sense to clear out these caches together with various other ones.
This commit is contained in:
parent
9ab1d9f596
commit
5e045bcdba
@ -20,8 +20,8 @@ import {
|
|||||||
warn
|
warn
|
||||||
} from '../shared/util';
|
} from '../shared/util';
|
||||||
import {
|
import {
|
||||||
Dict, isCmd, isDict, isName, isRef, isRefsEqual, isStream, Ref, RefSet,
|
clearPrimitiveCaches, Dict, isCmd, isDict, isName, isRef, isRefsEqual,
|
||||||
RefSetCache
|
isStream, Ref, RefSet, RefSetCache
|
||||||
} from './primitives';
|
} from './primitives';
|
||||||
import { Lexer, Parser } from './parser';
|
import { Lexer, Parser } from './parser';
|
||||||
import {
|
import {
|
||||||
@ -662,6 +662,7 @@ class Catalog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
|
clearPrimitiveCaches();
|
||||||
this.pageKidsCountCache.clear();
|
this.pageKidsCountCache.clear();
|
||||||
|
|
||||||
const promises = [];
|
const promises = [];
|
||||||
|
@ -19,36 +19,44 @@ import { assert } from '../shared/util';
|
|||||||
var EOF = {};
|
var EOF = {};
|
||||||
|
|
||||||
var Name = (function NameClosure() {
|
var Name = (function NameClosure() {
|
||||||
|
let nameCache = Object.create(null);
|
||||||
|
|
||||||
function Name(name) {
|
function Name(name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
Name.prototype = {};
|
Name.prototype = {};
|
||||||
|
|
||||||
var nameCache = Object.create(null);
|
|
||||||
|
|
||||||
Name.get = function Name_get(name) {
|
Name.get = function Name_get(name) {
|
||||||
var nameValue = nameCache[name];
|
var nameValue = nameCache[name];
|
||||||
return (nameValue ? nameValue : (nameCache[name] = new Name(name)));
|
return (nameValue ? nameValue : (nameCache[name] = new Name(name)));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Name._clearCache = function() {
|
||||||
|
nameCache = Object.create(null);
|
||||||
|
};
|
||||||
|
|
||||||
return Name;
|
return Name;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var Cmd = (function CmdClosure() {
|
var Cmd = (function CmdClosure() {
|
||||||
|
let cmdCache = Object.create(null);
|
||||||
|
|
||||||
function Cmd(cmd) {
|
function Cmd(cmd) {
|
||||||
this.cmd = cmd;
|
this.cmd = cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cmd.prototype = {};
|
Cmd.prototype = {};
|
||||||
|
|
||||||
var cmdCache = Object.create(null);
|
|
||||||
|
|
||||||
Cmd.get = function Cmd_get(cmd) {
|
Cmd.get = function Cmd_get(cmd) {
|
||||||
var cmdValue = cmdCache[cmd];
|
var cmdValue = cmdCache[cmd];
|
||||||
return (cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd)));
|
return (cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd)));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Cmd._clearCache = function() {
|
||||||
|
cmdCache = Object.create(null);
|
||||||
|
};
|
||||||
|
|
||||||
return Cmd;
|
return Cmd;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
@ -178,7 +186,7 @@ var Dict = (function DictClosure() {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
var Ref = (function RefClosure() {
|
var Ref = (function RefClosure() {
|
||||||
const refCache = Object.create(null);
|
let refCache = Object.create(null);
|
||||||
|
|
||||||
function Ref(num, gen) {
|
function Ref(num, gen) {
|
||||||
this.num = num;
|
this.num = num;
|
||||||
@ -202,6 +210,10 @@ var Ref = (function RefClosure() {
|
|||||||
return (refValue ? refValue : (refCache[key] = new Ref(num, gen)));
|
return (refValue ? refValue : (refCache[key] = new Ref(num, gen)));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Ref._clearCache = function() {
|
||||||
|
refCache = Object.create(null);
|
||||||
|
};
|
||||||
|
|
||||||
return Ref;
|
return Ref;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
@ -299,8 +311,15 @@ function isStream(v) {
|
|||||||
return typeof v === 'object' && v !== null && v.getBytes !== undefined;
|
return typeof v === 'object' && v !== null && v.getBytes !== undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clearPrimitiveCaches() {
|
||||||
|
Cmd._clearCache();
|
||||||
|
Name._clearCache();
|
||||||
|
Ref._clearCache();
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
EOF,
|
EOF,
|
||||||
|
clearPrimitiveCaches,
|
||||||
Cmd,
|
Cmd,
|
||||||
Dict,
|
Dict,
|
||||||
Name,
|
Name,
|
||||||
|
Loading…
Reference in New Issue
Block a user