Idorobots / spartan

A small Lisp dialect that serves me as a test bed for programming language features.
MIT License
13 stars 3 forks source link

Support for parallelism in the runtimes. #184

Open Idorobots opened 8 months ago

Idorobots commented 8 months ago

Currently, the runtimes support* concurrency by spawning green threads, but it would be really nice to actually run these in parallel. Racket supports places, but that requires serialization of the spawned closure. Similarly, Node.js and the browsers support Workers that, again communicate via message passing and require some form of serialization of the closure that is to be spawned in a given worker.

Right now, it's not possible to serialize functions to pass them along to the worker/place, but it should be possible to pass an offset into a function table. This function table can be created after hoisting the functions into the global scope and stored at the module level. Then, assuming that all the places/workers load the relevant modules, it should be possible to serialize a closure by just serializing each item in its environment and the function offset into the module table:

(define __foo (lambda (env x c) ...))
(define __bar (lambda (env y c) ...))

...
(&make-closure (&make-env (&make-closure (&make-env 23) __foo)) __bar)

;; Becomes:

(define __functions
  (vector (lambda (env x c) ...) ;; __foo
          (lambda (env y c) ...))) ;; __bar
...

(&make-closure (&make-env (&make-closure (&make-env 23) 0)) 1)

This representation can be then passed to a separate place/worker and reconstructed for execution. Alternatively, the offset can be used directly on each &apply call to facilitate hot code reloading.

As a bonus, given this scheme a place/worker doesn't have to run on the same machine anymore, as all values become serializable and can be sent over the wire. Assuming both sides are running compatible module code.