Fork me on GitHub
#cljs-dev
<
2019-09-09
>
andy.fingerhut20:09:07

I have no use case for such a thing, but was curious whether anyone had considered storing Clojure data structures, e.g. vectors, maps, sets, inside of a Web Worker shared buffer? I guess not just considered, but actually tried it, and/or looked at any changes in the ClojureScript implementation that would be required to support it?

favila01:09:20

last time I checked transit to a string in a postMessage was much faster

favila01:09:40

(Admittedly many years ago now. Since then structured cloning and dataview apis have gotten faster, maybe the situation is different now)

favila01:09:07

Fressian-cljs and transit msgpack both exist in browser so in theory you could use shared buffers, but shared buffers offer nothing over arraybuffer transferin these cases

andy.fingerhut01:09:55

Background on my question: I am writing up an article on Java synchronization techniques used in the Clojure/Java implementation to ensure that data is written by one thread in a way that other threads are guaranteed to get the most up to date version. In the introduction I have written a brief note that these issues do not apply for single-threaded ClojureScript, and then remembered that Web Workers and shared memory proposals exist for JavaScript, and was curious if anything had been done in ClojureScript to take advantage of that.

favila01:09:19

To really leverage sharedbuffer would require some in-memory layout for cljs data structures like eg captnproto (and some memory management!)

andy.fingerhut01:09:45

Makes sense. It certainly doesn't appear that doing what I asked about is any kind of sweet spot for any use case that anyone right now wants to write lots of tricky code for. I was primarily curious if anyone knew whether someone had tried going that route.

favila01:09:21

Java/Clojure concurrency is mostly about publishing pointer updates assuming all memory is shared

favila01:09:28

In js, there’s no way to share a pointer, threads are already fully isolated. All sharing and concurrency features are concerned with zero-copy array/memory publishing and sharing

favila01:09:57

So I doubt anyone has done this in cljs land because the problem doesn’t even make sense

andy.fingerhut01:09:50

I mean, if someone wanted to get tricky, they could use integers in shared memory as indices into arrays, effectively using them like "pointers", so could create potential inter-thread synchronization problems.