r/Clojure • • Aug 15 '20

Introducing Tapestry - Loom for Clojure

https://github.com/teknql/tapestry
77 Upvotes

12 comments sorted by

10

u/bsless Aug 16 '20

Few questions:

  • Why dynamic bindings?

  • Why go for fiber terminology when Loom authors sort of abandoned it and just call it virtual thread?

  • Why tie implementation to manifold?

10

u/Rschmukler Aug 16 '20

Great questions! Thanks for taking the time to really look at it. This is the reason I wanted to get the community's eyes on it early.

Why Dynamic Bindings

The dynamic bindings are used to inherit executor context between newly spawned fibers. Right now this is mostly used to implement the bounded parallelism, but it will also be used to inherit the withDeadline executor for fibers that die after a period of time. I tried to implement this once already, but it seemed like the JVM had a bug that caused it to deadlock. Re. why use it at all - Both future and manifold.deferred/future inherit dynamic scope from the caller, so I wanted to maintain compatibility with the feel of those libraries.

Why Fibers

I don't have any particular allegiance to the name fiber, although that term has dated back to C++, and even has a fancy wikipedia page. In general I'm a sucker for metaphoric names. Fibers being lighter weight threads has always "made sense." I think tapestry.core/virtual-thread and tapestry.core/virtual-thread-loop are viable, if there are strong reasons to not use fiber.

Why use Manifold today?

This one is a long one, but a great one. I do ultimately want to remove manifold, and removing the deferred and using a CompletableFuture would be easy. However I think how its streams work is genius, and bringing something as thoughtful to Tapestry will take some time in the ol' hammock.

In general with concurrency libraries you tend to have some sort of work-queue abstraction. In core.async it's the channel, and in manifold it's the stream - either way it's a way to introduce work into the system. Manifold does something really clever though: It makes streams feel like immutable sequences, handling broadcasting messages into multiple downstream queues. Consider the following code:

(defn order-system
  []
  (let [orders (s/stream)
        order-email-notifier (->> orders
                                  (s/map order->email)
                                  (asyncly send-email!)
        order-storage        (->> orders
                                  (s/buffer 100 1000)
                                  (s/consume store-order-batch!)]
     orders)

Multiplexing this with core.async would require multiplexing channels with tap. This is explicit, but always felt a bit lower level than what manifold had on offer. I'd love to do something similar in tapestry, but using a plain lazy-seq would cause all items to be cached in memory, which would explode eventually. Right now I'm currently thinking about what an ArrayBlockingQueue abstraction could look like but it's definitely early days.

Thanks again for the questions and taking the time to look! Also I should mention that I'm on the Clojure Slack, and there is a #teknql channel if you want to help shape the library's direction.

6

u/bsless Aug 16 '20

This is really funny that you started this work because I only just started feeling it out myself:

~/Projects/loom-playground λ ll project.clj 
-rw-rw-r-- 1 bensless bensless 407 Aug 13 16:25 project.clj

I started attacking the problem more from the direction of structured concurrency and providing core compatible idioms. My impressions so far, directed by my questions:

  • Dynamic bindings: I also had that deadlock! Besides that, how about using a global Scoped var for default executor context? Than you can create binding contexts for threads without using dynamic bindings. I wrote scope creating and handling macros, will be glad to share

  • Names: I went for lack of originality and called it vfuture and vthread :)

  • Manifold, etc.: What I found myself aiming at is a bit more modular instead of attaching to one abstraction in particular. Promesa has CompletableFutures covered, core.async does channels, Manifold does streams and deferreds. There are also libraries like Claypool which already support some idiomatic abstractions with pluggable pools/executors, which can easily be interfaced with. I propose a modular approach, similar to how Metosin organized reitit.

I'll pop over to the slack channel and maybe hit you with a few PRs since you beat me to it ;)

6

u/Rschmukler Aug 16 '20

Dynamic: What's the disadvantage you see with dynamic bindings? Since they are implemented as Thread Context in java anyway, and most of the clojure ecosystem seems to support inheriting the bindings (eg. `future`) why force a singleton?

Names: Fair enough! I'm not super opinionated. I will say that fibers have meaning to me outside of java, while I've never seen any language call them virtual threads. It's a very literal name, and that's useful, but it feels a bit like fighting against an already established trend.

Manifold: I do think that a primitive work queue (channel, or stream) is essential in a concurrency library. I'm definitely in favor of modularizing it to make it work with other libraries in the ecosystem (and eventually ripping out manifold), but right now manifold is saving me from having to implement the queue myself - and as I said, I think it will take some thought to get the in-library implementation right.

I welcome the chat and the PRs!

3

u/bsless Aug 16 '20

One disadvantage with dynamic bindings is performance overhead, but it might be negligible given the tasks virtual threads are used for. They introduce another problem which is difficulty in debugging. This might be the mother of all premature optimizations, so don't get over excited over this point. Still for clarity I prefer an argument over a dynamic var.

Why use a singleton? For the kind of work expected of virtual threads, besides using shared context for early termination, it doesn't really matter if they all share one executor in common, no? Besides, you also defined a global executor, the only difference is that it can by dynamically rebound.

Really don't know what's the best option here.

wrt names, maybe I'm being overly literal. On one hand, calling it a virtual-future makes it clear that you're getting something that behaves like clojure.core/future, virtual-thread for clojure.core.async/thread, etc, while fiber is slightly more ambiguous. On the other hand, fibers share a general idea which is universally recognized and was the previous name before Loom maintainers realized extending Thread was the way to go.

queues: Sadly, I don't think Carriers are part of Loom yet. Being an accommodating fellow, I am partial to supporting all the main concurrency abstractions available in Clojure (3).

3

u/Rschmukler Aug 16 '20 edited Aug 16 '20

Because I think inheriting the dynamic bindings is expected (that's how clojure.core/future and manifold.core/future both behave), I think the work around copying the ThreadContext and rebinding it will have to happen no matter what. Since the cost already exists, I'm just using it. As you said, I imagine that the performance overhead of the dynamic binding is negligible to spawning a new fiber - but I suppose I haven't benchmarked this yet, so I should! I agree with you about dynamic variables vs. arguments for public facing APIs but it's mostly an implementation detail to enable with-max-parallism and ultimately with-deadline, and having that be inherited across spawned fibers. I'm not sure how else I would be able to inherit it without ThreadContexts, ie. I think this is one of the usecases thread contexts were designed for. Perhaps it could also be done with Continuation Scopes, but I'm not sure if that's not what fibers are doing under the hood to implement their thread contexts .

I'm not familiar with the concept of carriers, what are those?

2

u/didibus Aug 18 '20

Is there a target date for the full release of Loom?

2

u/Rschmukler Aug 19 '20

If they’ve officially announced it, I haven’t heard it. They are currently using the JDK-16 branch, so that’d be March 2021 at the earliest. That being said, it was on JDK-15 earlier and it seems unlikely that they were ever targeting September for a release, so I wouldn’t put too much stake into JDK 16 implying a March release.

4

u/eprozium Aug 16 '20

There's a very popular framework with Apache https://tapestry.apache.org/ with the same name :).

1

u/liveoneggs Aug 15 '20

3

u/EmmanuelOga Aug 15 '20

I actually thought of this https://github.com/aysylu/loom :-)

2

u/Rschmukler Aug 16 '20

Indeed, I didn't want to squat on the already great loom library in the clojure land. As to what the JVM people decided to call it, I guess they hadn't considered the 1990 classic.