r/Clojure • • Jan 14 '23

Deep dive into Clojure's sequences, jank's runtime, and the rabbit hole I went down to optimize things

https://jank-lang.org/blog/2023-01-13-optimizing-sequences/
79 Upvotes

24 comments sorted by

7

u/ArmchairSpartan Jan 14 '23

Awesome to see this project is still going. What a massive undertaking, it will be a game changer for sure.

-3

u/deaddyfreddy Jan 14 '23

it will be a game changer for sure.

It's very cool to have a thing like that, but I wouldn't call it a game changer, especially given that it took multiple optimization iterations to get at least performance parity with Clojure/JVM.

18

u/Jeaye Jan 14 '23

Clojure has been developed and optimized for a decade. The JVM has been developed and optimized for multiple decades. It took a few weekends for jank to beat Clojure in this micro benchmark. The fact that multiple optimization iterations were needed is not an issue; it's how software is developed. :)

1

u/deaddyfreddy Jan 14 '23

How about the popular opinion that "native means fast"?

16

u/Jeaye Jan 14 '23

Native programs striving for performance typically avoid all of the dynamic and polymorphic things which make Clojure what it is. If I wrote the same (apply str ...) example directly in C++ (or Rust), without a whole dynamic object runtime on top, without trying to emulate Clojure, it would be ridiculously fast, but that misses the point. On top of this, the JVM excels in doing these things, which makes Clojure very well suited for it, and less suited for native. Combine that with the JVM's optimizing JIT compiler and it's a tough host to beat, if you're trying to be a Clojure dialect.

Yet, still, jank is faster in this benchmark. Something I'm hacking on in my spare time is faster than Clojure on the JVM. Its startup time is significantly lighter, across the board. Its memory profile is smaller. Each of these come from it being native. The same reason people are excited to take an uberjar and turn it into a native image using Graal, except jank takes it a step further, since jank's binaries are smaller and are still entirely interactive. Native can be very fast indeed, and it has a higher ceiling to that speed than the JVM does.

Also addressing your question above about why people are excited about a native Clojure, think about the integrations. Some of the most popular frameworks, libraries, and engines are built in C++. jank would be able to interop with them in the seamless way that Clojure interops with Java, which could give Clojure a clearer way into game development with Unreal Engine or Godot, for example. Or perhaps GUI development using Qt. Furthermore, not only does it interop with these, it does so interactively, so you can start working on your C++ application from your REPL now.

You asked what's so cool about native Clojure. What's so cool about ClojureScript? Or ClojureCLR? Same thing; it's another host for Clojure, which means more integrations, more use cases, and generally people being able to use Clojure in more places. Native or not, more Clojure hosts is just cool.

3

u/zetashift Jan 14 '23

How does one get started trying out jank?

Build locally and invoke the resulting binary?

3

u/Jeaye Jan 14 '23

Best way right now is to join the Slack channel, participate in the design discussions, and be informed of when jank becomes ready. There are still large parts of the language I've yet to implement, and then all of the library work on top of that.

2

u/zetashift Jan 15 '23

Sounds like exciting times ahead!

2

u/z_mitchell Jan 14 '23

What does it mean for something to be “lifted” in this context?

6

u/Jeaye Jan 14 '23 edited Jan 14 '23

Lifting here means pulling something out of an expression and doing it before hand. So (apply str [1 2 3 4 5 6 7 8 9 10]) is lifted in three ways.

  1. apply is defined as a var which is interned

  2. str is defined as a var which is interned

  3. The vector is defined as a constant

The benefit of lifting is largely twofold:

  1. The work doesn't need to happen in the expression (which is great when that expression is run multiple times; no need making the same vector every time, or interning the same var)

  2. If other code uses the same constants, they can benefit from sharing the lifted ones, rather than having to do the same work

