nbb

Chris McCormick 2025-07-31T06:48:37.583119Z

I finally figured out how to get vim-fireplace to interface with nbb's nrepl. You have to issue this after auto-connecting happens:

:CljEval (ns cljs.user)
I've made a PR to the vim-fireplace README but maybe it belongs in nbb's docs?

👍 1
2025-07-31T14:59:09.265809Z

Thanks for nbb.error/error-report ! Been finding it useful to add to CLIs this past week as it provides a much more helpful and prettier stacktrace than the default. I usually wrap the main CLI fn with a try and this catch

(catch ^:sci/error js/Error e
      (nbb.error/print-error-report e))
Sharing in case others don’t know about it

đź‘€ 1
🙏 1
2025-07-31T19:04:11.768069Z

When printing my nbb based CLI’s help, I’m seeing startup times at around 1.0-1.1s which seems quite long for printing text. I profiled the CLI with node --inspect-brk cli.mjs and found about 80% of the time is in nbb_core.js. I can’t identify the specific fns without source code maps which don’t seem to be available. Has anyone else seen something like this? I’m happy to open an issue for this with more detail

borkdude 2025-08-07T09:37:37.610969Z

I'm still not seeing any source maps in lib with bb dev

borkdude 2025-08-07T09:44:24.535529Z

I'm on your branch and the only difference is that I've added a nbb.edn

borkdude 2025-08-07T09:44:32.632389Z

on the newest commit

borkdude 2025-08-07T09:47:41.125299Z

This is the command generated:

"-M:test -m shadow.cljs.devtools.cli --force-spawn watch modules --config-merge shadow-tests.edn --config-merge '{:compiler-options {:source-map true}}'"
Does shadow support multiple --config-merge?

borkdude 2025-08-07T09:52:52.425009Z

even when removing the first shadow-tests.edn, no luck. darn

borkdude 2025-08-07T09:54:52.817949Z

thheller told me that source maps are the default behavior

borkdude 2025-08-07T09:54:57.162339Z

but I'm not getting any

borkdude 2025-08-07T10:09:58.783819Z

ok, it works, the maps are in lib/cljs-runtime

borkdude 2025-08-07T10:27:37.798499Z

anyway, in the profiler output I just see trees of evaluation + analysis, kind of what is expected. if you load 50 namespaces it may take 1 second, just like in JVM Clojure I guess

borkdude 2025-08-07T10:27:50.333469Z

it all has to be parsed, analyzed and evaluated on the fly

borkdude 2025-08-07T10:28:10.930999Z

if there's any quick wins I'd want to to know but it's not apparent to me where the gains are

2025-08-07T13:52:42.675609Z

Mornin. Good to hear sourcemaps worked. Thanks for taking a look and bummer there’s no quick win. Fwiw one downside of the lazy approach is that nbb bundle can’t be used since the lazy namespaces aren’t included. For now I’ll just distribute the CLI with nbb-logseq as a dep and https://github.com/logseq/logseq/pull/12000/commits/77f5dcb6152ec456009f73e3b48cb3c22f30bf50

borkdude 2025-08-07T13:53:08.635249Z

> Good to hear sourcemaps worked. Well, they did appear on disk but I didn't see them work in the chrome tab

👍 1
borkdude 2025-08-07T14:03:09.351269Z

if you want even faster startup times, move stuff to pre-compiled CLJS would still be my advice for now

đź‘€ 1
2025-08-05T18:07:01.064949Z

I decided to use nbb’s bundle approach to see if it would speed up the CLI. It still takes about 1.5s+ to start. Most of the time is in loadString. I’ve attached the bundled cli which has 55 loadString calls. I tried https://github.com/logseq/nbb/blob/2888eb678b92631eb78b1e2a6434c7e296b998ec/src/nbb/impl/bundler.cljs#L203-L204 to batch all the loadString calls into one loadString but reading load-string it looks like it’s only meant for one ns. Any thoughts on how I could speed up the initial load? Perhaps there’s a way to batch load sci namespaces that’s not in the public api

borkdude 2025-08-05T18:15:59.005239Z

Is there a specific nbb version where it became slow? or just happened over time?

borkdude 2025-08-05T18:19:06.629819Z

I'm getting this:

$ node out-cli.mjs
node:internal/modules/esm/resolve:264
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/private/tmp/dude/node_modules/@logseq/nbb-logseq/lib/nbb_cljs_bean_transit.js' imported from /private/tmp/dude/out-cli.mjs

2025-08-05T18:19:09.532019Z

This is a https://github.com/logseq/logseq/pull/12000 I’m writing and probably has more namespace deps than previous CLIs. This is my first time profiling this CLI so I’ve only ever used it based off of nbb 1.3.204

borkdude 2025-08-05T18:20:01.837619Z

And if you use nbb in the unbundled way, is it still doable? Perhaps this is due to loading all the things while you could just lazy load things?

