Skip to content

Commit c55122c

Browse files
Merge pull request #12089 from timvandermeij/refsetcache
Convert `RefSetCache` to a proper class and to use a `Map` internally
2 parents a604973 + b19a179 commit c55122c

File tree

2 files changed

+86
-32
lines changed

2 files changed

+86
-32
lines changed

src/core/primitives.js

+27-32
Original file line numberDiff line numberDiff line change
@@ -239,46 +239,41 @@ class RefSet {
239239
}
240240
}
241241

242-
var RefSetCache = (function RefSetCacheClosure() {
243-
// eslint-disable-next-line no-shadow
244-
function RefSetCache() {
245-
this.dict = Object.create(null);
242+
class RefSetCache {
243+
constructor() {
244+
this._map = new Map();
246245
}
247246

248-
RefSetCache.prototype = {
249-
get size() {
250-
return Object.keys(this.dict).length;
251-
},
252-
253-
get: function RefSetCache_get(ref) {
254-
return this.dict[ref.toString()];
255-
},
247+
get size() {
248+
return this._map.size;
249+
}
256250

257-
has: function RefSetCache_has(ref) {
258-
return ref.toString() in this.dict;
259-
},
251+
get(ref) {
252+
return this._map.get(ref.toString());
253+
}
260254

261-
put: function RefSetCache_put(ref, obj) {
262-
this.dict[ref.toString()] = obj;
263-
},
255+
has(ref) {
256+
return this._map.has(ref.toString());
257+
}
264258

265-
putAlias: function RefSetCache_putAlias(ref, aliasRef) {
266-
this.dict[ref.toString()] = this.get(aliasRef);
267-
},
259+
put(ref, obj) {
260+
this._map.set(ref.toString(), obj);
261+
}
268262

269-
forEach: function RefSetCache_forEach(callback) {
270-
for (const i in this.dict) {
271-
callback(this.dict[i]);
272-
}
273-
},
263+
putAlias(ref, aliasRef) {
264+
this._map.set(ref.toString(), this.get(aliasRef));
265+
}
274266

275-
clear: function RefSetCache_clear() {
276-
this.dict = Object.create(null);
277-
},
278-
};
267+
forEach(callback) {
268+
for (const value of this._map.values()) {
269+
callback(value);
270+
}
271+
}
279272

280-
return RefSetCache;
281-
})();
273+
clear() {
274+
this._map.clear();
275+
}
276+
}
282277

283278
function isEOF(v) {
284279
return v === EOF;

test/unit/primitives_spec.js

+59
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
Name,
2828
Ref,
2929
RefSet,
30+
RefSetCache,
3031
} from "../../src/core/primitives.js";
3132
import { StringStream } from "../../src/core/stream.js";
3233
import { XRefMock } from "./test_utils.js";
@@ -336,6 +337,64 @@ describe("primitives", function () {
336337
});
337338
});
338339

340+
describe("RefSetCache", function () {
341+
const ref1 = Ref.get(4, 2);
342+
const ref2 = Ref.get(5, 2);
343+
const obj1 = Name.get("foo");
344+
const obj2 = Name.get("bar");
345+
let cache;
346+
347+
beforeEach(function (done) {
348+
cache = new RefSetCache();
349+
done();
350+
});
351+
352+
afterEach(function () {
353+
cache = null;
354+
});
355+
356+
it("should put, have and get a value", function () {
357+
cache.put(ref1, obj1);
358+
expect(cache.has(ref1)).toBeTruthy();
359+
expect(cache.has(ref2)).toBeFalsy();
360+
expect(cache.get(ref1)).toBe(obj1);
361+
});
362+
363+
it("should put, have and get a value by alias", function () {
364+
cache.put(ref1, obj1);
365+
cache.putAlias(ref2, ref1);
366+
expect(cache.has(ref1)).toBeTruthy();
367+
expect(cache.has(ref2)).toBeTruthy();
368+
expect(cache.get(ref1)).toBe(obj1);
369+
expect(cache.get(ref2)).toBe(obj1);
370+
});
371+
372+
it("should report the size of the cache", function () {
373+
cache.put(ref1, obj1);
374+
expect(cache.size).toEqual(1);
375+
cache.put(ref2, obj2);
376+
expect(cache.size).toEqual(2);
377+
});
378+
379+
it("should clear the cache", function () {
380+
cache.put(ref1, obj1);
381+
expect(cache.size).toEqual(1);
382+
cache.clear();
383+
expect(cache.size).toEqual(0);
384+
});
385+
386+
it("should support iteration", function () {
387+
cache.put(ref1, obj1);
388+
cache.put(ref2, obj2);
389+
390+
const values = [];
391+
cache.forEach(function (value) {
392+
values.push(value);
393+
});
394+
expect(values).toEqual([obj1, obj2]);
395+
});
396+
});
397+
339398
describe("isEOF", function () {
340399
it("handles non-EOF", function () {
341400
const nonEOF = "foo";

0 commit comments

Comments
 (0)