Without lifting, the code for the expression would be something like (using Clojure's API):

RT.internVar("clojure.core/apply").getRawRoot().invoke(RT.internVar("clojure.core/str"), RT.vector(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L));

Compared to how it is with things lifted:

apply.getRawRoot().invoke(str.getRawRoot(), vec);

2

u/z_mitchell Jan 14 '23

That’s very helpful, thanks!

2

u/joshlemer Jan 15 '23

Hey /u/jeaye an idea: you should apply for Clojurists Together funding!

4

u/Jeaye Jan 15 '23

I did! Didn't get it. :) Probably because jank hasn't proven its value enough yet, or just because there were many great submissions!

3

u/NaiveRound Jan 17 '23

Don't get discouraged! I just sponsored your work on Github. =)

1

u/Jeaye Jan 18 '23

Thank you! You're the best and I appreciate the support. :)

Edit: Ah, I see it!

1

u/joshlemer Jan 15 '23

Whaaaaat! Aw man hope you get it next time!

2

u/NaiveRound Jan 18 '23

u/Jeaye: is Boehm GC a permanent addition to jank now? or was it just for experimentation/benchmarking?

I've always been fascinated with GCs. I'm more familiar with the JVM's many GC algorithms than Boehm GC's, but I imagine it's more than good enough for the jank's use cases.

That said, do you know what the specific characteristics of the BGC are? Google points me to https://www.reddit.com/r/programming/comments/9d5kr/ask_preddit_what_was_your_experience_with_boehms/, which is 13 years old!

2

u/Jeaye Jan 19 '23

The plan is to stay with Boehm for now, but I'm not hard stuck on it by any means. I think it's been proven very clear that ref counting would need to be drastically optimized in order to be viable, which would be feasible for generated code, but not for the code which works with jank from C++. This hurts, since I want jank to be just as much a C++ library as it is a language, if that makes sense. So, I've been convinced that jank needs a GC. Some GC. Clojure generates far too much garbage to eagerly clean it up.

Boehm is kind of the standard for a lot of projects, it seems. Notably, as I mentioned in the #jank channel on Slack recently, Unity3D uses Boehm: https://docs.unity3d.com/2021.2/Documentation/Manual/performance-incremental-garbage-collection.html

Unity has proven that a GC isn't a showstopper for real time, even multiplayer games, on desktop, mobile, and web. Fall Guys, Cuphead, Pokemon Go, Slime Rancher, and Valheim all come to mind for very successful Unity games, all of which are using the same GC that jank is now using.

Ultimately, I'm going to follow the profiler and benchmarks. Whatever GC jank uses is going to be guided first and foremost by that.

1

u/NaiveRound Jan 19 '23

Wow, no idea that Unity used Boehm. Good know!

Might not be worth the engineering effort, but you could support multiple GC algorithms, ala the JVM.

Also, conceivably it could be a mix, where some objects are "known" to be ref-counted correctly as they are only used internally to Clojure. That's probably a significant effort in-and-of itself. (I'm thinking of how the JVM allocates int 's on the stack and Integer's on the heap)

2

u/Jeaye Jan 19 '23

Might not be worth the engineering effort, but you could support multiple GC algorithms, ala the JVM.

Yep, what I've done when switching jank to a GC is that I've kept the smart ptr type there, but I alias it to just a raw ptr (or raw ptr wrapper, in dev, with more null checking). This would mean jank could still be compiled with that ptr type being something else, or allocated a different way, since all allocation happens within one overloaded fn. In short, intentionally leaving the door open for changing this again or adding multiple options.

I'm thinking of how the JVM allocates int 's on the stack and Integer's on the heap

This is generally referred to a boxing vs unboxing, rather than different GC schemes, since the unboxed ints are not ref counted either; the stack is cleaned up on by the OS (or VM). I'd like to achieve similar unboxing, where possible, with jank, but I haven't pushed into it yet. But jank's API is set up to make creating runtime objects on the stack (C++'s technical term for this is in automatic storage duration), as much as it can be right now.

9

u/therealdivs1210 Jan 14 '23

I love your project!

A native Clojure dialect with gradual typing would be ❤️.

3

u/deaddyfreddy Jan 14 '23

A native Clojure dialect with gradual typing would be

out of curiosity, what's so cool about being "native"?

1

u/therealdivs1210 Jan 16 '23

With graal native compilation, clojure already can compile to native.

Native binaries are easier to distribute and start up faster.

Also, Some platforms require native binaries

0

u/deaddyfreddy Jan 16 '23

With graal native compilation, clojure already can compile to native.

I'm using babashka and clj-kondo, thanks

Native binaries are easier to distribute

the easiest way to distribute an app is when it's packaged for some repository/store, whether it's native or not - doesn't matter that much

and start up faster

no doubt

Anyway, being "native" doesn't mean being faster by default, especially in competition with JVM.