borkdude 2025-08-05T18:21:20.578759Z

I'm also getting:

Could not resolve symbol: tc/to-date
when I comment out some files.

2025-08-05T18:24:58.300189Z

> I’m getting this: I don’t know how you’re building it but it seems like it’s not picking up nbb-logseq’s deps. nbb-logseq is nbb bundled with extra deps. With nbb-logseq checked out, I build it with rm -rf .shadow-cljs && NBB_CLI_NAME=nbb-logseq NBB_NPM_LIB_NAME='@logseq/nbb-logseq' bb release . There’s one fix for nbb-features I should push if you want to repro it locally

borkdude 2025-08-05T18:26:00.774359Z

I just installed nbb-logseq from npm

borkdude 2025-08-05T18:27:40.494129Z

I can try your branch and building it locally but some other time this week

1
2025-08-05T18:32:28.795129Z

> And if you use nbb in the unbundled way, is it still doable? Perhaps this is due to loading all the things while you could just lazy load things? Unbundled are the times I reported at the top of the thread so it’s not doable as far as what I can have users using. This would be logseq’s main CLI which possibly thousands of users would use. I’m open to lazy-loading namespaces but unsure which ones to do and if that would shave off significant time. All these namespaces are also used by the web, desktop and mobile apps so it adds loading complexity there as well

2025-08-05T18:33:42.132689Z

The nbb-logseq on npm is fairly out of date. I’ll push up a complete repro on that branch with specific instructions so hopefully a repro is pretty quick

borkdude 2025-08-05T18:34:12.234609Z

You mentioned that you need source maps. Since you build your own fork off of nbb, is it possible to enable them?

2025-08-05T18:36:24.404379Z

I tried enabling them but I couldn’t get them to work as expected with node’s inspector. I’m happy to push those changes on to this branch to if you want to try them

borkdude 2025-08-05T18:38:01.463339Z

it would be helpful to find out what part of nbb.core is slow. There is some stuff that could be optimized, I'm thinking of sci/binding especially since that mimicks JVM clojure semantics, but it shouldn't have to in CLJS. But until we find out what is causing slowness using source maps and profilers, it's just a guess

👍 1
borkdude 2025-08-05T18:39:28.696559Z

Perhaps functions like seq? etc are also slow since it uses satisfies? in CLJS

borkdude 2025-08-05T18:39:37.944619Z

stuff like that would be good to optimize

borkdude 2025-08-05T18:39:51.915239Z

but I'm just stabbing in the dark here

borkdude 2025-08-05T18:40:42.006949Z

zooming out a bit, is there a specific reason you're using nbb - you need others to use eval for example?

borkdude 2025-08-05T18:41:53.909159Z

you can move more stuff to compiled CLJS for example to make the part that's interpreted smaller

2025-08-05T18:47:57.949499Z

Not sure if you’re asking why nbb in general or for this specific CLI. In general, because it would allow us to provide an ecosystem for users to write and share scripts that quickly do useful things with logseq. For this specific CLI, I could move to compiled cljs although I would be sad to leave behind nbb’s more productive dev env

borkdude 2025-08-05T18:48:36.420019Z

for sure. we'll take a look where we can optimize stuff

borkdude 2025-08-05T18:50:12.635829Z

so the easier it is for me to run the repro somewhere this week, the better

👍 1
borkdude 2025-08-06T13:02:31.899569Z

Can you summarize for me how I can build your project locally and run it, step by step?

2025-08-06T15:23:02.927719Z

Mornin. I thought more on what you said about lazy loading and realized that lazy loading in the CLI is enough to fix my issue - https://github.com/logseq/logseq/pull/12000/commits/456f853174c93f4597ca03ec48eddf77b296361d . Instead of requiring 55 namespaces at 1.7s, the cli now requires 6 namespaces at 0.25s. That works great for me! Thanks for the suggestion

👍 1
2025-08-06T15:23:28.518739Z

Down the road it may be nice to improve load times if nbb scripts start to average requiring 10+ namespaces. One other load time data point - https://github.com/logseq/logseq/blob/master/deps/db/script/query.cljs loads 20 namespaces (5 coming from malli) and loads in about 0.7s

borkdude 2025-08-06T15:49:55.262699Z

Ok if you have some ready to go profiling stuff going on happy to take a look to improve things in the future but happy that you managed with lazy loading for now :)

👍 1
2025-08-06T15:58:41.547689Z

I don’t have a specific profiling thing setup. If you’re curious, here’s my wip sourcemap attempt with profiling that I attempted. I added optional sourcemaps to nbb with https://github.com/logseq/nbb/commit/4e3a6ebaa65e73e697faea3d59646b065830671d. I then enabled sourcemaps and invoked the inspector on the node nbb script with node --enable-source-maps --inspect-brk cli.mjs . The .js files showed up in chrome’s devtools Sources tab but then disappeared after I ran profile

