Skip to content

Commit

Permalink
Remove deep cloning of contexts during parsing
Browse files Browse the repository at this point in the history
This improves overall parsing performance
  • Loading branch information
jeswr authored Nov 8, 2023
1 parent 5a23c1f commit dc18d7f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/ParsingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class ParsingContext {
|| scopedContext[key]['@context']['@propagate']; // Propagation is true by default

if (propagate !== false || i === keysOriginal.length - 1 - offset) {
contextRaw = scopedContext;
contextRaw = { ...scopedContext };

// Clean up final context
delete contextRaw['@propagate'];
Expand Down
8 changes: 2 additions & 6 deletions lib/entryhandler/keyword/EntryHandlerKeywordType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,11 @@ export class EntryHandlerKeywordType extends EntryHandlerKeyword {
if (hasTypedScopedContext) {
// Do not propagate by default
scopedContext = scopedContext.then((c) => {
if (!('@propagate' in c.getContextRaw())) {
c.getContextRaw()['@propagate'] = false;
}

// Set the original context at this depth as a fallback
// This is needed when a context was already defined at the given depth,
// and this context needs to remain accessible from child nodes when propagation is disabled.
if (c.getContextRaw()['@propagate'] === false) {
c.getContextRaw()['@__propagateFallback'] = context.getContextRaw();
if (c.getContextRaw()['@propagate'] !== true) {
return new JsonLdContextNormalized({ ...c.getContextRaw(), '@propagate': false, '@__propagateFallback': context.getContextRaw() });
}

return c;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"buffer": "^6.0.3",
"canonicalize": "^1.0.1",
"http-link-header": "^1.0.2",
"jsonld-context-parser": "^2.3.3",
"jsonld-context-parser": "^2.4.0",
"rdf-data-factory": "^1.1.0",
"readable-stream": "^4.0.0"
},
Expand Down
10 changes: 4 additions & 6 deletions test/ParsingContext-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {JsonLdContextNormalized} from "jsonld-context-parser";
import {JsonLdContextNormalized, ContextParser} from "jsonld-context-parser";
import {ParsingContext} from "../lib/ParsingContext";
import {ParsingContextMocked} from "../mocks/ParsingContextMocked";

Expand Down Expand Up @@ -607,22 +607,20 @@ describe('ParsingContext', () => {
describe('for type-scoped and property-scoped contexts', () => {

beforeEach(() => {
parsingContext.contextTree.setContext(['', 'a'], Promise.resolve(new JsonLdContextNormalized({
'@__propagateFallback': { fallback: true },
'@propagate': false,
const contextParser = new ContextParser();
parsingContext.contextTree.setContext(['', 'a'], contextParser.parse({
'@vocab': 'http://bla.org/',
'bar': {
'@context': {
baz: { '@type': '@vocab' },
},
},
})));
}));
});

it('should consider the applicable property-scoped context', async () => {
return expect(await parsingContext.getContext(['', 'a', 'bar'], 0))
.toEqual(new JsonLdContextNormalized({
'@__propagateFallback': { fallback: true },
'@vocab': 'http://bla.org/',
'bar': { '@id': 'http://bla.org/bar' },
'baz': { '@type': '@vocab', '@id': 'http://bla.org/baz' },
Expand Down
9 changes: 4 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2518,14 +2518,13 @@ jsonld-context-parser@^2.0.2, jsonld-context-parser@^2.1.3:
http-link-header "^1.0.2"
relative-to-absolute-iri "^1.0.5"

jsonld-context-parser@^2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.3.3.tgz#0bdab9eb5cb4b7e68aa7d6c38e58455363caaf9c"
integrity sha512-H+REInOx7XI2ciF8wJV31D20Bh+ofBmEjN2Tkds51vypqDJIiD341E5g+hYyrEInIKRnbW58TN/Ehz+ACT0l0w==
jsonld-context-parser@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.4.0.tgz#fae15a56c5ceabd1c4520ab1a9cc12c9a0a8b67d"
integrity sha512-ZYOfvh525SdPd9ReYY58dxB3E2RUEU4DJ6ZibO8AitcowPeBH4L5rCAitE2om5G1P+HMEgYEYEr4EZKbVN4tpA==
dependencies:
"@types/http-link-header" "^1.0.1"
"@types/node" "^18.0.0"
canonicalize "^1.0.1"
cross-fetch "^3.0.6"
http-link-header "^1.0.2"
relative-to-absolute-iri "^1.0.5"
Expand Down

0 comments on commit dc18d7f

Please sign in to comment.