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

perf: use set #418

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/N3Reasoner.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class N3Reasoner {
if (index2 = index1[value]) {
if (v1) val1.value = Number(value);
v2 = !(value = val2.value);
for (value in v2 ? index2 : { [value]: index2[value] }) {
for (value of (v2 ? index2 : [value])) {
if (v2) val2.value = Number(value);

if (i === rule.premise.length - 1)
Expand Down
45 changes: 24 additions & 21 deletions src/N3Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import N3Writer from './N3Writer';
const ITERATOR = Symbol('iter');

function merge(target, source, depth = 4) {
if (depth === 0)
return Object.assign(target, source);
if (depth === 0) {
target ||= new Set();
for (const key of source)
target.add(key)
return target;
}

target ||= Object.create(null);

for (const key in source)
target[key] = merge(target[key] || Object.create(null), source[key], depth - 1);
target[key] = merge(target[key], source[key], depth - 1);

return target;
}
Expand Down Expand Up @@ -132,7 +138,7 @@ export default class N3Store {
for (const graphKey in graphs)
for (const subjectKey in (subjects = graphs[graphKey].subjects))
for (const predicateKey in (subject = subjects[subjectKey]))
size += Object.keys(subject[predicateKey]).length;
size += subject[predicateKey].size;
return this._size = size;
}

Expand All @@ -143,22 +149,22 @@ export default class N3Store {
_addToIndex(index0, key0, key1, key2) {
// Create layers as necessary
const index1 = index0[key0] || (index0[key0] = {});
const index2 = index1[key1] || (index1[key1] = {});
const index2 = index1[key1] || (index1[key1] = new Set());
// Setting the key to _any_ value signals the presence of the quad
const existed = key2 in index2;
const existed = index2.has(key2);
if (!existed)
index2[key2] = null;
index2.add(key2);
return !existed;
}

// ### `_removeFromIndex` removes a quad from a three-layered index
_removeFromIndex(index0, key0, key1, key2) {
// Remove the quad from the index
const index1 = index0[key0], index2 = index1[key1];
delete index2[key2];
index2.delete(key2);

// Remove intermediary index layers if they are empty
for (const key in index2) return;
if (index2.size > 0) return;
delete index1[key1];
for (const key in index1) return;
delete index0[key0];
Expand Down Expand Up @@ -188,10 +194,10 @@ export default class N3Store {
if (index2 = index1[value1]) {
parts[name1] = this._termFromId(entityKeys[value1]);
// If a key is specified, use only that part of index 2, if it exists.
const values = key2 ? (key2 in index2 ? [key2] : []) : Object.keys(index2);
const values = key2 ? (index2.has(key2) ? [key2] : []) : index2;
// Create quads for all items found in index 2.
for (let l = 0; l < values.length; l++) {
parts[name2] = this._termFromId(entityKeys[values[l]]);
for (const o of values) {
parts[name2] = this._termFromId(entityKeys[o]);
yield this._factory.quad(parts.subject, parts.predicate, parts.object, graph);
}
}
Expand Down Expand Up @@ -229,7 +235,7 @@ export default class N3Store {
_loopBy2Keys(index0, key0, key1, callback) {
let index1, index2, key2;
if ((index1 = index0[key0]) && (index2 = index1[key1])) {
for (key2 in index2)
for (key2 of index2)
callback(key2);
}
}
Expand All @@ -249,9 +255,9 @@ export default class N3Store {
for (const value1 in index1) {
if (index2 = index1[value1]) {
// If a key is specified, count the quad if it exists
if (key2) (key2 in index2) && count++;
if (key2) (index2.has(key2)) && count++;
// Otherwise, count all quads
else count += Object.keys(index2).length;
else count += index2.size;
}
}
}
Expand Down Expand Up @@ -368,7 +374,7 @@ export default class N3Store {
!(object = object && this._termToNumericId(object)) || !(graphItem = graphs[graph]) ||
!(subjects = graphItem.subjects[subject]) ||
!(predicates = subjects[predicate]) ||
!(object in predicates))
!predicates.has(object))
return false;

// Remove it from all indexes
Expand Down Expand Up @@ -834,12 +840,9 @@ export default class N3Store {
s1 = s1.subjects;
for (const subject in (s2 = g2[graph].subjects)) {
if (!(p1 = s1[subject])) return false;
for (const predicate in (p2 = s2[subject])) {
if (!(o1 = p1[predicate])) return false;
for (const object in p2[predicate])
if (!(object in o1)) return false;
for (const predicate in (p2 = s2[subject]))
if (!(o1 = p1[predicate]) || !p2[predicate].isSubsetOf(o1)) return false;
}
}
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion test/N3Reasoner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ describe('Reasoner', () => {

it('Should correctly apply the deep taxonomy benchmark', async () => {
for (let i = 0; i < 5; i++) {
const store = generateDeepTaxonomy(10 ** i);
const store = new Store([...generateDeepTaxonomy(10 ** i)]);

new Reasoner(store).reason(SUBCLASS_RULE);

Expand Down
11 changes: 11 additions & 0 deletions test/N3Store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import { Readable } from 'readable-stream';
import arrayifyStream from 'arrayify-stream';

describe('Store', () => {
describe('A store w. one elemnt', () => {
it('should have size 0', () => {
const store = new Store();
expect(store.addQuad(new Quad(new NamedNode('s1'), new NamedNode('p2'), new NamedNode('o2')))).toBe(true);
expect(store.size).toEqual(1);
});
});

describe('The Store export', () => {
it('should be a function', () => {
expect(Store).toBeInstanceOf(Function);
Expand Down Expand Up @@ -2021,6 +2029,9 @@ describe('Store', () => {
store.add(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2')));
store.add(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o4')));
expect(m.next().value).toEqual(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o3')));
expect(m.next().value).toEqual(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o0')));
expect(m.next().value).toEqual(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2')));
expect(m.next().value).toEqual(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o4')));
expect(m.next().done).toBe(true);
expect([...store.match(null, null, null, null)]).toHaveLength(5);
},
Expand Down
Loading