8 ms·
The Carcinization of Go Programs
- dottedmag 4y agoTL;DR: Instead of building Rust shared library for every OS/arch combination to use it from Go program, produce WASM chunk from Rust and load/execute it in Go using wazero.
- iudqnolq 4y agoThe interesting bit for me was that executing the moderately nontrivial wasm fn took <1ms. That makes it more practical than I'd have expected.
- infogulch 4y agoI wonder what the performance penalty is compared to running directly on native machine code.
- manchmalscott 4y agoI think the wasm runtime they used compiled it to native on program boot, so I’d guess the penalty is really only on startup time?
- iudqnolq 4y agoI have no experise, but my gut feeling is that the sandboxing should still add some cost?
- nicoburns 4y agoI believe wasm is usually within 2x of native machine code, with the caveat that it can’t do simd or multi threading.
- pjmlp 4y agoIt can in latest versions of the standard, but it is still far from what JVM and CLR bytecodes offer in the box.
- deleted 4y ago[deleted]
- rs_rs_rs_rs_rs 4y agoPersonal site with ads, seriously?
- adaml_623 4y agoI just get a message complaining that I'm using a web blocker even though I'm not.
- cinntaile 4y agoYou're probably using a DNS ad blocker.
- Whiteshadow12 4y agoWhat browser are you using, if it's firefox it might affect it?
- littlestymaar 4y agoublock origin on FF here, no issues.
- gwd 4y agoTheoretically I have a tracker blocker. I have absolutely no problem seeing ads -- it's being tracked around the internet that I don't like. And no, I won't make an exception for your site, even if I do like your content. It's possible that their ads don't actually do any tracking, in which case they're receiving collateral damage. They seem technical enough, they should be able to figure out why the tracker blockers are blocking their ads and get things fixed (either in the ad code or in the upstream tracker blocker software).
- nottorp 4y agoProblem is there are so many sites that it's hopeless to turn off firefox anti tracking and ublock origin based on a promise from one site that i've seen once because it was on HN...
- 4y ago
- EdwardDiego 4y agoI feel bad that I can't get over the fact that a Mastodon message is called a "toot". It just keeps reminding me of the old rhyme about beans, beans, the magical fruit, the more you eat, the more you toot. (Although in the English of most former colonies that didn't violently rebel, it's pronounced "fart"). I suppose that's the point, it's a nice meta joke punning on tweet, but implying it's all just farting in the wind.
- laumars 4y agoWhere I live we also call it “trump”. Which is why many of us never understood why people would claim that Donald Trump has a “powerful name”. However it did amuse use that a man full of hot air would be named after the expelling of warm but noxious air.
- EdwardDiego 4y agoOkay, can you please give me a hint as to where that is? As googling "trump fart" just leads to a lot of terrible Youtube videos.
- reom_tobit 4y agoI’m not the GP but in the UK “trumping” is what we call it (amongst other things, lots of fun words for body sounds).
- rob74 4y agoI think the (originally German) name is derived from trump cards - which are also powerful, but in a somewhat different way...
- laumars 4y agoThat’s only a modern day theory and feels somewhat retrofitted interpretation of the facts (from what I’ve seen when researching this myself). There are various spellings of the surname, since names often didn’t have a formal spelling in the early days of book keeping. And some of the spellings begun with a ‘T’. So the more likely scenario is the name was only picked because it sounded similar to their original German family name while being an English-styled spelling. Given we are taking 30+ years ago though, the best we can do at this stage is speculate. However even if the card game theory were true, it still doesn’t make Trump a powerful name in the U.K. because the fart connotation is still more prevalent.
- kretaceous 4y agoMy first thought after reading the problem was "WASM?" and I was glad to see a detailed writeup in the same. Have a question: Isn't the wasm file compiled by rust already a binary? Why do we need to compile it first and then run it in the Go code? Thanks!
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- hmry 4y agoIt is a binary, a WASM binary. However, your CPU can't execute WASM binaries. That's why you would either have to use an interpreter to execute them (which would be slow), or compile the WASM code into native machine code for your CPU first. Then you can run that machine code directly. The wazero library supports both of those options AFAIK.
- kretaceous 4y agoThanks. > CPU can't execute WASM binaries How was OP able to pipe the output of echo directly to the wasm binary initially?
- loosescrews 4y agoAlternatively, you could use Bazel and have it handle the shared objects. I normally put them on an object store (e.g. GCS).
- febusravenga 4y agoOr any build tool like make. When you are hammer ...
- dom96 4y agoYou going to give your end-users `make`/`bazel` as well?
- jstanley 4y ago"Carcinization" is not a reference to creating cancer as you might first think, it is instead "an example of convergent evolution in which a crustacean evolves into a crab-like form from a non-crab-like form"[0] - in this case because the Rust mascot is a crab. [0] https://en.wikipedia.org/wiki/Carcinisation https://en.wikipedia.org/wiki/Carcinisation
- fxtentacle 4y agoOdd. When I read the article, I thought "creating cancer" is a pretty good description of the idea that one embeds a whole f*ing JavaScript runtime and accept a 10x performance decrease on runtime just so that you can avoid copying one .so file around.
- masklinn 4y ago> I thought "creating cancer" is a pretty good description of the idea that one embeds a whole f*ing JavaScript runtime wasm is not a javascript runtime. > and accept a 10x performance decrease on runtime Unless SIMD is involved (which is indeed a major issue around warm), the overhead of compiled wasm is generally observed below 50%, often closer to 15. Obviously if you're using a wasm interpreter all bets are off.
- deleted 4y ago[deleted]
- nuc1e0n 4y agoIt might involve a performance penalty today, but in the years to come vendors will be investing in making it fast. And Wasm has a much smaller surface area to optimise than JavaScript does.
- masklinn 4y agoThe overhead I’m talking about is compared to native code, not to javascript. I’m not sure why you’re bringing up javascript, it’s nowhere in the discussion except in ggp’s misconceptions.
- grose 4y agoNice article! I think this is an exciting approach to cross-platform support. I took a similar approach for Trealla Prolog's Go library: https://github.com/trealla-prolog/go https://github.com/trealla-prolog/go The biggest roadblock at the moment is that Go WASM libraries are not in a very good state, except wazero. My assumption is that wazero will be slower than the cranelift-optimized wasmtime, etc. but I have not seen any benchmarks anywhere. My impressions of the Go WASM libraries: wazero: great Go-friendly API, not sure about performance, works on all OS (what the author chose) wasmer: provides bare-minimum input and output for WASI, fast (it's what I chose for trealla-go). Doesn't work on Windows without significant pain, doesn't static compile the wasmer libraries so distribution is a pain. Seems essentially abandoned as its main contributor left the company. wasmtime: basically impossible to get input and output in any reasonable way (unable to set stdin or read stdout; it can only inherit the FDs from the host), but might finally get buffers for I/O soon. wasmedge: haven't investigated this yet, but it seems like it solves many of the problems above, promising If anyone knows of a benchmark between these, I'd love to see it. I'm especially interested in wazero's execution speed. I think I'll try and run Trealla against it. edit: I should also mention that wazero avoids the cgo overhead which could be a significant optimization for small functions as in this article, even if the runtime is slower. My use case is a long lived interpreter which is quite different.
- adrianfcole 4y ago(wazero contributor) thanks for the analysis. certainly dependency pain is something we hear about, as well as lack of maintenance on alternatives. Some don't release at all or rarely, and many break api constantly. We also break api, but then again we aren't 1.0 yet. wrt performance, we do watch it closely, but we've not done benchmarks vs non-default settings in other runtimes. It is the case that the tradeoff of avoiding dependencies means any optimizing JIT would have to be written to make long running things faster over time... no one has contributed this, yet, and more folks are looking at short lived despite it being the case interpreters are a great example of long lived. Concretely, we do keep track of relative performance (literally each commit in fact), and it isn't uncommon to find PRs including perf before/after. here are some pointers if interested in digging in! there are certainly some things we'd lose at. https://github.com/tetratelabs/wazero/actions/runs/3523069865/jobs/5906786822 https://github.com/tetratelabs/wazero/actions/runs/352306986... https://github.com/tetratelabs/wazero/tree/main/internal/integration_test/vs https://github.com/tetratelabs/wazero/tree/main/internal/int... https://github.com/tetratelabs/wazero/blob/main/Makefile#L32 https://github.com/tetratelabs/wazero/blob/main/Makefile#L32 Thanks for the analysis and good luck on things!
- gw98 4y agoI know the point of this article is to demonstrate how to solve a problem but the premise is a bad one and must not be confused with sound software architecture at all. In fact it's slightly painful reading it and I really wish people would stop writing articles like this. The functional requirement here is to take some HTML, parse it and emit slack flavoured markdown. Involving WASM / Rust / cross compiling stuff and fucking around with a build tool chain should not even be being discussed anywhere as a solution for this. I mean it's fine as a toy but the problem is that half the industry doesn't have any idea what's a good idea and what isn't from an engineering perspective when it comes to solving problems and this will be taken as gospel on how to solve the thing. What we have here is a Rube Goldberg machine, not a cleanly solved engineering problem.
- c7DJTLrn 4y agoofftopic but I was looking at Xe's "salary transparency" page and they are paid quite a bit less than I was expecting. Hopefully they get RSUs too because their skillset is worth more than that.
- xena 4y agoI do get stock options at my current job. The price and amount are not listed because I don't know if revealing that breaks NDA or not. Startups that are pre liquid are weird. If you think I'm worth more, contact me with a job opportunity :)
- dom96 4y agoThis is really cool and shows WASM's power as a glue for different programming languages (that support building to WASM). What I wonder about is how well different Rust libraries support WASM, since this seems to be a requirement for this to work, do libraries need to be careful about ensuring WASM support or is it mostly free?
- kibwen 4y agoI think it depends on what your WASM runtime supports. The OP here is using a runtime called Wazero, which looks like it supports the first prototype of the WASI standard. You can think of WASI like libc for WASM, where it defines interfaces for operating with the underlying system. But WASI is still young and changing, and doesn't support everything that a program might want to do. And certain contexts, like web browsers, probably have no interest in providing runtimes that have full WASI support. If you're interested, I'd suggest compiling a crate for WASI and seeing if you get compilation errors. You can do so by cross-compiling for the `wasm32-wasi` target (`rustup toolchain install wasm32-wasi`, then `cargo build --target wasm32-wasi`). If your program tries to use any stdlib facilities that don't exist on that target, it will tell you.
- pjmlp 4y agoThe same glue that IBM and Unisys mainframes and micros have been enjoying for decades, .NET since 2002, among other examples.
- WesSouza 4y agoLovely. I was not expecting to see WASI.
- dmitriid 4y ago> Mastodon stores toots in HTML and presents that HTML to API consumers. What. The actual. F. How? Why? Who ever thought this was a good idea? I would often complain about Twitter API, and how bad it was, but this is just beyond awful.
- masklinn 4y ago> How? Why? Who ever thought this was a good idea? I guess it avoids needing to invent your own message format, but it means that every client needs an HTML parser in order to process and clean up statuses? That sounds... great.
- deleted 4y ago[deleted]
- rockisch 4y agoMy guess is they're using the same public API in the actual app. Of course, I'm really hoping the HTML is added at runtime, and not stored in the DB. Imagine doing a data migration to modify an HTML stored in the DB. C H A O S
- masklinn 4y ago> Of course, I'm really hoping the HTML is added at runtime, and not stored in the DB. Looks to me like HTML statuses are part of the federation protocol. In fact, there's an issue[0] indicating that the sanitizer of the reference implementation doesn't allow Markdown to federate. There is also an old status indicating that even if more elements are added, the reference CSS basically strips everything (I assume visually)[1]. [0] https://github.com/mastodon/mastodon/issues/8474 https://github.com/mastodon/mastodon/issues/8474 [1] https://is.a.qute.dog/@KS/872334 https://is.a.qute.dog/@KS/872334
- morelisp 4y agoConfluence also does this. Everyone loves Confluence, right?
- 4y ago
- bigbillheck 4y agoIf I read this correctly the whole point of the rigamarole was to be able to use the rust crate 'lol_html'. Is there nothing comparable in the go world? If it's that critical, why are you using go at all? And couldn't you have also saved a bunch of effort at the end by tweaking the rust program so that it wasn't just a one-and-done and only need one call to exec?
- xena 4y agoThen I have to ship the binary around. That makes it no longer a single binary solution.
- bigbillheck 4y agoSorry, I should have said "one call to InstantiateModule" instead of exec. I don't know how go processes work, but surely you can do that in a different context so that the runtime stays alive and you just pass around a way to connect to it. Then on the rust side, you change it to work on lines of input, one line in means one line out. The current way, your go code starts a new system process to process every message, the way I'm suggesting it'll only start one for the whole run.
- xena 4y agoI could look into that, but then doing that would mean that the program wouldn't be only 50 lines of rust :)