Fork me on GitHub
#clojurescript
<
2022-03-18
>
wombawomba14:03:14

Following up on yesterday's discussion: wrapping my unwanted code in delay helped the compiler eliminate it. Taking a closer look at the JS output, I notices that there are other function calls that the DCE doesn't seem to eliminate, such as the following. Sets:

(def unused-set #{...})
becomes
PersistentHashSet$createAsIfByAssoc(...);
Atoms:
(def unused-atom (atom ...))
becomes
cljs$core$atom(...);
Some fn calls:
(defn f [x] (str x "bar")
(def unused-map {:a {:b (f "foo")}})
becomes
$f$ = function($x$) {[$str$($x$), "bar"].join("");};
$f$("foo");
It seems like if possible, helping the Closure compiler eliminate these things could lead to pretty large savings in some circumstances. The emitted JS for some parts of my codebase consists almost entirely of this type of dead code.

dnolen15:03:33

@wombawomba that is interesting about delay - but I don't really see anything pressing for us to do here

Ferdinand Beyer15:03:26

@wombawomba — I would be super interested in a short summary of tips how to write code that optimizes for code elimination based on your findings 🙂

dnolen15:03:44

by pressing I mean nothing critical wrt. priority - some of these seem like minor patches we'd be happy to take if provided

dnolen15:03:09

specifically static sets, and unused atoms - these two cases seem odd

dnolen15:03:27

the str map thing - not going to do anything about that

wombawomba15:03:08

@ferdinand sure, I'll write something up

pinkfrog15:03:58

Hi guys, how do you log with clojurescript?

pinkfrog01:03:30

Was using timbre, but now thinking limiting the unnecessary dependencies. Since Goog ships with cljs, I shall directly use goog

pinkfrog15:03:06

tools.logging is missing in cljs

dnolen15:03:17

Google Closure Library has a logging thing - I believe it's been wrapped to be generic by some open source libs?

pinkfrog15:03:10

Are you directly using that library?

dnolen16:03:17

have used it directly, and wrapped in some cases, haven't personally found shared logging to be particularly useful

Yosevu Kilonzo19:03:14

Hello, I’m trying to translate some JS to CLJS and running into an error. Does this syntax look correct? Working JavaScript

const processor = unified()
  .use(parse)
  .use(uniorg2rehype)
  .use(rehypeParse)
  .use(rehypeReact, { createElement }) // Same error if I comment this line

const file = await processor.process(`* org-mode example\n your text goes here`);

console.log(file.result);
Not working ClojureScript (Uncaught TypeError: Cannot process without Compiler)
(def processor (-> (unified)
                   (.use org-parse)
                   (.use org-to-rehype)
                   (.use rehype-parse)
                   (.use rehype-to-react (clj->js {:createElement createElement})))) ;; I think the problem is here

(go (let [file (<p! (.process processor "* org-mode example\n your text goes here"))] ;; Fails here
      (.log js/console (:result file))))

p-himik20:03:36

Depending on what createElement is exactly, maybe replacing (clj->js {...}) with #js {...} will help.

p-himik20:03:13

Apart from that, your code looks pretty much what it should be. If that <p! is the only reason you need the go block here then I wouldn't use it at all - IMO better to use promise API, i.e. (.then ... (fn [file] ...)).

🙏 1
Yosevu Kilonzo20:03:41

Thanks @U2FRKM4TW, I did also try #js {...} , same issue.

p-himik20:03:46

Maybe the error lies elsewhere then, and not in that particular piece of code.

👍 1
thheller06:03:49

(:result file) should be (.-result file)?

thheller08:03:16

and FWIW don't use go for a single "await". use .then instead. the go will generate substantially more code when this is all you really need.

(-> (.process processor "* org-mode example\n your text goes here")
    (.then (fn [^js file]
             (js/console.log (.-result file)))))

Yosevu Kilonzo15:03:59

Thanks @U05224H0W! That was my initial syntax error. Also, note taken about go There is another issue underlying this one since rehype-react is an ESM only package. I’ll ask in #shadow-cljs.