SodiumFRP / sodium

Sodium - Functional Reactive Programming (FRP) Library for multiple languages
http://sodium.nz/
Other
848 stars 138 forks source link

Why we have to force garbage collection between tests? #102

Closed nartamonov closed 7 years ago

nartamonov commented 7 years ago

Hello! I'm reading book on FRP right now and for my own experiments I'm trying to play with Sodium-scala in ScalaJS environment, so that is will be possible to use Sodium with Scala in browser. It seems that Sodium is successfully compiled by ScalaJS. Now I'm trying to port test suite to ScalaJS to perform it in NodeJS. Since JS runtimes like NodeJS normally don't provide API to control garbage collector, I would ask, why we have to force garbage collection between tests, is it really needed? Can it be replaced by special testing API in Sodium to manually clear some internal state?

the-real-blackh commented 7 years ago

Well, the Scala version of Sodium is a little out-of-date, so it might need a little tidying up. I changed the semantics of merge() and I believe the Scala version doesn't have the new semantics. But this is all easily fixed.

The explicit gc definitely shouldn't be needed.

Note however that there is a reason why this implementation won't be able to manage memory properly in Javascript. It relies on finalizers, and they don't exist in Javascript. I just recently ported Sodium to Typescript, and your best bet might be to write a Scala wrapper for the Typescript version. This version is completely up-to-date, too. It hasn't received much testing yet, though, apart from passing all the test cases without leaking memory.

nartamonov commented 7 years ago

Oh, I was thought it will be easy, especially after all tests (except memory tests) have passed in nodejs :stuck_out_tongue_winking_eye: Thanks! Do you think it is possible theoretically to have common interface (facade) for typescript wrapper and native scala implementation?

the-real-blackh commented 7 years ago

Yes, it is possible. But, the only thing is, in Javascript we need to explicitly declare any Sodium objects held inside closures. So, we could make a dummy version of this for the Scala implementation.

csw = csw_str.map(lambda1(s => s == "ca" ? ca : cb, [ca, cb])),

https://github.com/SodiumFRP/sodium-typescript/issues/4

Another option would be to change the Scala implementation to use this method instead of finalizers, then just compile it to Javascript. That's just a matter of porting all the right logic over from the Typescript implementation.