squint

2025-02-21T20:29:55.626739Z

Just thought I'd give a report, I recently ported my NES audio emulator to official cljs and then to squint, and took some benchmarks: Pure JS: 13ms per frame CLJS: 25ms per frame Squint: 18ms per frame A frame of audio is 1/60 second, and the emulated CPU runs at 1.7MHz, requiring several updates each cycle. This was after changing the CLJS port to use native arrays instead of swapping atoms. It took 300ms+ before that. The biggest thing still slowing down the cljs version are the equality checks. Squint wins there by using the native ==

🎉 6
2025-02-22T19:51:39.392749Z

Yeah that was pretty dramatic. I can't say exactly how much because I realized I was measuring it while running in a RAF loop... Now I have a proper benchmark that runs 1000 frames in a while loop and gives an average, and it's well below 5ms, but there's still room for improvement because the JS version is under 2ms. I think getting rid of the rest of the swaps will be next.

borkdude 2025-02-22T19:55:54.276049Z

Yes, you could just use a mutable var instead of an atom for this + swap!

borkdude 2025-02-22T19:56:07.349529Z

Eh set! I mean

2025-02-22T23:55:28.881009Z

and it works!

2
🎉 6
2025-02-27T19:46:43.805189Z

That song really takes me back. Great work!

2025-02-27T19:47:44.094509Z

Would be cool to be able to browse through a library of old games and play their tunes.

2025-02-27T19:49:08.700809Z

Thanks! It's now a chiptune editor that compiles Clojure code into NSF files

2025-02-27T19:52:03.399219Z

There is a website that archives most of the soundtracks, I'd have to look into whether it would be appropriate to make a thing that fetches them on demand Also, https://chiptune.app/ already exists EDIT: Actually, it could be useful because I also have an importer, which does the inverse by turning NSF files into Clojure code (and for extra fun, there's a function that runs the tune through a Markov chain randomizer!)

2025-02-21T20:33:58.028969Z

that's so cool

2025-02-21T20:36:45.578149Z

It would need to be below 15ms to be viable for realtime playback, but as you can see the JS version just barely makes it

2025-02-21T20:37:21.711039Z

(and those times are without doing any audio processing that would be needed for playback)

borkdude 2025-02-21T20:47:48.271279Z

Thanks for sharing! There are some tricks to speed up the code to make it even more like native JS but then I'd need to see the code :)

2025-02-21T20:53:30.762229Z

I checked it in here if you care to look: https://codeberg.org/bobbicodes/lispytunes-squint Fair warning though, it's... well, it's an emulator, lol I'm gonna be sharing this on an upcoming Apropos, so it would be extra cool if it could be all in Clojure

borkdude 2025-02-21T21:22:34.104329Z

before I look, did you advance compile CLJS and did you process the code with esbuild for squint to optimize it?

2025-02-21T21:26:22.768249Z

I did compile cljs advanced but actually didn't make any difference This was just running with Vite in dev mode, I haven't tried building it yet

2025-02-21T21:27:12.074919Z

but to be fair, that was the same way I measured the JS one

2025-02-21T21:28:12.309369Z

oooh, it looks like building it got it down around 13-15ms :)

borkdude 2025-02-21T21:28:42.851599Z

Building might help a tiny bit. I see a few things that could be optimized (in squint). E.g. aset could be directly translated to JS without invoking aset. https://squint-cljs.github.io/squint/?src=KGxldCBbeCBbMF1dCiAgKGFzZXQgeCAwIDEpKQ%3D%3D I'm a bit surprised I hadn't done this yet. There's a few other things I see in the compiled code that could be improved. I'll get back to you later.

borkdude 2025-02-21T22:42:46.317779Z

@btowers793 ok, I optimized aset in this release now: https://github.com/squint-cljs/squint/releases/tag/v0.8.135 try again with and without build :)

borkdude 2025-02-21T22:43:02.534759Z

there's more stuff to optimize but it's night time here so I'll look again tomorrow

2025-02-21T22:43:26.773279Z

Thanks! This is really great

borkdude 2025-02-21T22:44:49.065889Z

I discovered one bug though if you use aset in return position, let me fix that real quick.

borkdude 2025-02-21T22:47:37.324049Z

ok v0.8.136 should be good

2025-02-21T22:57:46.040829Z

14ms in dev, 12 in prod 🙂

🎉 5
borkdude 2025-02-21T23:00:31.927209Z

ok, I'll check tomorrow for more speedups :)

🚀 1