Re: Fast execution of scripts
Oliver Ruebenacker <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAA=X4OCfhzA8-TN0ruiUbtsdxZ89Ap9HJ_NE8bNY6yJi77pZ_Q@mail.gmail.com> |
Hello,
Clever idea. The obvious drawback is that you are now running two JVMs,
one for the server and one for the client. JVMs can be rather big,
depending on your setup. Also, if two clients ever want to run
simultaneously on the same machine, they'd conflict over network ports.
It is an interesting question why your client runs so much faster. It
still needs on every invocation to start up a new JVM and load the Java
runtime classes. The only reason I can think of is that is does not need to
load the Scala runtime and Scala compiler classes each time. But two
seconds seems like a long time for this.
Probably the scala command can be made a lot faster by reading the Scala
runtime and compiler classes from a faster disk, maybe even a RAM disk.
It would be awesome if the scala command could be made to avoid starting
a new JVM each time.
Best, Oliver
On Sun, Feb 15, 2015 at 1:39 AM, SpiderPig <[email protected]> wrote:
> 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.
>
--
Oliver Ruebenacker
Solutions Architect at Altisource Labs <http://www.altisourcelabs.com/>
Be always grateful, but never satisfied.
--
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.