IronJS icon indicating copy to clipboard operation
IronJS copied to clipboard

Loading coffee-script compiler is very slow

Open andrewdavey opened this issue 13 years ago • 20 comments

I'm trying to execute the Coffee-Script compiler ( https://raw.github.com/jashkenas/coffee-script/master/extras/coffee-script.js ) to bring it into scope and then use it to compile coffee-script into javascript

var ctx = new IronJS.Hosting.CSharp.Context();
ctx.ExecuteFile("coffee-script.js");

However the ExecuteFile call maxes out a CPU and never seems to stop. I left it running for over a minute. Any ideas what's going wrong, is the execution engine getting stuck in a tight loop?

I'm using IronJS v0.2

andrewdavey avatar Jun 18 '11 18:06 andrewdavey

Make sure you are running in release mode. In debug mode, there is a lot going on to aid in debugging.

Can you try it in release mode and let us know if that works better?

otac0n avatar Jun 18 '11 22:06 otac0n

Pulled master. Built Release.

Now when creating the context I get a TypeLoadException:

Could not load type 'IronJS.BoxedValue' from assembly 'IronJS, Version=0.2.1.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 4 that is incorrectly aligned or overlapped by a non-object field.

The code breaks into Hosting.fs line 211

let context = 
    FSharp.createContext()

Any ideas what's going wrong here? F# or 64 bit issues perhaps?

andrewdavey avatar Jun 19 '11 08:06 andrewdavey

Changing my console application to be x86 instead of Any CPU seemed to fix that.

andrewdavey avatar Jun 19 '11 08:06 andrewdavey

OK, so now it takes about 30 seconds to load the coffee-script compiler. Slow - but manageable for now I guess. However running the following code:

var ctx = new IronJS.Hosting.CSharp.Context();
ctx.ExecuteFile(@"..\..\coffee-script.js");
var coffeeScript = ctx.GetGlobal("CoffeeScript").Object;
var compile = coffeeScript.Get("compile").Func;
var js = compile.Call(coffeeScript, "a = x: 2").String;
Console.WriteLine(js);
Console.ReadLine();

Results in:

Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.
   at System.String.wstrcpy(Char* dmem, Char* smem, Int32 charCount)
   at System.String.CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count)
   at System.IO.TextWriter.WriteLine(String value)
   at System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
   at System.Console.WriteLine(String value)
   at ConsoleApplication1.Program.Main(String[] args) at line 24

Why would a corrupt string be marshalled back from the JavaScript function call?

andrewdavey avatar Jun 19 '11 08:06 andrewdavey

Hey!

First, we're looking into why the CoffeScript compiler is taking such a long time to load, there is something going on here that we haven't found that is slowing it down considerably. Sadly I haven't had time (can't speak for @otac0n) to look into it closer.

However, the problem you're experiencing is the problem the corrupt memory is because you're accessing the fields of the BoxedValue struct directly.

Basically where you're doing compile.Call(coffeeScript, "a = x: 2").String you should not access .String but rather rewrite the whole thee previous lines to look like this:

var coffeeScript = ctx.GetGlobalAs<IronJS.CommonObject>("CoffeScript");
var compile = coffeeScript.GetT<IronJS.FunctionObject>("compile");
var js = compile.Call(coffeeScript, "a = x: 2");
var source = IronJS.TypeConverter.ToString(js);

I'm aware the API is rather inconsistent!

fholm avatar Jun 19 '11 10:06 fholm

Thanks for the quick response and letting me know the correct API calls. I was basically coding blind with Intellisense before :)

andrewdavey avatar Jun 19 '11 11:06 andrewdavey

So, is this resolved for you? I know that we need to re-imagine the API to be more IntelliSense friendly...

otac0n avatar Jun 19 '11 14:06 otac0n

ExecuteFile for the CoffeeScript compiler is still about 30 seconds for me (release build of IronJS). So can we leave this issue open until the cause of the slow loading has been tracked down?

