Creatures are now cached in memory for computation
Also removed dependency group calculation because the optimisation isn't as useful to reduce DB calls if the creature is in memory anyway
This commit is contained in:
@@ -2,6 +2,7 @@ import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
|
||||
import CreatureProperties,
|
||||
{ DenormalisedOnlyCreaturePropertySchema as denormSchema }
|
||||
from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||
import { loadedCreatures } from '../loadCreatures.js';
|
||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||
import computedOnlySchemas from '/imports/api/properties/computedOnlyPropertySchemasIndex.js';
|
||||
import computedSchemas from '/imports/api/properties/computedPropertySchemasIndex.js';
|
||||
@@ -15,7 +16,6 @@ import linkTypeDependencies from './buildComputation/linkTypeDependencies.js';
|
||||
import computeSlotQuantityFilled from './buildComputation/computeSlotQuantityFilled.js';
|
||||
import CreatureComputation from './CreatureComputation.js';
|
||||
import removeSchemaFields from './buildComputation/removeSchemaFields.js';
|
||||
import assignDependencyGroups from '/imports/api/engine/computation/utility/assignDependencyGroups.js';
|
||||
|
||||
/**
|
||||
* Store index of properties
|
||||
@@ -38,35 +38,22 @@ export default function buildCreatureComputation(creatureId){
|
||||
return computation;
|
||||
}
|
||||
|
||||
export function buildDependencyGroupComputation(depGroupIds, creatureId) {
|
||||
const creature = getCreature(creatureId);
|
||||
const properties = getGroupProperties(depGroupIds);
|
||||
const computation = buildComputationFromProps(properties, creature);
|
||||
return computation;
|
||||
}
|
||||
|
||||
function getProperties(creatureId) {
|
||||
return CreatureProperties.find({
|
||||
if (loadedCreatures.has(creatureId)) {
|
||||
const creature = loadedCreatures.get(creatureId);
|
||||
const props = Array.from(creature.properties.values());
|
||||
return props;
|
||||
}
|
||||
console.time(`fetching from db: ${creatureId}`)
|
||||
const props = CreatureProperties.find({
|
||||
'ancestors.id': creatureId,
|
||||
'removed': {$ne: true},
|
||||
}, {
|
||||
sort: { order: 1 },
|
||||
fields: { icon: 0 },
|
||||
}).fetch();
|
||||
}
|
||||
|
||||
function getGroupProperties(depGroupIds) {
|
||||
console.log({ depGroupIds });
|
||||
if (!depGroupIds || depGroupIds.includes(undefined)) {
|
||||
throw `Expected array full of ids, got ${depGroupIds}`
|
||||
}
|
||||
return CreatureProperties.find({
|
||||
depGroupId: { $in: depGroupIds },
|
||||
'removed': { $ne: true },
|
||||
}, {
|
||||
sort: { order: 1 },
|
||||
fields: { icon: 0 },
|
||||
}).fetch();
|
||||
console.timeEnd(`fetching from db: ${creatureId}`);
|
||||
return props;
|
||||
}
|
||||
|
||||
function getCreature(creatureId){
|
||||
@@ -138,8 +125,5 @@ export function buildComputationFromProps(properties, creature){
|
||||
linkCalculationDependencies(dependencyGraph, prop, computation);
|
||||
});
|
||||
|
||||
// Store the connected groups of the dependency graph
|
||||
assignDependencyGroups(dependencyGraph);
|
||||
|
||||
return computation;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ export default function writeAlteredProperties(computation){
|
||||
'deactivatedBySelf',
|
||||
'deactivatedByAncestor',
|
||||
'deactivatedByToggle',
|
||||
'depGroupId',
|
||||
'damage',
|
||||
...schema.objectKeys(),
|
||||
];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import buildCreatureComputation, { buildDependencyGroupComputation } from './computation/buildCreatureComputation.js';
|
||||
import buildCreatureComputation from './computation/buildCreatureComputation.js';
|
||||
import computeCreatureComputation from './computation/computeCreatureComputation.js';
|
||||
import writeAlteredProperties from './computation/writeComputation/writeAlteredProperties.js';
|
||||
import writeScope from './computation/writeComputation/writeScope.js';
|
||||
@@ -6,14 +6,10 @@ import writeErrors from './computation/writeComputation/writeErrors.js';
|
||||
|
||||
export default function computeCreature(creatureId){
|
||||
if (Meteor.isClient) return;
|
||||
console.time('Compute');
|
||||
const computation = buildCreatureComputation(creatureId);
|
||||
computeComputation(computation, creatureId);
|
||||
}
|
||||
|
||||
// Recompute only some groups of the dependency tree
|
||||
export function computeCreatureDependencyGroup(depGroupIds, creatureId) {
|
||||
const computation = buildDependencyGroupComputation(depGroupIds, creatureId);
|
||||
computeComputation(computation, creatureId);
|
||||
console.timeEnd('Compute');
|
||||
}
|
||||
|
||||
function computeComputation(computation, creatureId) {
|
||||
|
||||
117
app/imports/api/engine/loadCreatures.js
Normal file
117
app/imports/api/engine/loadCreatures.js
Normal file
@@ -0,0 +1,117 @@
|
||||
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
|
||||
import Creatures from '/imports/api/creature/creatures/Creatures.js';
|
||||
|
||||
export const loadedCreatures = new Map(); // creatureId => {creature, properties, etc.}
|
||||
|
||||
export function loadCreature(creatureId, subscription) {
|
||||
if (!creatureId) throw 'creatureId is required';
|
||||
let creature = loadedCreatures.get(creatureId);
|
||||
if (loadedCreatures.has(creatureId)) {
|
||||
creature.subs.add(subscription);
|
||||
} else {
|
||||
console.time(`loading to memory ${creatureId}`);
|
||||
creature = new LoadedCreature(subscription, creatureId);
|
||||
loadedCreatures.set(creatureId, creature);
|
||||
console.timeEnd(`loading to memory ${creatureId}`);
|
||||
console.log('Creatures in memory: ', loadedCreatures.size);
|
||||
}
|
||||
subscription.onStop(() => {
|
||||
unloadCreature(creatureId, subscription);
|
||||
});
|
||||
}
|
||||
|
||||
function unloadCreature(creatureId, subscription) {
|
||||
if (!creatureId) throw 'creatureId is required';
|
||||
const creature = loadedCreatures.get(creatureId);
|
||||
if (!creature) return;
|
||||
creature.subs.delete(subscription);
|
||||
if (creature.subs.size === 0) {
|
||||
creature.stop();
|
||||
loadedCreatures.delete(creatureId);
|
||||
}
|
||||
console.log('Creatures in memory: ', loadedCreatures.size);
|
||||
}
|
||||
|
||||
class LoadedCreature {
|
||||
constructor(sub, creatureId) {
|
||||
// This may be called from a subscription, but we don't want the observers
|
||||
// to be destroyed with it, so use a non-reactive context to observe
|
||||
// the required documents
|
||||
const self = this;
|
||||
Tracker.nonreactive(() => {
|
||||
|
||||
self.subs = new Set([sub]);
|
||||
|
||||
self.properties = new Map();
|
||||
// Observe all creature properties which are needed for computation
|
||||
self.propertyObserver = CreatureProperties.find({
|
||||
'ancestors.id': creatureId,
|
||||
removed: { $ne: true },
|
||||
}, {
|
||||
// sort: { order: 1 },
|
||||
fields: { icon: 0 },
|
||||
}).observeChanges({
|
||||
added(id, fields) {
|
||||
fields._id = id;
|
||||
return self.addProperty(fields)
|
||||
},
|
||||
changed(id, fields) {
|
||||
return self.changeProperty(id, fields);
|
||||
},
|
||||
removed(id) {
|
||||
return self.removeProperty(id);
|
||||
},
|
||||
});
|
||||
|
||||
self.creatures = new Map();
|
||||
// Observe the creature itself
|
||||
self.creatureObserver = Creatures.find({
|
||||
_id: creatureId,
|
||||
}).observeChanges({
|
||||
added(id, fields) {
|
||||
fields._id = id;
|
||||
self.addCreature(fields)
|
||||
},
|
||||
changed(id, fields) {
|
||||
return self.changeCreature(id, fields);
|
||||
},
|
||||
removed(id) {
|
||||
return self.removeCreature(id);
|
||||
},
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
stop() {
|
||||
this.creatureObserver.stop();
|
||||
this.propertyObserver.stop();
|
||||
}
|
||||
addProperty(prop) {
|
||||
this.properties.set(prop._id, prop);
|
||||
}
|
||||
addCreature(creature) {
|
||||
this.creatures.set(creature._id, creature);
|
||||
}
|
||||
changeProperty(id, fields) {
|
||||
this.changeMap(id, fields, this.properties);
|
||||
}
|
||||
changeCreature(id, fields) {
|
||||
this.changeMap(id, fields, this.creatures);
|
||||
}
|
||||
changeMap(id, fields, map) {
|
||||
const doc = map.get(id);
|
||||
for (let key in fields) {
|
||||
if (key === undefined) {
|
||||
delete doc[key];
|
||||
} else {
|
||||
doc[key] = fields[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
removeProperty(id) {
|
||||
this.properties.delete(id)
|
||||
}
|
||||
removeCreature(id) {
|
||||
this.creatures.delete(id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user