From 77738b001f127dea3ecc1fb482bf4b7822b9dd31 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Fri, 6 Mar 2015 23:11:55 +0300 Subject: [PATCH] Speed-up context switching --- .../resources/org/teavm/javascript/runtime.js | 15 ++++++++++----- .../org/teavm/samples/async/AsyncProgram.java | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js index edd420b48..4a7dd2c94 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -458,7 +458,13 @@ function $rt_rootInvocationAdapter(f) { console.error(!hasWrappers ? prefix : "Root cause is %s at %o", e.message, e.stack); } }); - return f.apply(this, args); + f.apply(this, args); + var thread = $rt_getThread(); + while (thread.postponed) { + var postponed = thread.postponed; + thread.postponed = null; + postponed(); + } } } function $rt_mainWrapper(f) { @@ -485,16 +491,15 @@ function $rt_s(index) { } var $rt_continueCounter = 0; function $rt_continue(f) { - if ($rt_continueCounter++ == 10) { + if ($rt_continueCounter++ == 40) { $rt_continueCounter = 0; return function() { var self = this; var args = arguments; var thread = $rt_getThread(); - setTimeout(function() { - $rt_setThread(thread); + thread.postponed = function() { f.apply(self, args); - }, 0); + }; }; } else { return f; diff --git a/teavm-samples/teavm-samples-async/src/main/java/org/teavm/samples/async/AsyncProgram.java b/teavm-samples/teavm-samples-async/src/main/java/org/teavm/samples/async/AsyncProgram.java index c21160c6e..e03956bb0 100644 --- a/teavm-samples/teavm-samples-async/src/main/java/org/teavm/samples/async/AsyncProgram.java +++ b/teavm-samples/teavm-samples-async/src/main/java/org/teavm/samples/async/AsyncProgram.java @@ -87,7 +87,7 @@ public final class AsyncProgram { prime[i] = true; } StringBuilder sb = new StringBuilder(); - for (int i = 0; i < 100; ++i) { + for (int i = 0; i < 1000; ++i) { if (prime[i]) { sb.append(i).append(' '); }