funcool/cuerdas

Make cuerdas amenable to Closure DCE

borkdude opened this issue · 8 comments

Cuerdas has a significant impact on the size of compiled JavaScript output from Google Closure.

Quoting mfikes from Slack:

Adding cuerdas as a dep and simply making a call to cuerdas.core/format adds 47 KB unzipped and 19 KB zipped. Perhaps there is some stuff in that library that is not amenable to DCE.
By manually pasting in cuerdas.core/format and all of the functions it depends on, things grow by only 723 bytes uncompressed, 359 bytes compressed. But, by then simply adding [cuerdas.core] to my :require vector (without actually using any functionality in it) causes it to explode back out by 47 KB / 19 KB. So there is something in the cuerdas library that is not amenable to DCE
I think I've isolated it to this being pulled in, regardless of whether you want it https://github.com/funcool/cuerdas/blob/master/assets/xregexp/xregexp.js
So, by using this library instead of goog.string/format your asset will grow significantly. Might be worth filing an issue about.

Might be worth looking into.

Hi @borkdude
I understand the problem, and I'm practically sure that this is because of xregexp. But using xregexp is a tradeoff. If you want correctness we need to have properly regular expression support with all unicode stuff.

Take care that many functions (such as blank?, letters? and many others) handles properly the unicode characters... and this require that dependency that is loaded as is, because is not google closure compiler compatible.

As I said before, is a tradeoff, if you know any improvement in this area, that will be very welcome, but the correctness is important. If you don't want correctness, I suggest use the clojure.string or any alternative.

Maybe with the new stuff on clojurescript compiler that converts nodejs modules into gclosure modules, we can setup the package that just imports the xregexp as gclosure (converted) module... and this will make the package more friende to the DCE... I don't know.

After some hacking with that, i found that :foreign-libs with module transformations stills does not works if they are declared in deps.cljs so I think we still can't improve the current situation :(

After hacking a little bit more, I have converted the entire XRegExp.js source into google closure modules and integrated it into cuerdas,... but unfortunatelly this does not improves the output...

(ns cuerdas.experiment
  (:require [cuerdas.core :as str :include-macros true]))

(js/console.log (str/format "hello %s and %s" "yen" "ciri"))

Compiling a file with that content includes all the XRegExp stuff in the bundle (but not uses it). For some reason DCE is not working but I'm not sure why... :(

I have opened an issue related to the module conversion and deps.cljs: https://dev.clojure.org/jira/browse/CLJS-2399

@niwinz sorry for hijacking this issue I was wondering why xregexp and not using :npm-deps. I am looking at converting cuerdas, as part of macchiato, to self-host (lumo) ClojureScript.

Also which version would I need to target if I were to use the npm version?

the latest one

Looks like this issue has been resolved in the latest version where xregexp has been removed:

https://github.com/funcool/cuerdas/blob/master/CHANGES.md#version-20210529-0