Skip to content

Commit

Permalink
Patch compiler to squeeze more performance
Browse files Browse the repository at this point in the history
- Perform I/O directly instead of using event loop
- Make loops not yield
  • Loading branch information
hieplpvip committed Jan 6, 2024
1 parent de27118 commit 419c342
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 37 deletions.
136 changes: 123 additions & 13 deletions patches/scratch-vm+2.1.46.patch
Original file line number Diff line number Diff line change
@@ -1,18 +1,128 @@
diff --git a/node_modules/scratch-vm/src/blocks/scratch3_looks.js b/node_modules/scratch-vm/src/blocks/scratch3_looks.js
index e84e0f8..214e1b4 100644
--- a/node_modules/scratch-vm/src/blocks/scratch3_looks.js
+++ b/node_modules/scratch-vm/src/blocks/scratch3_looks.js
@@ -336,7 +336,7 @@ class Scratch3LooksBlocks {
}
diff --git a/node_modules/scratch-vm/src/compiler/compat-blocks.js b/node_modules/scratch-vm/src/compiler/compat-blocks.js
index 1c9d8f5..67f2221 100644
--- a/node_modules/scratch-vm/src/compiler/compat-blocks.js
+++ b/node_modules/scratch-vm/src/compiler/compat-blocks.js
@@ -8,12 +8,8 @@
const stacked = [
'looks_changestretchby',
'looks_hideallsprites',
- 'looks_say',
- 'looks_sayforsecs',
'looks_setstretchto',
'looks_switchbackdroptoandwait',
- 'looks_think',
- 'looks_thinkforsecs',
'motion_align_scene',
'motion_glidesecstoxy',
'motion_glideto',
@@ -21,7 +17,6 @@ const stacked = [
'motion_pointtowards',
'motion_scroll_right',
'motion_scroll_up',
- 'sensing_askandwait',
'sensing_setdragmode',
'sound_changeeffectby',
'sound_changevolumeby',
diff --git a/node_modules/scratch-vm/src/compiler/irgen.js b/node_modules/scratch-vm/src/compiler/irgen.js
index c2cf19d..10d3178 100644
--- a/node_modules/scratch-vm/src/compiler/irgen.js
+++ b/node_modules/scratch-vm/src/compiler/irgen.js
@@ -1147,6 +1147,26 @@ class ScriptTreeGenerator {
kind: 'timer.reset'
};

+ case 'looks_say':
+ case 'looks_sayforsecs':
+ return {
+ kind: 'looks.say',
+ message: this.descendInputOfBlock(block, 'MESSAGE')
+ }
+
+ case 'looks_think':
+ case 'looks_thinkforsecs':
+ return {
+ kind: 'looks.think',
+ message: this.descendInputOfBlock(block, 'MESSAGE')
+ }
+
+ case 'sensing_askandwait':
+ return {
+ kind: 'sensing.ask',
+ question: this.descendInputOfBlock(block, 'QUESTION')
+ };
+
default: {
const opcodeFunction = this.runtime.getOpcodeFunction(block.opcode);
if (opcodeFunction) {
diff --git a/node_modules/scratch-vm/src/compiler/jsgen.js b/node_modules/scratch-vm/src/compiler/jsgen.js
index bf9cbf3..3af8700 100644
--- a/node_modules/scratch-vm/src/compiler/jsgen.js
+++ b/node_modules/scratch-vm/src/compiler/jsgen.js
@@ -781,7 +781,7 @@ class JSGenerator {
}
this.source += '}\n'; // close switch
this.source += `if (!${branchVariable}.isLoop) break;\n`;
- this.yieldLoop();
+ // this.yieldLoop();
this.source += '}\n'; // close while
} else {
throw new Error(`Unknown block type: ${blockType}`);
@@ -811,7 +811,7 @@ class JSGenerator {
this.source += `${index}++; `;
this.source += `${this.referenceVariable(node.variable)}.value = ${index};\n`;
this.descendStack(node.do, new Frame(true));
- this.yieldLoop();
+ // this.yieldLoop();
this.source += '}\n';
break;
}
@@ -830,7 +830,7 @@ class JSGenerator {
const i = this.localVariables.next();
this.source += `for (var ${i} = ${this.descendInput(node.times).asNumber()}; ${i} >= 0.5; ${i}--) {\n`;
this.descendStack(node.do, new Frame(true));
- this.yieldLoop();
+ // this.yieldLoop();
this.source += `}\n`;
break;
}
@@ -868,11 +868,11 @@ class JSGenerator {
this.resetVariableInputs();
this.source += `while (${this.descendInput(node.condition).asBoolean()}) {\n`;
this.descendStack(node.do, new Frame(true));
- if (node.warpTimer) {
- this.yieldStuckOrNotWarp();
- } else {
- this.yieldLoop();
- }
+ // if (node.warpTimer) {
+ // this.yieldStuckOrNotWarp();
+ // } else {
+ // this.yieldLoop();
+ // }
this.source += `}\n`;
break;

@@ -1167,6 +1167,18 @@ class JSGenerator {
break;
}

sayforsecs (args, util) {
- this.say(args, util);
+ return this.say(args, util);
const target = util.target;
const usageId = this._getBubbleState(target).usageId;
return new Promise(resolve => {
+ case 'sensing.ask':
+ this.source += `runtime.ext_scratch3_sensing._answer = runtime._scratch_run_ask(${this.descendInput(node.question).asString()});\n`;
+ break;
+
+ case 'looks.say':
+ this.source += `runtime._scratch_run_say(${this.descendInput(node.message).asString()});\n`;
+ break;
+
+ case 'looks.think':
+ this.source += `runtime._scratch_run_think(${this.descendInput(node.message).asString()});\n`;
+ break;
+
default:
log.warn(`JS: Unknown stacked block: ${node.kind}`, node);
throw new Error(`JS: Unknown stacked block: ${node.kind}`);
diff --git a/node_modules/scratch-vm/src/engine/blocks.js b/node_modules/scratch-vm/src/engine/blocks.js
index 71ace3a..4bbcad4 100644
index 71ace3a..7e6357b 100644
--- a/node_modules/scratch-vm/src/engine/blocks.js
+++ b/node_modules/scratch-vm/src/engine/blocks.js
@@ -1,9 +1,9 @@
Expand Down
44 changes: 20 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const Kattio = require('./kattio');

function check_scratch_file(filename) {
const vm = new scratchVM();
vm.convertToPackagedRuntime();

// Block loading extensions (e.g., music)
vm.extensionManager.loadExtensionIdSync =
Expand Down Expand Up @@ -66,14 +67,7 @@ function check_scratch_file(filename) {

function run_scratch_file(filename) {
const vm = new scratchVM();

// Hack to speeding up vm.runtime._step calls
const real_step = vm.runtime._step.bind(vm.runtime);
vm.runtime._step = () => {
for (let loop_count = 0; loop_count < 4000; ++loop_count) {
setImmediate(real_step);
}
};
vm.convertToPackagedRuntime();

// Block loading extensions (e.g., music)
vm.extensionManager.loadExtensionIdSync =
Expand All @@ -84,27 +78,29 @@ function run_scratch_file(filename) {
process.exit(1);
};

// _scratch_run_* are called from the generated code by scratch-vm's compiler
let stdoutBuffer = '';
if (argv['buffer-stdout']) {
vm.runtime.on('SAY', function (target, type, text) {
text = text.toString() + (type === 'say' ? '\n' : '');
vm.runtime._scratch_run_say = function (text) {
stdoutBuffer += text + '\n';
};
vm.runtime._scratch_run_think = function (text) {
stdoutBuffer += text;
});
};
} else {
vm.runtime.on('SAY', function (target, type, text) {
text = text.toString() + (type === 'say' ? '\n' : '');
vm.runtime._scratch_run_say = function (text) {
process.stdout.write(text + '\n');
};
vm.runtime._scratch_run_think = function (text) {
process.stdout.write(text);
});
};
}

vm.runtime.on('QUESTION', function (question) {
vm.runtime._scratch_run_ask = function (question) {
try {
if (question !== null) {
if (question === 'read_token') {
vm.runtime.emit('ANSWER', Kattio.nextToken());
} else {
vm.runtime.emit('ANSWER', Kattio.nextLine());
}
if (question === 'read_token') {
return Kattio.nextToken();
} else {
return Kattio.nextLine();
}
} catch (e) {
writeStderrSync(
Expand All @@ -114,7 +110,7 @@ function run_scratch_file(filename) {
);
process.exit(1);
}
});
};

vm.runtime.on('PROJECT_RUN_STOP', function () {
vm.runtime.quit();
Expand All @@ -132,7 +128,7 @@ function run_scratch_file(filename) {
for (const target of vm.runtime.targets) {
target.setVisible(false);
}
vm.convertToPackagedRuntime();
vm.runtime.precompile();
vm.setTurboMode(true);
vm.setFramerate(250);
vm.start();
Expand Down

0 comments on commit 419c342

Please sign in to comment.