mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
cache the set of actively monitored blocks
Projects can have 100's of potentially monitored blocks. Caching the set of monitored blocks can save time in Runtime._step every frame.
This commit is contained in:
parent
de86eb3f19
commit
a3f9aa1e22
1 changed files with 26 additions and 7 deletions
|
@ -63,7 +63,14 @@ class Blocks {
|
|||
* execute.
|
||||
* @type {object.<string, object>}
|
||||
*/
|
||||
_executeCached: {}
|
||||
_executeCached: {},
|
||||
|
||||
/**
|
||||
* A cache of block IDs and targets to start threads on as they are
|
||||
* actively monitored.
|
||||
* @type {Array<{blockId: string, target: Target}>}
|
||||
*/
|
||||
_monitored: null
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -491,6 +498,7 @@ class Blocks {
|
|||
this._cache.procedureParamNames = {};
|
||||
this._cache.procedureDefinitions = {};
|
||||
this._cache._executeCached = {};
|
||||
this._cache._monitored = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -667,14 +675,25 @@ class Blocks {
|
|||
* @param {!object} runtime Runtime to run all blocks in.
|
||||
*/
|
||||
runAllMonitored (runtime) {
|
||||
Object.keys(this._blocks).forEach(blockId => {
|
||||
if (this.getBlock(blockId).isMonitored) {
|
||||
if (this._cache._monitored === null) {
|
||||
this._cache._monitored = Object.keys(this._blocks)
|
||||
.filter(blockId => this.getBlock(blockId).isMonitored)
|
||||
.map(blockId => {
|
||||
const targetId = this.getBlock(blockId).targetId;
|
||||
runtime.addMonitorScript(blockId, targetId ? runtime.getTargetById(targetId) : null);
|
||||
}
|
||||
return {
|
||||
blockId,
|
||||
target: targetId ? runtime.getTargetById(targetId) : null
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const monitored = this._cache._monitored;
|
||||
for (let i = 0; i < monitored.length; i++) {
|
||||
const {blockId, target} = monitored[i];
|
||||
runtime.addMonitorScript(blockId, target);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Block management: delete blocks and their associated scripts. Does nothing if a block
|
||||
* with the given ID does not exist.
|
||||
|
|
Loading…
Reference in a new issue