Fast execution of scripts

SpiderPig <[email protected]>
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
I often use Scala in script mode and have always been annoyed at the slow 
compile speed. It takes about 2 - 3 seconds for a script (run with "scala 
script.scala") to start.
But recently I learned there is a way to start a scala script much faster.

Here is some example code that demonstrates this. First you start 
ScriptServer.scala and afterwards you can run any scala script with "java 
RunScript script.scala". The first time it still takes 2 seconds but all 
future invocations run in less than 200 ms.
Now that raises the question if it is possible to add a command line option 
to scala that lets you run scripts that fast. There probably is some 
drawback to this approach I'm not aware of but I'm guessing it would work 
at least for most small scripts.

ScriptServer.scala:
import javax.script._
import java.net._

val engine = new ScriptEngineManager().getEngineByName("scala")

val ss = new ServerSocket(10123)

while(true) {
  val so = ss.accept()
  val in = scala.io.Source.fromInputStream(so.getInputStream)
  val script = engine.asInstanceOf[Compilable].compile(in.mkString)
  script.eval()
}.

RunScript.java:
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.file.*;
import java.util.List;

public class RunScript {
  public static void main(String[] args) throws Exception {
    String name = args[0];
    List<String> lines = Files.readAllLines(Paths.get(name));
    PrintWriter out = new PrintWriter(new Socket("localhost", 
10123).getOutputStream());
    for(String line: lines) {
      out.println(line);
    }
    out.flush();
    out.close();
  }
}


-- 
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.