Skip to content

Commit 84d1d2b

Browse files
dlongleydavidlehn
authored andcommitted
Improve active context cache performance.
- Use `Map` to improve cache performance. - Cache initial contexts based on options.
1 parent 9960afa commit 84d1d2b

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

lib/ActiveContextCache.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
2+
* Copyright (c) 2017-2018 Digital Bazaar, Inc. All rights reserved.
33
*/
44
'use strict';
55

@@ -13,31 +13,32 @@ module.exports = class ActiveContextCache {
1313
*/
1414
constructor(size = 100) {
1515
this.order = [];
16-
this.cache = {};
16+
this.cache = new Map();
1717
this.size = size;
1818
}
1919

2020
get(activeCtx, localCtx) {
21-
const key1 = JSON.stringify(activeCtx);
22-
const key2 = JSON.stringify(localCtx);
23-
const level1 = this.cache[key1];
24-
if(level1 && key2 in level1) {
25-
return level1[key2];
21+
const key = JSON.stringify(localCtx);
22+
const level1 = this.cache.get(activeCtx);
23+
if(level1) {
24+
const result = level1.get(key);
25+
return result || null;
2626
}
2727
return null;
2828
}
2929

3030
set(activeCtx, localCtx, result) {
3131
if(this.order.length === this.size) {
3232
const entry = this.order.shift();
33-
delete this.cache[entry.activeCtx][entry.localCtx];
33+
this.cache.get(entry.activeCtx).delete(entry.localCtx);
3434
}
35-
const key1 = JSON.stringify(activeCtx);
36-
const key2 = JSON.stringify(localCtx);
37-
this.order.push({activeCtx: key1, localCtx: key2});
38-
if(!(key1 in this.cache)) {
39-
this.cache[key1] = {};
35+
const key = JSON.stringify(localCtx);
36+
this.order.push({activeCtx, localCtx: key});
37+
let level1 = this.cache.get(activeCtx);
38+
if(!level1) {
39+
level1 = new Map();
40+
this.cache.set(activeCtx, level1);
4041
}
41-
this.cache[key1][key2] = clone(result);
42+
level1.set(key, clone(result));
4243
}
4344
};

lib/context.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const {
2323

2424
const MAX_CONTEXT_URLS = 10;
2525

26+
const INITIAL_CONTEXT_CACHE = new Map();
27+
2628
const api = {};
2729
module.exports = api;
2830

@@ -45,9 +47,9 @@ api.process = ({activeCtx, localCtx, options}) => {
4547
}
4648
const ctxs = _isArray(localCtx) ? localCtx : [localCtx];
4749

48-
// no contexts in array, clone existing context
50+
// no contexts in array, return current active context w/o changes
4951
if(ctxs.length === 0) {
50-
return activeCtx.clone();
52+
return activeCtx;
5153
}
5254

5355
// process each context in order, update active context
@@ -630,14 +632,22 @@ api.expandIri = (activeCtx, value, relativeTo, localCtx, defined) => {
630632
*/
631633
api.getInitialContext = (options) => {
632634
const base = parseUrl(options.base || '');
633-
return {
635+
const key = JSON.stringify({base, processingMode: options.processingMode});
636+
const cached = INITIAL_CONTEXT_CACHE.get(key);
637+
if(cached) {
638+
return cached;
639+
}
640+
641+
const initialContext = {
634642
'@base': base,
635643
processingMode: options.processingMode,
636644
mappings: {},
637645
inverse: null,
638646
getInverse: _createInverseContext,
639647
clone: _cloneActiveContext
640648
};
649+
INITIAL_CONTEXT_CACHE.set(key, initialContext);
650+
return initialContext;
641651

642652
/**
643653
* Generates an inverse context for use in the compaction algorithm, if

0 commit comments

Comments
 (0)