borkdude 2025-08-06T18:39:38.963489Z

if you hit profile, how do you actually execute something without completely restarting?

borkdude 2025-08-06T18:50:06.356799Z

ok I figured it out

borkdude 2025-08-06T18:50:11.710789Z

now how do I load those 50 namespaces?

borkdude 2025-08-06T18:50:27.728789Z

I did remotes/origin/add-optional-sourcemap) $ node --enable-source-maps --inspect-brk cli.js --help

borkdude 2025-08-06T18:50:34.204589Z

but it seems to be vanilla nbb right

borkdude 2025-08-06T18:50:53.264819Z

I think even without source maps I'd get a pretty good idea where to look

borkdude 2025-08-06T18:54:58.365669Z

this should give me some useful info maybe:

node --enable-source-maps --inspect-brk cli.js  -e "(require '[honey.sql])"
with nbb.edn:
{:deps {com.github.seancorfield/honeysql {:mvn/version "2.2.868"}}}

👍 1
2025-08-06T18:58:00.145169Z

Yep. Adding deps and getting to about 50 load-file calls should repro the load time I was seeing. medley/medley, metsonin/malli, io.github.nextjournal/nbb-test-runner are other nbb deps that you could add

borkdude 2025-08-06T18:59:00.166579Z

hmm, yeah now I can see how source maps are going to be useful

borkdude 2025-08-06T18:59:42.271819Z

I don't see shadow spitting out source maps though

borkdude 2025-08-06T18:59:47.043419Z

perhaps that should be configured

2025-08-06T18:59:50.072059Z

Putting a print statement in nbb.core/load-file was an easy way I found to monitor the number of calls

borkdude 2025-08-06T19:00:35.966649Z

$ ls lib/
cljs-runtime/              nbb_cljs_bean.js           nbb_deps.js                nbb_impl_main.js           nbb_math.js                nbb_promesa.js             nbb_repl.js                nbb_tests.js               nbb_zip.js
nbb_api.js                 nbb_core.js                nbb_goog_crypt.js          nbb_js_interop.js          nbb_nrepl_server.js        nbb_reagent_dom_server.js  nbb_spec.js                nbb_tools_cli.js
nbb_bundler.js             nbb_data.js                nbb_goog_string.js         nbb_main.js                nbb_pprint.js              nbb_reagent.js             nbb_test.js                nbb_transit.js

borkdude 2025-08-06T19:00:41.398739Z

there should be source maps here right?

2025-08-06T19:02:08.299549Z

Yes. With my branch source maps are optional so build/release should be called with {:include-source-map? true}

borkdude 2025-08-06T19:03:11.546929Z

I'm using bb dev but not seeing source maps

borkdude 2025-08-06T19:04:21.353789Z

can you do ls lib/ and show that you have source maps?

2025-08-06T19:05:47.207109Z

I only ever used bb release. I could try with bb dev in a min

borkdude 2025-08-06T19:06:13.645079Z

bb release is painful if you want to iterate

2025-08-06T19:55:46.638699Z

After pushing https://github.com/logseq/nbb/commit/52c3cdea432fa3785ca48565dff65468f71dfe6b, I’m seeing sourcemaps in lib/ for dev and release tasks

borkdude 2025-08-06T20:16:30.236269Z

oh thanks!

borkdude 2025-08-06T20:16:56.419269Z

I'll take a look tomorrow

🙌 1
borkdude 2025-07-31T19:07:09.915489Z

It depends how you're invoking nbb I guess

$ time node_modules/.bin/nbb -e '(+ 1 2 30)'
33
node_modules/.bin/nbb -e '(+ 1 2 30)'   0,07s  user 0,01s system 97% cpu 0,091 total

2025-07-31T19:18:23.084009Z

I’m seeing similar times when I comment out loadFile in my https://github.com/logseq/logseq/pull/12000/files#diff-5a2292e8c38c8cb7586476ce5a505e0956b6800c00a57c4e3dee02824505de8e:

# When I comment out loadFile and remaining lines in cli.mjs
$ time node cli.mjs
node cli.mjs  0.07s user 0.01s system 99% cpu 0.083 total
# When I just uncomment line 10 containing loadFile call in cli.mjs
$ time node cli.mjs
node cli.mjs  1.77s user 0.08s system 200% cpu 0.925 total
The cli has https://github.com/logseq/logseq/blob/master/deps/db/nbb.edn but something seems off for this much time to be spent before executing any script code

borkdude 2025-07-31T19:22:41.822829Z

perhaps try running with a dev build and insert some printlns?

2025-07-31T19:28:09.724669Z

I can try that. I’m also going to see if I can enable source maps and see the specific nbb.core fns in the chrome profiler. I’m using nbb-logseq which is using the latest nbb - 1.3.204

👍 1