Fork me on GitHub
#babashka
<
2022-11-30
>
borkdude10:11:28

I have worked on including spec.alpha as a pre-compiled built rather than an https://github.com/babashka/spec.alpha but there are some edge cases I haven't had time to find out yet. The optional library works better but is interpreted, thus slower. As a compromise, I'm going to include the sources of the library in the bb binary which should add no more than 100kb or so, and this will make the UX of not having to include spec.alpha separately better, while having the same perf as before, with only marginal extra binary size.

4
jeroenvandijk10:11:25

Nice idea. Is there a big difference in loading time for a source from the filesystem vs a source in the binary itself?

borkdude10:11:16

no, the same. I could pre-compile the interpretation though, (by evaluating the source in bb at compile time)

borkdude10:11:30

but that may have a different effect on the image size. I'll try it

jeroenvandijk10:11:26

Ah i see, nice trick

borkdude10:11:01

the trade-off here is that currently I've put these "internal sources" at the end of the classpath to they are easy to override if you still include the library yourself

borkdude10:11:15

I think I'll leave it like this for now since moving the evaluation to compile time requires more changes

jeroenvandijk11:11:17

Makes sense. But sound like useful tricks to keep in mind

borkdude11:11:35

I'll have a quick try

borkdude11:11:18

Made the refactor. Binary size before:

$ ls -lat ./bb
-rwxr-xr-x  1 borkdude  staff  72399695 Nov 30 11:47 ./bb
Now trying the compile time change...

borkdude11:11:31

ah right, that doesn't work, since some things can't be initialized at build time, like java.net.http classes

borkdude11:11:51

(def class-map
  "A delay to delay initialization of java-net-http classes to run time, since GraalVM 22.1"
  (delay (persistent! (reduce (fn [acc c]
                                (assoc! acc c (Class/forName (str c))))
                              (transient class-map*) (when features/java-net-http?
                                                       java-net-http-classes)))))

😎 1
borkdude11:11:11

I can probably work around that :/

😅 1
borkdude12:11:03

Got the workaround in place now 😅

borkdude12:11:52

new binary size:

$ ls -lat ./bb
-rwxr-xr-x  1 borkdude  staff  75537359 Nov 30 13:11 ./bb
hmmm

borkdude12:11:40

but the loading time of spec got a lot better, but the cost seems to be 3mb or so.. which is almost equal to having it as a compile time thing

borkdude12:11:48

so imo not worth it, currently...

borkdude12:11:42

I think the built-in source + low image size addition strikes the right balance for convenience and as little bloat as possible. When spec non-alpha ever comes out, I'll include it as a fast built-in

👍 1
borkdude12:11:12

They are running on the same linux and JVM version, yet I get different output for:

$ clojure -M:babashka/dev test-resources/babashka/file_location1.clj
----- Error --------------------------------------------------------------------
Type:     java.lang.ArithmeticException
Message:  Divide by zero
:/

borkdude10:11:40

$ time ./bb -e "(time (require '[clojure.spec.alpha :as s])) (s/def ::int int?) (s/valid? ::int 3)"
"Elapsed time: 52.029708 msecs"
true
./bb -e    0.06s  user 0.01s system 91% cpu 0.075 total

licht1stein13:11:49

I recently started using babashka for our git hooks and wrote up the process. I think it's extremely convenient :) https://blaster.ai/blog/posts/manage-git-hooks-w-babashka.html

borkdude13:11:52

This is an extremely cool article, thank you!

borkdude13:11:42

@U01V67SBM55 You can mention articles like this in #C8NUSGWG6 for. more exposure, if you want

emilaasa13:11:43

@U01V67SBM55 implementing this immediately!

emilaasa13:11:45

Looking good

licht1stein14:11:55

Thanks @U04V15CAJ posted there too 🙂

borkdude13:11:14

https://github.com/babashka/babashka: Native, fast starting Clojure interpreter for scripting 1.0.167: the ultimate Advent of Code edition! babashka https://github.com/babashka/babashka/blob/master/CHANGELOG.md

🎉 1
👍 1
😆 1
borkdude15:11:01

In the newest babashka (1.0.167) you can do this without having a fork of clojure.spec in your bb.edn

$ bb -e "(require '[clojure.spec.alpha :as s]) (s/valid? int? 2)"
true

🎉 5
avfonarev15:11:22

Is there a way to use tasks in babashka in such a way that if a task fails, the task depending on it does not get executed?

borkdude15:11:21

That is already the behavior

avfonarev15:11:42

Oh, I did not get that from the book. What is the correct way to fail?

borkdude15:11:57

throwing an exception

avfonarev15:11:24

Thanks a lot. Maybe it is worth mentioning in the docs

borkdude15:11:42

would you mind creating an issue or PR to the book?

🙌 1
avfonarev15:11:23

Absolutely. Thanks for your work and the amazing tech support

Tommy22:11:36

im trying to write transit in a bb script, i'm not sure how to do it and cant find an example. ive tried

(with-out-str 
 (transit/write ( *out*) "foo"))

borkdude22:11:41

you mean to a string?

Tommy22:11:44

yes. I figured it out. (def out (ByteArrayOutputStream. 4096))`` failed so I assumed it wasnt supported on babashka. in reality i forgot to import it