r/Clojure • • Jun 26 '23

A case for ClojureScript 2.0

https://tonsky.me/blog/clojurescript-2/
82 Upvotes

25 comments sorted by

View all comments

56

u/thheller Jun 26 '23

Unfortunately there are so many wrong takes in this that I completely disagree with the premise. Yes, :advanced can be intimidating. Yes, it can break a build. However, the reason that is breaks is interop with anything that is NOT part of those :advanced optimizations.

If you can stomach to 3-4x your build size just run with :simple? Arguably, if you can live with that you can also probably take the very minor performance hit that comes with it. :simple requires no externs whatsoever and is almost on par with the minification done by other JS tools. Modern JS tools can tree shake a little, however it is not compatible with :simple so that is a problem. It is also possible to set :compiler-options {:property-renaming :off} in your build config. That will disable to most aggressive optimization of :advanced, but honestly at that point you can just use :simple.

Let's take shadow-cljs with datascript as an example. The reason it breaks is not the lack of externs. The reason it breaks is that the code was written by complecting two entirely different usage scenarios. One is Datascript as a library for CLJS consumers, the other is Datascript as a library published for JS consumers. If you are using the CLJS code from CLJS you do not require any externs whatsoever. The reason it breaks is ignoring the rules, not because :advanced arbitrarily breaks your code. I suspect the code for scenario 2 is just so old that it was written before good options from the build tools existed and never revised.

:advanced is stricter than other tools yes, and interop with not-:advanced compiled output can be tricky. Externs inference made it much less of a pain though. shadow-cljs also tries to infer more externs from the node_modules code it processes, so that it is often even less of an issue.

Now, onto the topic of ClojureScript 2.0. I'm totally with you, there is potential is doing such a thing. Modern JS has changed a lot over the years. Many things are possible today that weren't when CLJS was first released. Many of the choices made are simply outdated. Choosing the Closure Compiler and :advanced optimizations is not one of them. They are optional today, and they would be optional in CLJS 2.0. I'd still work on supporting them because I absolutely want them. I very much care about the sizes of my builds.

Now, I'm not even trying to defend the Closure Compiler itself. Their project management style is horrible. Constantly making breaking changes for no reason is very frustrating. But shadow-cljs for example makes use of many non-public APIs, so this is sort of understandable and expected. It's a choice I made specifically, so I'm fine to deal with it. Fact remains :advanced is still years ahead of any other tool, and not having to write it ourselves made our life much easier.

Comparing the dynamic-ness of CLJ is also sort of a bad take. Clojure absolutely has many issues if you factor in AOT compilation and other kinds of "optimizers" such as GraalVM. You have to sacrifice some dynamic-ness to get those benefits, CLJS is no different on that front.

So, I'm all for starting a CLJS 2.0 effort. shadow-cljs is already working in that direction in some ways. I'm actively trying to avoid a CLJS fork, since what we have to gain is sort of minimal IMHO. Blaming :advanced however is not a premise to start that work on. Just emit modern JS, and :advanced can process it just fine. If you don't want that use something else. The reason that interop requires many hacks nowadays is that CLJS still emits a dead and otherwise unused ClosureJS format. Getting rid of that should be the goal, and the Closure Compiler becomes fully optional as a bonus.

9

u/tonsky Jun 27 '23

Hi Thomas, thanks for reading and for thorough reply! Re:

Just emit modern JS, and :advanced can process it just fine.

Didn’t you also wrote this?

Ideally we want to use :closure as our primary JS Provider since that will run the entire application through :advanced giving us the most optimized output. In practice however lots of code available via npm is not compatible with the aggressive optimizations that :advanced compilation does. They either fail to compile at all or expose subtle bugs at runtime that are very hard to identify.

I don’t understand how these two viewpoints co-exist. Don’t you want to use npm code? Isn’t code published on npm modern enough? Shouldn’t we work with code that is given to us, instead of waiting for all code to become perfect?

Re:

The reason it breaks is ignoring the rules

Aren’t externs part of the rules? I mean, does Google itself proposes to avoid them? I think they only encourage their use, but I might be wrong, maybe situation has changed in the last years.

Re: using :simple

That’s the problem! I want to use simple, but unfortunately advanced complects three things that should be separated: more aggressive renaming, dead code removal and function inlining. Also unfortunately, CLJS _relies_ on advanced to do inlining and produces sub-optimal code counting on that.

Also, it’s not really up to me as library developer to decide which level to use. If I were developing an app—sure, I choose what fits me. But I am developing a library, and my users decide which compilation level to use. So I have to support both

11

u/thheller Jun 29 '23

Didn’t you also wrote this?

Indeed, I did. It doesn't matter what the ClojureScript compiler generates, it won't change what is published to npm. In case you aren't aware most packages on npm are quite a mess and far from standards compliant or even modern. I still have close to zero hope that the vast majority of npm packages will ever survive :advanced, there might be some, but I haven't found one.

That's the deliberate choice made in shadow-cljs as well. CLJS code goes through :advanced, all node_modules code goes through :simple only. Unfortunately that means we need externs, but since Externs Inference that has been pretty much a solved problem if you ask me. And I'm referring to shadow-cljs here, it is still a problem in other toolchains, since Externs Inference is still opt-in there and not enabled by default. So, the warnings it emits often go unnoticed.

Aren’t externs part of the rules?

Not in this case. Externs are meant to teach the compiler about external code, i.e. code it doesn't see during compilation. If you are using externs to fix a problem in your own code then that's a bug if you ask me. Sure, shadow-cljs could be less conservative and apply all deps.cljs externs it finds on the classpath. I decided not to because many of them were auto generated and far too broad, basically often harming your build size instead of supplying anything useful. There is also no way to control when they get included in a build, I strongly dislike including anything automatically just because its on the classpath. It should at least be "required" by something.

CLJS relies on advanced to do inlining

I'm unsure what you mean by this. Yes, Closure may inline code. But it'll only do so for very small functions, or code only used once. So, in the vast majority of cases the benefit of this inlining is negligible. Also, modern JS engine JITs are very good at figuring this out on their own. After all most JS tools don't do :advanced style inlining either, and they seem to be fine.

Could be that you comparing code with :static-fns false, which defaults to true for :advanced builds? This will indeed have a massive impact on perf, but nothing is stopping you from setting it yourself. :static-fns true is actually the default for everything in shadow-cljs, so even for :none, since it is only relevant in a very small amount of cases, and I'd rather have faster running code by default than a feature I'm unlikely to use (e.g. with-redefs) being available. IMHO, YMMV.

I would never advise using :simple unless you absolutely have to. It is simply just too large. We could of course significantly shrink that size, and that's what I'd focus CLJS 2.0 on. Still not the removal on :advanced, since it will remain the superior option.