andrewdavey avatar Jun 20 '11 07:06 andrewdavey

Yeah, let's leave it open. I'll try to do some preliminary tests when I come home to track down the issue.

fholm avatar Jun 20 '11 08:06 fholm

Hrm... here are my timings: Load CoffeeScript: 00:00:05.0645218 Compiling simple function: 00:00:03.8926191

I'm running Release, x86, on a 64 machine. (since that is what I have found to be the fastest.) Are you running an x86 or a 64-bit build?

otac0n avatar Jun 22 '11 02:06 otac0n

Release x86 build running on Windows 7 64bit - MacBook Pro Intel Core2 Duo @ 2.4Ghz

Test program (also Release x86 build):

var ctx = new IronJS.Hosting.CSharp.Context();

var stopWatch = new Stopwatch();
stopWatch.Start();
ctx.ExecuteFile(@"..\..\coffee-script.js");
Console.WriteLine("Coffee-Script compiler loaded:");
Console.WriteLine(stopWatch.Elapsed.ToString());

stopWatch.Restart();

var coffeeScript = ctx.GetGlobalAs<IronJS.CommonObject>("CoffeeScript");
var compile = coffeeScript.GetT<IronJS.FunctionObject>("compile");
var js = compile.Call(coffeeScript, "a = x: 2");
var source = IronJS.TypeConverter.ToString(js);

Console.WriteLine("Simple code compiled:");
Console.WriteLine(stopWatch.Elapsed.ToString());

Console.WriteLine(source);

Output:

Coffee-Script compiler loaded:
00:00:28.8578343
Simple code compiled:
00:00:05.4059259
(function() {
  var ;
  a = {
    x: 2
  };
}).call(this);

Also note the incorrect JavaScript being generated! var ; should be var a;

andrewdavey avatar Jun 22 '11 07:06 andrewdavey

With an AnyCPU release build, I get 10s to load the compiler, but I get the same var ; problem.

Anything I could do to help ?

thinkbeforecoding avatar Jun 29 '11 08:06 thinkbeforecoding

The 'var ;' thing should be fixed as of about 9:00 Central last night. Can you try grabbing the most recent version off of NuGet, or pulling 'master' again.

otac0n avatar Jun 29 '11 11:06 otac0n

ok i'll try that, and I tell you.

cheers

http://thinkbeforecoding.com

On Wed, Jun 29, 2011 at 1:33 PM, otac0n < [email protected]>wrote:

The 'var ;' thing should be fixed as of about 9:00 Central last night. Can you try grabbing the most recent version off of NuGet, or pulling 'master' again.

Reply to this email directly or view it on GitHub: https://github.com/fholm/IronJS/issues/43#issuecomment-1466343

thinkbeforecoding avatar Jun 29 '11 13:06 thinkbeforecoding

Nice, it's working fine. Thanks.

thinkbeforecoding avatar Jun 29 '11 13:06 thinkbeforecoding

Thanks for the fix. I'm still seeing ~30 seconds to load the CoffeeScript compiler, but at this point correctness is more important than performance :)

andrewdavey avatar Jun 30 '11 08:06 andrewdavey

No problem. We'll keep this issue open to track the current performance issues with compilation.

otac0n avatar Jun 30 '11 11:06 otac0n

I also experience the issue of CoffeScript "hanging" when being compiled. Additionaly to the lag, I experience a huge memory load, especially when run as 64 bit process. The process aborts with an OutOfMemoryException after allocating nearly 4GB.

IronJS CoffeeScript loading issue

avonwyss avatar Sep 26 '11 15:09 avonwyss

Interesting... I'll see if I can repro. What version of CoffeeScript are you using?

otac0n avatar Sep 27 '11 20:09 otac0n

I believe this was with Version 1.1.0.

avonwyss avatar Sep 27 '11 23:09 avonwyss